Skip to content

♻️ refactor(c): overflow-safe shared vec.h buffer growth - #511

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:refactor/core-dedup
Jul 5, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:refactor/core-dedup

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

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 inline helper, th_grow_cap in core/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_t could underallocate and let a later write run past the buffer, the class of libxml2's CVE-2022-29824. th_grow_cap bounds both the doubling step and the capacity * element_size product, so such a length fails before it allocates. No real append can reach those overflow arms, so a _grow_probe hook 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.

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 gaborbernat added the enhancement New feature or request label Jul 5, 2026
@gaborbernat
gaborbernat enabled auto-merge (squash) July 5, 2026 09:04
@codspeed

codspeed Bot commented Jul 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 58 untouched benchmarks
⏩ 18 skipped benchmarks1


Comparing gaborbernat:refactor/core-dedup (b558ea9) with main (2b838d5)

Open in CodSpeed

Footnotes

  1. 18 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 75cadc6 into tox-dev:main Jul 5, 2026
48 checks passed
@gaborbernat
gaborbernat deleted the refactor/core-dedup branch July 10, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant