From e29b49e3b14eee50bb5ff310409393e1e7e7e147 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sat, 27 Mar 2021 14:45:10 +0900 Subject: [PATCH 1/2] Add test for raising TypeError instead of segfault --- test/pathname/test_pathname.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 46a04ee..8a0f3cb 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -1067,6 +1067,21 @@ def test_expand_path def test_split assert_equal([Pathname("dirname"), Pathname("basename")], Pathname("dirname/basename").split) + + assert_separately([], <<-'end;') + require 'pathname' + + mod = Module.new do + def split(_arg) + end + end + + File.singleton_class.prepend(mod) + + assert_raise(TypeError) do + Pathname('/').split + end + end; end def test_blockdev? From 1db7479a74c237086dc65e4aec340bd26f802c6e Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Sat, 27 Mar 2021 15:13:59 +0900 Subject: [PATCH 2/2] Fix segfault of Pathname#split --- ext/pathname/pathname.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index 55577d7..1d4ed28 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -834,7 +834,7 @@ path_split(VALUE self) VALUE str = get_strpath(self); VALUE ary, dirname, basename; ary = rb_funcall(rb_cFile, id_split, 1, str); - ary = rb_check_array_type(ary); + Check_Type(ary, T_ARRAY); dirname = rb_ary_entry(ary, 0); basename = rb_ary_entry(ary, 1); dirname = rb_class_new_instance(1, &dirname, rb_obj_class(self));