Fix integer overflow checks in enumerator#15829
Merged
nobu merged 1 commit intoruby:masterfrom Jan 9, 2026
Merged
Conversation
❌ 3/67208 Tests Failedtest/-ext-/stack/test_stack_overflow.rb#test_fiber_stack_overflowtest/-ext-/stack/test_stack_overflow.rb#test_overflowtest/-ext-/stack/test_stack_overflow.rb#test_thread_stack_overflow |
nobu
requested changes
Jan 9, 2026
Member
nobu
left a comment
There was a problem hiding this comment.
Please remove unrelated and unnecessary commits first.
regparse.c
Outdated
Comment on lines
+4789
to
+4793
| if ((size_t)i > (size_t)ONIGENC_CODE_TO_MBC_MAXLEN) { | ||
| r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING; | ||
| goto err; | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
Here i should equal ONIGENC_MBC_MINLEN(env->enc), this check is redundant.
nobu
reviewed
Jan 9, 2026
ext/-test-/stack/stack.c
Outdated
Comment on lines
+10
to
+19
| // Allocate and touch memory to force actual stack usage: | ||
| volatile char *stack = alloca(1024); | ||
| // Allocate and touch memory to force actual memory usage: | ||
| char *buf = (char *)malloc(1024); | ||
| if (!buf) { | ||
| break; | ||
| } | ||
| volatile char *stack = buf; | ||
| stack[0] = (char)i; | ||
| stack[1023] = (char)i; | ||
| i++; | ||
| free(buf); |
Member
There was a problem hiding this comment.
This is intentionally repeating alloca to cause a stack overflow, as the function name and the comment describe.
This change doesn't only break the purpose, falls into an infinite loop.
This was referenced Feb 6, 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.
fix this kind of problem you must avoid performing a signed addition that may overflow and then using its result in a comparison. Instead, reformulate the condition in terms of inequalities that do not overflow, or perform the addition in a wider or unsigned type where overflow is either impossible or well‑defined, and then convert back safely.
For this specific case, the intention of
if (i + unit < i) break;(forunit >= 0) is to stop the loop if incrementingibyunitwould overflow the signedlongrange. A safe, equivalent check is to compareiagainstLONG_MAX - unit: ifi > LONG_MAX - unit, theni + unitwould exceedLONG_MAXand overflow. Similarly, in theunit < 0branch, the intention ofif (i + unit > i) break;is to detect overflow when adding a negativeunit; the safe equivalent is to checki < LONG_MIN - unit(sinceunitis negative,LONG_MIN - unitis well‑defined and ≥LONG_MIN), meaning that addingunitwould go belowLONG_MIN.Concretely, in the first loop (for
unit >= 0) at line 4011, replaceif (i + unit < i) break;withif (i > LONG_MAX - unit) break;. In the second loop (forunit < 0) at line 4024, replaceif (i + unit > i) break;withif (i < LONG_MIN - unit) break;. These changes preserve existing behavior while avoiding undefined signed overflow. To useLONG_MAXandLONG_MIN, we need<limits.h>. Since we cannot assume it is already included and we’re allowed to add standard headers, add#include <limits.h>near the other#includes at the top of enumerator.c.References
comp.lang.c FAQ list · Question 3.19 (Preserving rules)
INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data
Integer Overflow in C/C++