fix(cutils): handle vsnprintf encoding error in dbuf_printf#1482
Open
andreasrosdal wants to merge 1 commit into
Open
fix(cutils): handle vsnprintf encoding error in dbuf_printf#1482andreasrosdal wants to merge 1 commit into
andreasrosdal wants to merge 1 commit into
Conversation
When vsnprintf returns -1 (encoding error - e.g. an %ls conversion that hits an invalid multibyte sequence), dbuf_printf's slow path advanced s->size by len = -1, wrapping the size_t to ~SIZE_MAX. Subsequent dbuf operations then treated the buffer as essentially unlimited and indexed off the end. Bail out, flag the buffer as errored, and let callers propagate. No automated test: dbuf_printf is internal and reaching the vsnprintf encoding-error branch requires either an invalid multibyte input to %ls (locale-dependent and not portable) or a vsnprintf implementation that returns -1 for other reasons. Reviewed by inspection.
bnoordhuis
reviewed
May 17, 2026
Comment on lines
+849
to
+854
| if (len < 0) { | ||
| /* vsnprintf encoding error: don't let the caller wrap s->size by | ||
| advancing it by -1, which would underflow to near SIZE_MAX. */ | ||
| s->error = true; | ||
| return -1; | ||
| } |
Contributor
There was a problem hiding this comment.
Maybe this should just be an assert? I'm trying to think of a scenario where len < 0 could happen that is not a bug on our side, but I can't come up with anything.
Contributor
There was a problem hiding this comment.
Agreed, this is not exposed to user code to it must be an internal one, so let's assert on it.
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.
When vsnprintf returns -1 (encoding error - e.g. an %ls conversion that hits an invalid multibyte sequence), dbuf_printf's slow path advanced s->size by len = -1, wrapping the size_t to ~SIZE_MAX. Subsequent dbuf operations then treated the buffer as essentially unlimited and indexed off the end.
Bail out, flag the buffer as errored, and let callers propagate.
No automated test: dbuf_printf is internal and reaching the vsnprintf encoding-error branch requires either an invalid multibyte input to %ls (locale-dependent and not portable) or a vsnprintf implementation that returns -1 for other reasons. Reviewed by inspection.