Repository navigation
♻️ refactor(c): overflow-safe shared vec.h buffer growth - #511
Merged
Merged
Conversation
Every growable buffer in the C core doubled its capacity and multiplied it by the element size with no bound check. th_grow_cap centralizes that arithmetic and bounds both the doubling and the byte-size multiply, so a length crafted to wrap size_t fails before it allocates instead of underallocating (the libxml2 CVE-2022-29824 class). No real append can reach the overflow arms, so the _grow_probe hook drives the guard and a test covers them. Part of tox-dev#478.
Replace 43 hand-rolled doubling loops across the tokenizer, tree builder, serializers, and the CSS and JavaScript minifier engines with a th_grow_cap call plus the site's own realloc. Output stays byte-identical for every normal input; only a size that would wrap size_t now stops the growth. th_grow_cap does arithmetic alone, so each site keeps its own allocator and the standalone css/js builds compile unchanged. Part of tox-dev#478.
gaborbernat
enabled auto-merge (squash)
July 5, 2026 09:04
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Every growable buffer in the C core (the tokenizer, the tree builder, the six serializers, and the standalone CSS and JavaScript minifier engines) kept its own copy of the same doubling loop: start at an initial capacity, double until the requested length fits, multiply by the element size, and realloc. Forty-three sites duplicated that arithmetic. This routes all of them through one
static inlinehelper,th_grow_capincore/vec.h. Each site keeps its own allocator and failure handling, so the standalone css/js builds compile unchanged and the hot append path stays as it was.The helper also closes a size-overflow gap (security task S4). The old loops multiplied the doubled capacity by the element size with no bound check, so a length crafted to wrap
size_tcould underallocate and let a later write run past the buffer, the class of libxml2's CVE-2022-29824.th_grow_capbounds both the doubling step and thecapacity * element_sizeproduct, so such a length fails before it allocates. No real append can reach those overflow arms, so a_grow_probehook drives the guard and a test covers them. Behavior stays byte-identical for every ordinary input.Coverage holds at 100% line and branch on both llvm-cov and the CI gcc-13 gate, which I reproduced in an ubuntu-24.04 container. The -Werror plus clang-tidy build, the type and docs checks, and the fuzz-smoke ASan gate (including the standalone js and idna harnesses) all pass. Part of #478.