File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed
Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -180,6 +180,7 @@ check_modifiable(struct StringIO *ptr)
180180 else if (OBJ_FROZEN_RAW (ptr -> string )) {
181181 rb_raise (rb_eIOError , "not modifiable string" );
182182 }
183+ rb_str_modify (ptr -> string );
183184}
184185
185186static VALUE
Original file line number Diff line number Diff line change @@ -14,6 +14,24 @@ def open_file(content)
1414
1515 include TestEOF ::Seek
1616
17+ def test_do_not_mutate_shared_buffers
18+ # Ensure we have two strings that are not embedded but have the same shared
19+ # string reference.
20+ #
21+ # In this case, we must use eval because we need two strings literals that
22+ # are long enough they cannot be embedded, but also contain the same bytes.
23+
24+ a = eval ( ( "x" * 1024 ) . dump )
25+ b = eval ( ( "x" * 1024 ) . dump )
26+
27+ s = StringIO . new ( b )
28+ s . getc
29+ s . ungetc '#'
30+
31+ # We mutated b, so a should not be mutated
32+ assert_equal ( "x" , a [ 0 ] )
33+ end
34+
1735 def test_version
1836 assert_kind_of ( String , StringIO ::VERSION )
1937 end
You can’t perform that action at this time.
0 commit comments