From 41c13c3a989b819a034eb04d58ad57233ce1ea1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20St=C3=A4ber?= Date: Sun, 2 Jun 2019 00:15:14 +0200 Subject: [PATCH] #58 better error message for Oniguruma's "retry-limit-in-match over" --- oniguruma/oniguruma.go | 3 +++ oniguruma/oniguruma_helper.c | 21 +++++++++++++++++++-- oniguruma/oniguruma_helper.h | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/oniguruma/oniguruma.go b/oniguruma/oniguruma.go index 6ccc1016..7abdb15a 100644 --- a/oniguruma/oniguruma.go +++ b/oniguruma/oniguruma.go @@ -124,6 +124,9 @@ func (regex *Regex) searchWithOffset(input string, offset int) (*SearchResult, e }, nil } else if r < 0 { C.onig_region_free(region, 1) + if C.oniguruma_helper_is_retry_limit_error(r) != 0 { + return nil, fmt.Errorf("the match takes too long to process: the oniguruma regular expression library aborted the match with error: %v", errMsg(r)) + } return nil, errors.New(errMsg(r)) } else { return &SearchResult{ diff --git a/oniguruma/oniguruma_helper.c b/oniguruma/oniguruma_helper.c index ba7e0f10..512ada65 100644 --- a/oniguruma/oniguruma_helper.c +++ b/oniguruma/oniguruma_helper.c @@ -18,8 +18,17 @@ // This initialization wrapper is to be compatible with both, Onituruma 5.9.6 and Oniguruma 6.0.0. int oniguruma_helper_initialize(OnigEncoding encodings[], int n) { - #if ONIGURUMA_VERSION_MAJOR == 6 - return onig_initialize(encodings, n); + #if ONIGURUMA_VERSION_MAJOR >= 6 + int result = onig_initialize(encodings, n); + #if ONIGURUMA_VERSION_MINOR >= 8 && ONIGURUMA_VERSION_TEENY >= 2 + // In order to avoid retry limit errors in the examples attached to issue #58 + // we would need to increase the limit by a factor of 100. However, this results + // in unreasonably long processing times for the affected match. + // As a trade-off, we increase the default by a factor of 10. + // See here for documentation: https://github.com/kkos/oniguruma/issues/143 + onig_set_retry_limit_in_match(10L*onig_get_retry_limit_in_match()); + #endif + return result; #else return 0; #endif @@ -35,3 +44,11 @@ int oniguruma_helper_error_code_with_info_to_str(UChar* err_buf, int err_code, O int oniguruma_helper_error_code_to_str(UChar* err_buf, int err_code) { return onig_error_code_to_str(err_buf, err_code); } + +int oniguruma_helper_is_retry_limit_error(int err_code) { + #ifdef ONIGERR_RETRY_LIMIT_IN_MATCH_OVER + return err_code == ONIGERR_RETRY_LIMIT_IN_MATCH_OVER; + #else + return 0; + #endif +} \ No newline at end of file diff --git a/oniguruma/oniguruma_helper.h b/oniguruma/oniguruma_helper.h index 9ddc532d..6c33cbdd 100644 --- a/oniguruma/oniguruma_helper.h +++ b/oniguruma/oniguruma_helper.h @@ -17,3 +17,4 @@ extern int oniguruma_helper_initialize(OnigEncoding encodings[], int n); extern int oniguruma_helper_error_code_with_info_to_str(UChar* err_buf, int err_code, OnigErrorInfo *errInfo); extern int oniguruma_helper_error_code_to_str(UChar* err_buf, int err_code); +extern int oniguruma_helper_is_retry_limit_error(int err_code);