Skip to content

Commit

Permalink
Use assertion to ensure erroroffset return from pcre2_compile is with…
Browse files Browse the repository at this point in the history
…in bounds (#460)

When testing a patch for PCRE2, I found that due to a bug in my code,
`pcre2_compile()` could return a totally invalid error offset. In case
something similar ever happens again, I've added an assertion which will
make it easier to notice the problem.

It should be noted that the pcre2api manpage states: "Some errors are
not detected until the whole pattern has been scanned; in these cases,
the offset passed back is the length of the pattern." Since patterns are
not always null-terminated, this means that `pattern + erroroffset` may
sometimes point to uninitialized (or even unmapped) memory. However,
it is still worthwhile to guard against other unexpected values being
returned in `erroroffset`.
  • Loading branch information
alexdowad authored Sep 4, 2024
1 parent 06a49d8 commit 317f339
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/pcre2_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -11201,6 +11201,8 @@ an offset is available in the parsed pattern. */
ptr = pattern + cb.erroroffset;

HAD_EARLY_ERROR:
PCRE2_ASSERT(ptr >= pattern); /* Ensure we don't return invalid erroroffset */
PCRE2_ASSERT(ptr <= (pattern + patlen));
*erroroffset = ptr - pattern;

HAD_ERROR:
Expand Down

0 comments on commit 317f339

Please sign in to comment.