Fix parser out-of-bounds reads - #131
Open
laurikari wants to merge 5 commits into
Open
Conversation
dag-erling
requested changes
Jan 31, 2026
Collaborator
|
ping @laurikari |
By passing exact-length unpadded inputs to regncomp, we can catch out-of-bounds reads using -fsanitize=address.
Many tools expect source files to have some valid encoding (like UTF-8), this makes them happy.
These were all found with the latest retest.c using the Address Sanitizer with: CFLAGS=-O1 -g -fsanitize=address -fno-omit-frame-pointer LDFLAGS=-fsanitize=address
dag-erling
reviewed
May 15, 2026
| buf = xmalloc(len * sizeof(CHAR_T)); | ||
| if (buf == NULL) | ||
| return REG_ESPACE; | ||
| memcpy(buf, data, len * sizeof(CHAR_T)); |
Collaborator
There was a problem hiding this comment.
This is likely to still be null-terminated at least some of the time. It would be better to allocate a slightly larger buffer and deliberately set the first out-of-bounds character to something non-zero.
Collaborator
There was a problem hiding this comment.
static int
wrap_regcomp(regex_t *preg, const CHAR_T *data, size_t len, int cflags)
{
#ifdef HAVE_REGNCOMP
CHAR_T *buf;
int ret;
if (use_regncomp)
{
buf = xmalloc((len + 1) * sizeof(CHAR_T));
if (buf == NULL)
return REG_ESPACE;
memcpy(buf, data, len * sizeof(CHAR_T));
buf[len] = (CHAR_T)~0;
ret = tre_regncomp(preg, buf, len, cflags);
xfree(buf);
return ret;
}
#endif /* HAVE_REGNCOMP */
return tre_regcomp(preg, data, cflags);
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Updated
retestsoregncompis called with a strictly non‑padded input buffer, rather than passing aNULterminated buffer. With that in place, AddressSanitizer exposed several out‑of‑bounds reads in the parser.Added a few additional parser test cases around atoms, bracket expressions, and escapes.
Then fixed the parser, adding explicit bounds checks and better error handling.