Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,11 @@ private RubyFixnum seekCommon(ThreadContext context, int argc, IRubyObject arg0,
offset += ptr.pos;
break;
case 2:
offset += ptr.string.size();
if (ptr.string == null) {
offset += 0;
} else {
offset += ptr.string.size();
}
break;
default:
throw runtime.newErrnoEINVALError("invalid whence");
Expand Down
6 changes: 5 additions & 1 deletion ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,11 @@ strio_seek(int argc, VALUE *argv, VALUE self)
offset = ptr->pos;
break;
case 2:
offset = RSTRING_LEN(ptr->string);
if (NIL_P(ptr->string)) {
offset = 0;
} else {
offset = RSTRING_LEN(ptr->string);
}
break;
default:
error_inval("invalid whence");
Expand Down
10 changes: 10 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ def test_null
assert_nil io.getc
end

def test_seek_null
io = StringIO.new(nil)
assert_equal(0, io.seek(0, IO::SEEK_SET))
assert_equal(0, io.pos)
assert_equal(0, io.seek(0, IO::SEEK_CUR))
assert_equal(0, io.pos)
assert_equal(0, io.seek(0, IO::SEEK_END)) # This should not segfault
assert_equal(0, io.pos)
end

def test_truncate
io = StringIO.new("")
io.puts "abc"
Expand Down
Loading