Repository navigation
Conversation
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>
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.
Summary
Adds documented buffer-size macros for the
*_bufferedfunctions and fixes the heap overflow ind2fixed()/d2exp()(#197). Split into small independent commits — refactorings first, tests before fixes:define RYU_F2S_BUFFER_SIZE (16) and use it in f2s()— pure refactoring, replaces the magic16.define RYU_D2S_BUFFER_SIZE (25) and use it in d2s()— pure refactoring, replaces the magic25.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).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.check malloc return value in d2s/f2s/d2fixed/d2exp— return NULL on allocation failure.add RYU_D2FIXED_BUFFER_SIZE and fix d2fixed heap overflow (#197)— allocation scales with precision; base size is the named macroRYU_D2FIXED_BASE_SIZE(312), shared by the size macro and thesize_toverflow guard. Adds<stddef.h>, first needed here forsize_t.add RYU_D2EXP_BUFFER_SIZE and fix d2exp heap overflow (#197)— same for d2exp, base macroRYU_D2EXP_BASE_SIZE(11).The malloc assertions use
ASSERT_NE(static_cast<char*>(nullptr), ...)so the test also compiles on MSVC, where streaming a barenullptr_tin a gtest failure message is ambiguous (error C2593).Verification
make -C ryu test(gcc 14.2): 8/8 test binaries,api_bounds_test7/7; all five presubmit--coptvariants (-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