Skip to content

Commit 0004820

Browse files
committed
Update rb_str_modify and rb_str_modify_expand to raise a FrozenError for frozen strings
1 parent e840004 commit 0004820

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Bug fixes:
99
Compatibility:
1010

1111
* Implement `Process::Status.wait` (#2378).
12+
* Update `rb_str_modify` and `rb_str_modify_expand` to raise a `FrozenError` when given a frozen string (#2392).
1213

1314
Performance:
1415

lib/cext/ABI_check.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3
1+
4

spec/ruby/optional/capi/string_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,12 @@ def inspect
665665
end
666666
end
667667

668+
describe "rb_str_modify" do
669+
it "raises an error if the string is frozen" do
670+
-> { @s.rb_str_modify("frozen".freeze) }.should raise_error(FrozenError)
671+
end
672+
end
673+
668674
describe "rb_str_modify_expand" do
669675
it "grows the capacity to bytesize + expand, not changing the bytesize" do
670676
str = @s.rb_str_buf_new(256, "abcd")
@@ -685,6 +691,10 @@ def inspect
685691
@s.RSTRING_LEN(str).should == 3
686692
@s.rb_str_capacity(str).should == 1027
687693
end
694+
695+
it "raises an error if the string is frozen" do
696+
-> { @s.rb_str_modify_expand("frozen".freeze, 10) }.should raise_error(FrozenError)
697+
end
688698
end
689699

690700
describe "rb_str_resize" do

src/main/c/cext/string.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ VALUE rb_str_split(VALUE string, const char *split) {
174174
}
175175

176176
void rb_str_modify(VALUE string) {
177+
rb_check_frozen(string);
177178
ENC_CODERANGE_CLEAR(string);
178179
}
179180

@@ -337,6 +338,7 @@ void rb_str_modify_expand(VALUE str, long expand) {
337338
rb_raise(rb_eArgError, "string size too big");
338339
}
339340

341+
rb_check_frozen(str);
340342
if (expand > 0) {
341343
// rb_str_modify_expand() resizes the native buffer but does not change
342344
// RSTRING_LEN() (and therefore String#bytesize).

0 commit comments

Comments
 (0)