Repository navigation
Fix out-of-bounds reads and writes when indexing a zero-dimensional array with an index array - #47
Conversation
|
/opencode review |
|
@Watson1978 I have updated the opencode-review workflow, so could you please rebase your branch onto main? |
…al array An index into a zero-dimensional array is past its last dimension, so the aref functions took it for a new axis. They stepped by the element size and ignored the index array, so each extra index read or wrote one element further, outside the zero-dimensional array. Treat an axis as new only when it has no index array. In the view path, start with a zero stride so that the index array takes the "index <- step" branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
af1722b to
a019d1b
Compare
|
Rebased |
|
/opencode review |
PR Review: Fix out-of-bounds reads/writes when indexing a 0-d array with an index arrayVerdictThe fix is correct and minimal. I traced the full indexing pipeline (parse → Why the fix is correctRoot cause confirmed. For a 0-d array, every parsed index arg gets The
I also checked The Memory ownership is preserved. Each array-index branch sets Assignment path is covered. Minor suggestions
The PR description's ASAN evidence, the "tests fail before the fix" property, and the reference to the equivalent Cumo fix all check out against the code. Approve. |
Add Int32 and a Ruby Array index to the assignment test, and slice and an out-of-range index to the read test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. I addressed the minor suggestions.
I left |
Purpose
Indexing a zero-dimensional array with an index array reads or writes past its buffer. The values in the index array are not used, and each extra index goes one element further.
Views (
x.view[...]), Ruby Array indexes (x[[0, 0, 0]]),Numo::BitandNumo::RObjecttake the same path.Summary of Changes
An index into a zero-dimensional array has
orig_dim == ndim == 0.na_index_aref_nadataandna_index_aref_naviewuseorig_dim >= ndimto find a:newaxis, so they took this index for a new axis. They stepped by the element size and ignored the index array.:newnever has one, so new axes work as before.na_index_aref_naview,sdx1started as all zeros, whichSDX_IS_INDEXreads as an index with a NULL pointer. It now starts as a zero stride, so the index array takes the "index <- step" branch and every index points to the single element.na_index_aref_nadataalready starts with a zero stride.Cumo fixed the same in sonots/cumo#661.
Testing
With Ruby 4.0.9 built with AddressSanitizer, the code before the fix reports a heap-buffer-overflow in
iter_dfloat_to_afor the read and initer_dfloat_store_dfloatfor the write. The fixed code reports nothing for these cases or for the test suites. 38 other indexing results, such as:newaxes, index arrays on 1-D and 2-D arrays, out-of-range indexes and writes, are the same before and after the fix.The new tests fail on the code before the fix.
test_narray,test_bit,test_extra,test_mathandtest_ractorall pass on Ruby 4.0.10.Related Issues
Relates to sonots/cumo#661.
🤖 Generated with Claude Code