From 6ef0eb29c6579db58e2cf3407b41d58c687ee742 Mon Sep 17 00:00:00 2001 From: Augustin Gottlieb <33221555+aguspe@users.noreply.github.com> Date: Fri, 23 Jan 2026 03:00:37 +0100 Subject: Add early type validation for ObjectSpace.memsize_of_all klass argument --- ext/objspace/objspace.c | 1 + test/objspace/test_objspace.rb | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c index 457ffc2789..1143e4801d 100644 --- a/ext/objspace/objspace.c +++ b/ext/objspace/objspace.c @@ -137,6 +137,7 @@ memsize_of_all_m(int argc, VALUE *argv, VALUE self) if (argc > 0) { rb_scan_args(argc, argv, "01", &data.klass); + if (!NIL_P(data.klass)) rb_obj_is_kind_of(Qnil, data.klass); } each_object_with_flags(total_i, &data); diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 78947a095b..8b369c3894 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -54,7 +54,11 @@ class TestObjSpace < Test::Unit::TestCase assert_operator(a, :>, b) assert_operator(a, :>, 0) assert_operator(b, :>, 0) - assert_raise(TypeError) {ObjectSpace.memsize_of_all('error')} + assert_kind_of(Integer, ObjectSpace.memsize_of_all(Enumerable)) + end + + def test_memsize_of_all_with_wrong_type + assert_raise(TypeError) { ObjectSpace.memsize_of_all(Object.new) } end def test_count_objects_size -- cgit v1.2.3