Bound the leading-zero skip in pm_integer_parse - #4193
Merged
Conversation
The PM_INTEGER_BASE_DEFAULT branch skips leading zeros without checking
`end`, so it reads one byte past the range for an all-zero input.
pm_float_node_rational_create() reaches it: it allocates `length` bytes
for the mantissa, writes only `length - 1` of them because the decimal
point is squeezed out, and passes end = digits + length - 1. Parsing
`+0.0r` then walks the skip onto the byte that was never written.
$ valgrind ./parse +0.0r
Conditional jump or move depends on uninitialised value(s)
at pm_integer_parse (integer.c:506)
by pm_float_node_rational_create (prism.c:3963)
by parse_expression_prefix (prism.c:17898)
The value parsed is unaffected, since `start >= end` returns 0 either
way. The other bases already guard with `(end - start) > 1`.
Found by an mruby fuzzer testcase.
kddnewton
approved these changes
Aug 1, 2026
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.
Problem
pm_integer_parse()skips leading zeros in thePM_INTEGER_BASE_DEFAULTbranch without checkingend:For an all-zero input this reads one byte past the range. The
if (start >= end) return;below it comes too late; the loop has already dereferenced the byte atend.pm_float_node_rational_create()reaches it. It allocateslengthbytes for the mantissa, writes onlylength - 1of them because the decimal point is squeezed out, and passesend = digits + length - 1. So the byte the skip lands on is not merely one-past-the-end, it is uninitialized heap:A leading
+is needed to make the mantissa buffer long enough for the skip to reach the uninitialized byte, so+0.0rreports while0.0rdoes not.Fix
Bound the loop, matching what
PM_INTEGER_BASE_DECIMALandPM_INTEGER_BASE_UNKNOWNalready do with(end - start) > 1.The parsed value does not change:
start >= endreturns 0 either way. I confirmed the same harness reports the read before the patch and is clean after, withvalue=0in both cases.Found by an mruby fuzzer testcase.