Repository navigation
Fix crashes in integer /, %, << and >> on empty arrays - #51
Open
Watson1978 wants to merge 3 commits into
Open
Watson1978 wants to merge 3 commits into
Watson1978 wants to merge 3 commits into
Conversation
…isor The / and % iterators of the integer types and Numo::RObject check the divisor for zero before their loop when its step is 0. That is the case for a scalar divisor and also for an empty array, whose data pointer is NULL, so Numo::Int32[] / Numo::Int32[] read through a NULL pointer. Check the divisor only when there is an element. An empty integer array divided by 0 now returns an empty array instead of raising ZeroDivisionError, as no division takes place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The << and >> iterators of the integer types and Numo::RObject read the shift amount before their loop when its step is 0, which is also the case for an empty array. Numo::Int32[] << Numo::Int32[] read through a NULL pointer. Take that branch only when there is an element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Watson1978
force-pushed
the
fix-int-div-empty
branch
from
December 2, 2026 14:31
f447ab6 to
11640b6
Compare
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.
Purpose
/,%,<<and>>crash the interpreter when both operands are empty integer arrays. All integer types andNumo::RObjectcrash the same way.Summary of Changes
When the second operand has a step of 0, these iterators read it once before their loop:
/and%to check it for zero, and<<and>>to keep the shift amount. A step of 0 is used for a scalar, and also for an empty array, which has no data. So the read went through a NULL pointer. The read now happens only when there is an element. Floating-point/and%do not crash on empty arrays.This changes one error. An empty array divided by 0, such as
Numo::Int32[] / 0orNumo::Int32.new(0, 2) % 0, raisedZeroDivisionErrorand now returns an empty array, as no division takes place. ForNumo::RObjectthis applies tofalseinstead of 0, because its zero check matchesfalse. Cumo gives the same results for these cases, and a non-empty array divided by 0 still raisesZeroDivisionError.Testing
I compared these operators before and after the fix for the integer types,
Numo::RObjectandNumo::DFloat, including non-empty arrays, broadcasting, strided and transposed views, in-place operations and division by 0. Only the empty arrays divided by 0 orfalsedescribed above changed. With Ruby 4.0.9 built with AddressSanitizer, the cases that crashed report nothing.The new test crashes the test process before the fix.
test_narray,test_bit,test_extra,test_mathandtest_ractorall pass on Ruby 4.0.10.Related Issues
None.
🤖 Generated with Claude Code