Raise ArgumentError if IO.read is provided negative offset
Fixes [Bug #19380]
This commit is contained in:
parent
836e9a192b
commit
6c60006de5
Notes:
git
2023-03-24 19:29:23 +00:00
4
io.c
4
io.c
@ -12125,9 +12125,13 @@ static VALUE
|
|||||||
rb_io_s_read(int argc, VALUE *argv, VALUE io)
|
rb_io_s_read(int argc, VALUE *argv, VALUE io)
|
||||||
{
|
{
|
||||||
VALUE opt, offset;
|
VALUE opt, offset;
|
||||||
|
long off;
|
||||||
struct foreach_arg arg;
|
struct foreach_arg arg;
|
||||||
|
|
||||||
argc = rb_scan_args(argc, argv, "13:", NULL, NULL, &offset, NULL, &opt);
|
argc = rb_scan_args(argc, argv, "13:", NULL, NULL, &offset, NULL, &opt);
|
||||||
|
if (!NIL_P(offset) && (off = NUM2LONG(offset)) < 0) {
|
||||||
|
rb_raise(rb_eArgError, "negative offset %ld given", off);
|
||||||
|
}
|
||||||
open_key_args(io, argc, argv, opt, &arg);
|
open_key_args(io, argc, argv, opt, &arg);
|
||||||
if (NIL_P(arg.io)) return Qnil;
|
if (NIL_P(arg.io)) return Qnil;
|
||||||
if (!NIL_P(offset)) {
|
if (!NIL_P(offset)) {
|
||||||
|
@ -128,9 +128,18 @@ describe "IO.read" do
|
|||||||
-> { IO.read @fname, -1 }.should raise_error(ArgumentError)
|
-> { IO.read @fname, -1 }.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an Errno::EINVAL when not passed a valid offset" do
|
ruby_version_is ''...'3.3' do
|
||||||
-> { IO.read @fname, 0, -1 }.should raise_error(Errno::EINVAL)
|
it "raises an Errno::EINVAL when not passed a valid offset" do
|
||||||
-> { IO.read @fname, -1, -1 }.should raise_error(Errno::EINVAL)
|
-> { IO.read @fname, 0, -1 }.should raise_error(Errno::EINVAL)
|
||||||
|
-> { IO.read @fname, -1, -1 }.should raise_error(Errno::EINVAL)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is '3.3' do
|
||||||
|
it "raises an ArgumentError when not passed a valid offset" do
|
||||||
|
-> { IO.read @fname, 0, -1 }.should raise_error(ArgumentError)
|
||||||
|
-> { IO.read @fname, -1, -1 }.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uses the external encoding specified via the :external_encoding option" do
|
it "uses the external encoding specified via the :external_encoding option" do
|
||||||
|
@ -2866,6 +2866,9 @@ class TestIO < Test::Unit::TestCase
|
|||||||
assert_equal("foo\nbar\nbaz\n", File.read(t.path))
|
assert_equal("foo\nbar\nbaz\n", File.read(t.path))
|
||||||
assert_equal("foo\nba", File.read(t.path, 6))
|
assert_equal("foo\nba", File.read(t.path, 6))
|
||||||
assert_equal("bar\n", File.read(t.path, 4, 4))
|
assert_equal("bar\n", File.read(t.path, 4, 4))
|
||||||
|
|
||||||
|
assert_raise(ArgumentError) { File.read(t.path, -1) }
|
||||||
|
assert_raise(ArgumentError) { File.read(t.path, 1, -1) }
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user