Skip to content

Commit 7a953f4

Browse files
committed
fix: prevent segfault in StringIO#seek with SEEK_END on null device
1 parent 3c52ddc commit 7a953f4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

ext/stringio/stringio.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,11 @@ strio_seek(int argc, VALUE *argv, VALUE self)
837837
offset = ptr->pos;
838838
break;
839839
case 2:
840-
offset = RSTRING_LEN(ptr->string);
840+
if (NIL_P(ptr->string)) {
841+
offset = 0;
842+
} else {
843+
offset = RSTRING_LEN(ptr->string);
844+
}
841845
break;
842846
default:
843847
error_inval("invalid whence");

test/stringio/test_stringio.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ def test_null
7070
assert_nil io.getc
7171
end
7272

73+
def test_seek_null
74+
io = StringIO.new(nil)
75+
assert_equal(0, io.seek(0, IO::SEEK_SET))
76+
assert_equal(0, io.pos)
77+
assert_equal(0, io.seek(0, IO::SEEK_CUR))
78+
assert_equal(0, io.pos)
79+
assert_equal(0, io.seek(0, IO::SEEK_END)) # This should not segfault
80+
assert_equal(0, io.pos)
81+
end
82+
7383
def test_truncate
7484
io = StringIO.new("")
7585
io.puts "abc"

0 commit comments

Comments
 (0)