Skip to content

[GR-18163] Fix rb_str_locktmp() and rb_str_unlocktmp() to accept Strings that are already frozen #3867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Compatibility:
* Fix numeric coercing when `#coerce` method is not public (#3848, @andrykonchin).
* Fix `Kernel#raise` and don't override `cause` at exception re-raising (#3831, @andrykonchin).
* Return a pointer with `#type_size` of 1 for `Pointer#read_pointer` (@eregon).
* Fix `rb_str_locktmp()` and `rb_str_unlocktmp()` to raise `FrozenError` when string argument is frozen (#3752, @andrykonchin).

Performance:

Expand Down
30 changes: 26 additions & 4 deletions spec/ruby/optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1201,28 +1201,50 @@ def inspect

describe "rb_str_locktmp" do
it "raises an error when trying to lock an already locked string" do
str = "test"
str = +"test"
@s.rb_str_locktmp(str).should == str
-> { @s.rb_str_locktmp(str) }.should raise_error(RuntimeError, 'temporal locking already locked string')
end

it "locks a string so that modifications would raise an error" do
str = "test"
str = +"test"
@s.rb_str_locktmp(str).should == str
-> { str.upcase! }.should raise_error(RuntimeError, 'can\'t modify string; temporarily locked')
end

ruby_bug "#20998", ""..."3.5" do
it "raises FrozenError if string is frozen" do
str = -"rb_str_locktmp"
-> { @s.rb_str_locktmp(str) }.should raise_error(FrozenError)

str = +"rb_str_locktmp"
str.freeze
-> { @s.rb_str_locktmp(str) }.should raise_error(FrozenError)
end
end
end

describe "rb_str_unlocktmp" do
it "unlocks a locked string" do
str = "test"
str = +"test"
@s.rb_str_locktmp(str)
@s.rb_str_unlocktmp(str).should == str
str.upcase!.should == "TEST"
end

it "raises an error when trying to unlock an already unlocked string" do
-> { @s.rb_str_unlocktmp("test") }.should raise_error(RuntimeError, 'temporal unlocking already unlocked string')
-> { @s.rb_str_unlocktmp(+"test") }.should raise_error(RuntimeError, 'temporal unlocking already unlocked string')
end

ruby_bug "#20998", ""..."3.5" do
it "raises FrozenError if string is frozen" do
str = -"rb_str_locktmp"
-> { @s.rb_str_unlocktmp(str) }.should raise_error(FrozenError)

str = +"rb_str_locktmp"
str.freeze
-> { @s.rb_str_unlocktmp(str) }.should raise_error(FrozenError)
end
end
end

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/truffleruby/cext/CExtNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,10 @@ public abstract static class RbStrLockTmpNode extends CoreMethodArrayArgumentsNo

@Specialization
RubyString rbStrLockTmp(RubyString string,
@Cached InlinedBranchProfile errorProfile) {
@Cached InlinedBranchProfile errorProfile,
@Cached TypeNodes.CheckFrozenNode raiseIfFrozenNode) {
raiseIfFrozenNode.execute(this, string);

if (string.locked) {
errorProfile.enter(this);
throw new RaiseException(getContext(),
Expand All @@ -976,8 +979,7 @@ RubyString rbStrLockTmp(RubyString string,

@Specialization
RubyString rbStrLockTmpImmutable(ImmutableRubyString string) {
throw new RaiseException(getContext(),
coreExceptions().runtimeError("temporal locking immutable string", this));
throw new RaiseException(getContext(this), coreExceptions(this).frozenError(string, this));
}

}
Expand All @@ -987,7 +989,10 @@ public abstract static class RbStrUnlockTmpNode extends CoreMethodArrayArguments

@Specialization
RubyString rbStrUnlockTmp(RubyString string,
@Cached InlinedBranchProfile errorProfile) {
@Cached InlinedBranchProfile errorProfile,
@Cached TypeNodes.CheckFrozenNode raiseIfFrozenNode) {
raiseIfFrozenNode.execute(this, string);

if (!string.locked) {
errorProfile.enter(this);
throw new RaiseException(getContext(),
Expand All @@ -999,8 +1004,7 @@ RubyString rbStrUnlockTmp(RubyString string,

@Specialization
ImmutableRubyString rbStrUnlockTmpImmutable(ImmutableRubyString string) {
throw new RaiseException(getContext(),
coreExceptions().runtimeError("temporal unlocking immutable string", this));
throw new RaiseException(getContext(this), coreExceptions(this).frozenError(string, this));
}

}
Expand Down
Loading