Fix GH-16998: UBSAN warning in rfc1867 #17000
Closed
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.
The "else branch" of
next_line
can reset thebuf_begin
field to NULL, causing the next invocation to pass NULL tomemchr
with a 0 length. When UBSAN is enabled this causes an UBSAN abort. Real world impact is likely none because of the 0 length.To fix this, don't set the pointer to NULL, which means that the
memchr
will return NULL and sinceself->bytes_in_buffer < self->bufsize
we return NULL and request more data throughfill_buffer
. That function will resetbuf_begin
andbytes_in_buffer
so that the next invocation works fine.I chose this solution so we have an invariant that
buf_begin
is never NULL, which makes reasoning easier. An alternative solution is keeping the NULLing ofbuf_begin
and add an extra check at the top ofnext_line
, but I didn't like special casing this.