loop_narray and the buffer copies run their body once before they
check any count. Most iterators with NDF_HAS_LOOP then loop zero
times, but the iterators of eye, poly and bincount with weights are
registered with NO_LOOP and do not look at a count. With an empty
outer axis they accessed a NULL pointer, as in
Numo::DFloat.new(0, 2, 2).eye and Numo::DFloat[].poly(1, 2), or wrote
into the base of an empty view. The buffer copies, used for views that
an iterator cannot step through, copied one element of an empty view.
They read an empty index array past its end and wrote past an empty
buffer, as in Numo::DFloat.new(2, 3).seq[[], true].eye.
Return from loop_narray before the loop when the ndfunc has no loop of
its own and an outer axis is empty. Skip the copies for a buffer with
an empty axis, including one folded into the element size. Check the
bincount weight length before the loop as well, so that a weight with
an empty outer axis still raises ShapeError.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Purpose
eye,polyandbincountwith weights crash the interpreter when an axis is empty. All numeric types crash the same way, andNumo::RObjecttoo foreyeandpoly.On an empty view,
eyecan also write into the base array without an error.Summary of Changes
loop_narrayand the buffer copies inndloop.crun their body once before they check any count.NDF_HAS_LOOPthen loop zero times, but the iterators of these three methods are registered withNO_LOOP. They do not look at a count, so they read and wrote through the pointer of an empty array.loop_narraynow returns before the loop when the ndfunc has noNDF_HAS_LOOPand an outer axis is empty.Numo::DFloat.new(2, 3).seq[[], [0, 1, 2]] + 1.bincountchecked that the weight has the same length asselfinside its iterator, which is no longer called for an empty outer axis. The check now runs before the loop, soNumo::UInt32[0, 1, 2].bincount(Numo::DFloat.new(0, 5))still raisesShapeError.Integer and
Numo::RObject/and%read the divisor before their loop, soNumo::Int32[] / Numo::Int32[]still crashes. That needs a fix in those iterators and is not part of this change.Cumo avoids the same crashes with a guard for every loop in sonots/cumo#513. That PR also keeps the shape of empty arrays through ndloop, which changes the results of other operations, so this change does not take that part.
Testing
With Ruby 4.0.9 built with AddressSanitizer, the fixed code reports nothing for these cases on 8 dtypes or for the test suites. 128 other results of
eyeare the same before and after the fix. They cover non-empty arrays, views, diagonal offsets, empty arrays that did not crash, and the errors for invalid arguments and frozen arrays. I also called about 140 other methods on 6 dtypes and 6 empty arrays and views, and the results are the same before and after the fix. 36 calls ofbincountwith valid and invalid weights give the same results and errors before and after the fix.Each 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
Relates to sonots/cumo#513.
🤖 Generated with Claude Code