Skip to content

Commit

Permalink
#58 better error message for Oniguruma's "retry-limit-in-match over"
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Jun 1, 2019
1 parent cde2d12 commit 41c13c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions oniguruma/oniguruma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
21 changes: 19 additions & 2 deletions oniguruma/oniguruma_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
1 change: 1 addition & 0 deletions oniguruma/oniguruma_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 41c13c3

Please sign in to comment.