Skip to content

Guard print_string_ptr counters against 32-bit overflow (CVE-2026-16554) - #1117

Open
w4ffl35 wants to merge 1 commit into
DaveGamble:masterfrom
w4ffl35:fix/print-string-overflow
Open

w4ffl35 wants to merge 1 commit into
DaveGamble:masterfrom
w4ffl35:fix/print-string-overflow

Conversation

@w4ffl35

@w4ffl35 w4ffl35 commented Oct 26, 2026

Copy link
Copy Markdown

On 32-bit platforms size_t is 32 bits: escape_characters += 5 wraps past ~858M control characters (5 * 858993460 = 2^32 + 4), and the length sums can wrap too, so ensure() is asked for a small buffer while the write loop emits the true escaped length -- heap overflow, reachable via cJSON_PrintBuffered with a pre-allocated buffer.

This fails instead of wrapping at each accumulation step and before sizing the allocation, using (size_t)-1 so no new headers are needed. On 64-bit the guards are inert (verified: full suite green, escape-heavy output byte-exact).

Evidence:

  • Reproducer: 200KB control-char string prints byte-exact (1200002 chars); uint32_t simulation of the exact counter expressions proves the pre-fix wrap (counter lands on 4) and the post-fix guard trip. A 32-bit trigger needs ~859MB of control characters, not feasible on this 64-bit host -- the arithmetic proof plus inspection stands in for execution there. Full ctest suite passes (19/19).
  • Adds print_string_should_print_escape_heavy_strings_exactly to tests/print_string.c.

On 32-bit platforms size_t is 32 bits: escape_characters += 5 wraps
past ~858M control characters, and the length sums can wrap too, so
ensure() hands out a small buffer while the write loop emits the true
(giant) escaped length -- heap overflow, reachable via
cJSON_PrintBuffered with a pre-allocated buffer. Fail instead of
wrapping at each accumulation step and before sizing the allocation.

Add an escape-heavy exactness regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant