Skip to content

Commit 18a0c41

Browse files
committed
regex_error_to_buffer
1 parent 0211fe2 commit 18a0c41

1 file changed

Lines changed: 32 additions & 28 deletions

File tree

runtime-light/stdlib/string/regex-functions.cpp

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -267,24 +267,22 @@ class preg_replacement_parser {
267267
}
268268
};
269269

270-
template<size_t N>
271-
void log_regex_error(const char (&msg)[N], int32_t regex_error) noexcept {
272-
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
270+
bool regex_error_to_buffer(std::array<char, ERROR_BUFFER_LENGTH>& buffer, int32_t regex_error) noexcept {
273271
auto ret_code{pcre2_get_error_message_8(regex_error, reinterpret_cast<PCRE2_UCHAR8*>(buffer.data()), buffer.size())};
272+
return true;
274273
if (ret_code < 0) [[unlikely]] {
275274
switch (ret_code) {
276275
case PCRE2_ERROR_BADDATA:
277276
kphp::log::warning("unknown regex error code: {}", regex_error);
278-
return;
277+
return false;
279278
case PCRE2_ERROR_NOMEMORY:
280-
kphp::log::warning("[truncated] {}: {}", msg, buffer.data());
281-
return;
279+
kphp::log::warning("[truncated]: {}", buffer.data());
280+
return false;
282281
default:
283282
kphp::log::warning("unsupported regex error code: {}", ret_code);
284-
return;
283+
return false;
285284
}
286285
}
287-
kphp::log::warning("{}: {}", msg, buffer.data());
288286
}
289287

290288
bool compile_regex(RegexInfo& regex_info) noexcept {
@@ -443,21 +441,9 @@ bool compile_regex(RegexInfo& regex_info) noexcept {
443441
pcre2_code_free_8};
444442
if (!regex_code) [[unlikely]] {
445443
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
446-
auto ret_code{pcre2_get_error_message_8(error_number, reinterpret_cast<PCRE2_UCHAR8*>(buffer.data()), buffer.size())};
447-
if (ret_code < 0) [[unlikely]] {
448-
switch (ret_code) {
449-
case PCRE2_ERROR_BADDATA:
450-
kphp::log::warning("unknown regex error code: {}", error_number);
451-
return false;
452-
case PCRE2_ERROR_NOMEMORY:
453-
kphp::log::warning("[truncated] can't compile pcre2 regex due to error at offset {}: {}", error_offset, buffer.data());
454-
return false;
455-
default:
456-
kphp::log::warning("unsupported regex error code: {}", ret_code);
457-
return false;
458-
}
444+
if (regex_error_to_buffer(buffer, error_number)) {
445+
kphp::log::warning("can't compile pcre2 regex due to error at offset {}: {}", error_offset, buffer.data());
459446
}
460-
kphp::log::warning("can't compile pcre2 regex due to error at offset {}: {}", error_offset, buffer.data());
461447
return false;
462448
}
463449

@@ -687,7 +673,10 @@ bool replace_regex(RegexInfo& regex_info, uint64_t limit) noexcept {
687673
reinterpret_cast<PCRE2_UCHAR8*>(runtime_ctx.static_SB.buffer()), std::addressof(output_length));
688674

689675
if (regex_info.replace_count < 0) [[unlikely]] {
690-
log_regex_error("pcre2_substitute error", regex_info.replace_count);
676+
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
677+
if (regex_error_to_buffer(buffer, regex_info.replace_count)) {
678+
kphp::log::warning("pcre2_substitute error: {}", buffer.data());
679+
}
691680
return false;
692681
}
693682
} else { // replace only 'limit' times
@@ -700,7 +689,10 @@ bool replace_regex(RegexInfo& regex_info, uint64_t limit) noexcept {
700689
for (; regex_info.replace_count < limit; ++regex_info.replace_count) {
701690
auto expected_opt_match_view{pcre2_matcher.next()};
702691
if (!expected_opt_match_view.has_value()) [[unlikely]] {
703-
log_regex_error("can't replace by pcre2 regex due to match error", expected_opt_match_view.error());
692+
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
693+
if (regex_error_to_buffer(buffer, expected_opt_match_view.error())) {
694+
kphp::log::warning("can't replace by pcre2 regex due to match error: {}", buffer.data());
695+
}
704696
return false;
705697
}
706698
auto opt_match_view{*expected_opt_match_view};
@@ -723,7 +715,10 @@ bool replace_regex(RegexInfo& regex_info, uint64_t limit) noexcept {
723715
regex_info.replace_options, nullptr, regex_state.match_context.get(), reinterpret_cast<PCRE2_SPTR8>(regex_info.replacement.data()),
724716
regex_info.replacement.size(), reinterpret_cast<PCRE2_UCHAR8*>(runtime_ctx.static_SB.buffer()), std::addressof(length_after_replace))};
725717
replace_one_ret_code != 1) [[unlikely]] {
726-
log_regex_error("pcre2_substitute error", replace_one_ret_code);
718+
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
719+
if (regex_error_to_buffer(buffer, replace_one_ret_code)) {
720+
kphp::log::warning("pcre2_substitute error: {}", buffer.data());
721+
}
727722
return false;
728723
}
729724

@@ -760,7 +755,10 @@ std::optional<array<mixed>> split_regex(RegexInfo& regex_info, int64_t limit, bo
760755
for (size_t out_parts_count{1}; limit == kphp::regex::PREG_NOLIMIT || out_parts_count < limit;) {
761756
auto expected_opt_match_view{pcre2_matcher.next()};
762757
if (!expected_opt_match_view.has_value()) [[unlikely]] {
763-
log_regex_error("can't split by pcre2 regex due to match error", expected_opt_match_view.error());
758+
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
759+
if (regex_error_to_buffer(buffer, expected_opt_match_view.error())) {
760+
kphp::log::warning("can't split by pcre2 regex due to match error: {}", buffer.data());
761+
}
764762
return std::nullopt;
765763
}
766764
auto opt_match_view{*expected_opt_match_view};
@@ -862,7 +860,10 @@ Optional<int64_t> f$preg_match(const string& pattern, const string& subject, Opt
862860

863861
auto expected_opt_match_view{matcher{regex_info, static_cast<size_t>(offset)}.next()};
864862
if (!expected_opt_match_view.has_value()) [[unlikely]] {
865-
log_regex_error("can't match by pcre2 regex due to error", expected_opt_match_view.error());
863+
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
864+
if (regex_error_to_buffer(buffer, expected_opt_match_view.error())) {
865+
kphp::log::warning("can't match by pcre2 regex due to error: {}", buffer.data());
866+
}
866867
return false;
867868
}
868869
auto opt_match_view{*expected_opt_match_view};
@@ -932,7 +933,10 @@ Optional<int64_t> f$preg_match_all(const string& pattern, const string& subject,
932933
expected_opt_match_view = pcre2_matcher.next();
933934
}
934935
if (!expected_opt_match_view.has_value()) [[unlikely]] {
935-
log_regex_error("can't find all matches due to match error", expected_opt_match_view.error());
936+
std::array<char, ERROR_BUFFER_LENGTH> buffer{};
937+
if (regex_error_to_buffer(buffer, expected_opt_match_view.error())) {
938+
kphp::log::warning("can't find all matches due to match error: {}", buffer.data());
939+
}
936940
return false;
937941
}
938942

0 commit comments

Comments
 (0)