Raise TypeError for bad IO::Buffer.map argument (#8728)

* Raise TypeError when IO::Buffer.map argument is neither IO nor implements #fileno

* Use UNREACHABLE_CODE

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>

* Use macro for undef check

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>

---------

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
Charles Oliver Nutter 2023-10-20 23:30:40 -05:00 committed by GitHub
parent 0e62802c3b
commit 55d954721e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

9
io.c
View File

@ -2870,8 +2870,15 @@ rb_io_descriptor(VALUE io)
return fptr->fd;
}
else {
return RB_NUM2INT(rb_funcall(io, id_fileno, 0));
VALUE fileno = rb_check_funcall(io, id_fileno, 0, NULL);
if (!UNDEF_P(fileno)) {
return RB_NUM2INT(fileno);
}
}
rb_raise(rb_eTypeError, "expected IO or #fileno, %"PRIsVALUE" given", rb_obj_class(io));
UNREACHABLE_RETURN(-1);
}
int

View File

@ -80,7 +80,7 @@ class TestIOBuffer < Test::Unit::TestCase
end
def test_file_mapped_invalid
assert_raise NoMethodError do
assert_raise TypeError do
IO::Buffer.map("foobar")
end
end