Skip to content

Fix crashes in integer /, %, << and >> on empty arrays - #51

Open
Watson1978 wants to merge 3 commits into
yoshoku:mainfrom
Watson1978:fix-int-div-empty
Open

Watson1978 wants to merge 3 commits into
yoshoku:mainfrom
Watson1978:fix-int-div-empty

Conversation

@Watson1978

@Watson1978 Watson1978 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

/, %, << and >> crash the interpreter when both operands are empty integer arrays. All integer types and Numo::RObject crash the same way.

Numo::Int32[] / Numo::Int32[]                    # [BUG] Segmentation fault at 0x0000000000000000
Numo::Int32.new(2, 0) % Numo::Int32.new(2, 0)    # same
Numo::Int32[] << Numo::Int32[]                   # same
Numo::RObject[] >> Numo::RObject[]               # same

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[] / 0 or Numo::Int32.new(0, 2) % 0, raised ZeroDivisionError and now returns an empty array, as no division takes place. For Numo::RObject this applies to false instead of 0, because its zero check matches false. Cumo gives the same results for these cases, and a non-empty array divided by 0 still raises ZeroDivisionError.

Testing

  • Tests pass locally
  • Manual testing completed
Numo::Int32[] / Numo::Int32[]   # Numo::Int32#shape=[0](empty)
Numo::Int32[] << Numo::Int32[]  # Numo::Int32#shape=[0](empty)
Numo::Int32[] / 0               # Numo::Int32#shape=[0](empty)
Numo::Int32[7, 8, 9] / 0        # ZeroDivisionError

I compared these operators before and after the fix for the integer types, Numo::RObject and Numo::DFloat, including non-empty arrays, broadcasting, strided and transposed views, in-place operations and division by 0. Only the empty arrays divided by 0 or false described 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_math and test_ractor all pass on Ruby 4.0.10.

Related Issues

None.

🤖 Generated with Claude Code

Watson1978 and others added 3 commits December 2, 2026 20:44
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant