Actions
Bug #22193
closedSegmentation fault in functions to check MemoryView contiguity with NULL shape and/or strides
Bug #22193:
Segmentation fault in functions to check MemoryView contiguity with NULL shape and/or strides
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-07-13T13:26:16Z origin/master cb2ea293c6) +PRISM [arm64-darwin25]
Description
Hello,
I encountered a segmentation fault when calling rb_memory_view_is_row_major_contiguous with a MemoryView whose shape and strides are NULL. For instance, a MemoryView initialized with rb_memory_view_init_as_byte_array function causes this problem. The cause is that the function handles shape and strides as arrays without checking whether they are NULL or not.
Environment¶
- Ruby commit: cb2ea293c6
- OS: macOS (M2 Mac)
Code to reproduce the problem:¶
I will send a pull request later for the full patch.
ext/-test-/memory_view/memory_view.c¶
static VALUE
memory_view_is_row_major_contiguous(VALUE mod, VALUE obj)
{
rb_memory_view_t view;
VALUE result;
if (!rb_memory_view_get(obj, &view, 0)) {
rb_raise(rb_eArgError, "Unable to get MemoryView");
}
result = rb_memory_view_is_row_major_contiguous(&view) ? Qtrue : Qfalse;
rb_memory_view_release(&view);
return result;
}
static VALUE
memory_view_is_column_major_contiguous(VALUE mod, VALUE obj)
{
rb_memory_view_t view;
VALUE result;
if (!rb_memory_view_get(obj, &view, 0)) {
rb_raise(rb_eArgError, "Unable to get MemoryView");
}
result = rb_memory_view_is_column_major_contiguous(&view) ? Qtrue : Qfalse;
rb_memory_view_release(&view);
return result;
}
// ...
void
Init_memory_view(void)
{
// ...
rb_define_module_function(mMemoryViewTestUtils, "is_row_major_contiguous", memory_view_is_row_major_contiguous, 1);
rb_define_module_function(mMemoryViewTestUtils, "is_column_major_contiguous", memory_view_is_column_major_contiguous, 1);
// ...
}
test/ruby/test_memory_view.rb¶
class TestMemoryView < Test::Unit::TestCase
# ...
def test_rb_memory_view_is_row_major_contiguous
es = MemoryViewTestUtils::ExportableString.new("ruby")
assert_true(MemoryViewTestUtils.is_row_major_contiguous(es))
end
def test_rb_memory_view_is_column_major_contiguous
es = MemoryViewTestUtils::ExportableString.new("ruby")
assert_true(MemoryViewTestUtils.is_column_major_contiguous(es))
end
# ...
end
Steps to reproduce the problem:¶
- Add code above to the current Ruby (cb2ea293c6)
- Run
make test-all TESTS=test/ruby/test_memory_view.rb - Then you can see segmentation fault
Thank you.
Updated by KitaitiMakoto (真 北市) 13 days ago
I sent a pull request to reproduce and fix the problem: https://github.com/ruby/ruby/pull/17851
Updated by KitaitiMakoto (真 北市) 12 days ago
- Subject changed from Segmentation fault in functions to MemoryView contiguity with NULL shape and/or strides to Segmentation fault in functions to check MemoryView contiguity with NULL shape and/or strides
Updated by mrkn (Kenta Murata) 4 days ago
- Status changed from Open to Closed
Actions