Skip to content

Add buffer-size macros and fix d2fixed/d2exp heap overflow - #258

Open
mbouaziz wants to merge 8 commits into
ulfjack:masterfrom
mbouaziz:vibe/security-buffer-bounds-msvc-fix-d4c96c
Open

mbouaziz wants to merge 8 commits into
ulfjack:masterfrom
mbouaziz:vibe/security-buffer-bounds-msvc-fix-d4c96c

Conversation

@mbouaziz

Copy link
Copy Markdown

Summary

Adds documented buffer-size macros for the *_buffered functions and fixes the heap overflow in d2fixed()/d2exp() (#197). Split into small independent commits — refactorings first, tests before fixes:

  1. define RYU_F2S_BUFFER_SIZE (16) and use it in f2s() — pure refactoring, replaces the magic 16.
  2. define RYU_D2S_BUFFER_SIZE (25) and use it in d2s() — pure refactoring, replaces the magic 25.
  3. test: add buffer-bounds tests for the *_buffered functions — canary bounds checks over special values and random bit patterns, plus d2fixed/d2exp cross-checked against snprintf. Compiles and passes on the current code (verified: 6/6).
  4. test: add malloc-variant tests reproducing the d2fixed/d2exp overflow — d2fixed(1.5, 100000) / d2exp(1.5, 100000) smash the old fixed 2000-byte allocation (Buffer overflow possible with d2fixed() and friends #197); this test segfaults until commits 6–7 land.
  5. check malloc return value in d2s/f2s/d2fixed/d2exp — return NULL on allocation failure.
  6. add RYU_D2FIXED_BUFFER_SIZE and fix d2fixed heap overflow (#197) — allocation scales with precision; base size is the named macro RYU_D2FIXED_BASE_SIZE (312), shared by the size macro and the size_t overflow guard. Adds <stddef.h>, first needed here for size_t.
  7. add RYU_D2EXP_BUFFER_SIZE and fix d2exp heap overflow (#197) — same for d2exp, base macro RYU_D2EXP_BASE_SIZE (11).

The malloc assertions use ASSERT_NE(static_cast<char*>(nullptr), ...) so the test also compiles on MSVC, where streaming a bare nullptr_t in a gtest failure message is ambiguous (error C2593).

Verification

  • Every commit compiles. Commit 3's tests pass pre-fix (6/6); commit 4's malloc test segfaults pre-fix (exit 139), reproducing Buffer overflow possible with d2fixed() and friends #197.
  • The head passes make -C ryu test (gcc 14.2): 8/8 test binaries, api_bounds_test 7/7; all five presubmit --copt variants (-DRYU_OPTIMIZE_SIZE, -DRYU_ONLY_64_BIT_OPS, both combined, -DRYU_FLOAT_FULL_TABLE, -DRYU_ONLY_64_BIT_OPS -DRYU_32_BIT_PLATFORM) also pass.

Closes #197
Closes #149
Closes #183

Co-authored-by: Mistral Vibe vibe@mistral.ai

mistral-vibe and others added 8 commits November 16, 2026 14:38
Pure refactoring: replaces the magic 16 in f2s()'s malloc with a
documented macro in ryu.h.

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
Pure refactoring: replaces the magic 25 in d2s()'s malloc with a
documented macro in ryu.h.

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
Canary-based bounds checks for d2s_buffered/f2s_buffered/
d2fixed_buffered/d2exp_buffered over special values and random bit
patterns, plus d2fixed/d2exp output cross-checked against snprintf.
d2s/f2s sizes use the new RYU_*_BUFFER_SIZE macros; the d2fixed/d2exp
sizes are literals until their macros land with the heap-overflow fix.
Compiles and passes on the current code.

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
MallocVariantsLargePrecision calls d2fixed(1.5, 100000) and
d2exp(1.5, 100000): with the current fixed 2000-byte allocation these
write ~100k bytes past the buffer (issue ulfjack#197), so this test fails
(heap corruption / crash) until the allocation is fixed to scale
with the precision. The assertions cast nullptr to char* because
streaming a bare nullptr_t in a gtest failure message is ambiguous
on MSVC (error C2593).

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
Return NULL instead of writing through a failed allocation.

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
d2fixed() allocated a fixed 2000 bytes, overflowing the heap for
precision > ~1688. The allocation now scales with the requested
precision via RYU_D2FIXED_BUFFER_SIZE, with a size_t overflow guard.
The base size (309 integer digits + optional sign + decimal point +
NUL) is the named macro RYU_D2FIXED_BASE_SIZE (312), shared by the
size macro and the guard. <stddef.h> is included here because the
macro is the first to use size_t. The d2fixed canary/snprintf tests
now use the macro.

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
d2exp() had the same fixed 2000-byte allocation bug as d2fixed(). The
allocation now scales with the requested precision via
RYU_D2EXP_BUFFER_SIZE, with a size_t overflow guard. The base size
(one digit + decimal point + e±XXX exponent + sign + NUL, plus up to
11 bytes for special values like "-nan(snan)" on MSVC) is the named
macro RYU_D2EXP_BASE_SIZE (11), shared by the size macro and the
guard. The d2exp canary/snprintf tests now use the macro.

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
Adds a "Buffer sizes" section next to the test instructions, covering
the macros introduced above and the malloc-based functions' contract
(appropriate-size allocation, NULL on failure).

Co-authored-by: Mistral Vibe <vibe@mistral.ai>

Co-authored-by: mbouaziz <mbouaziz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants