Skip to content

Commit

Permalink
[GR-18163] Raise FrozenError when a frozen String passed as a destina…
Browse files Browse the repository at this point in the history
…tion buffer to Encoding::Convertor#primitive_convert method

PullRequest: truffleruby/3909
  • Loading branch information
andrykonchin committed Jul 25, 2023
2 parents 2b40141 + e0e6da7 commit a4a32f1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Compatibility:
* Add `Module#refinements` (#3039, @itarato).
* Add `Refinement#refined_class` (#3039, @itarato).
* Add `rb_hash_new_capa` function (#3039, @itarato).
* Fix `Encoding::Converter#primitive_convert` and raise `FrozenError` when a destination buffer argument is frozen (@andrykonchin).

Performance:

Expand Down
2 changes: 1 addition & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ def rb_str_conv_enc_opts(str, from, to, ecflags, ecopts)
end

ec = Encoding::Converter.new(from, to, ecopts || ecflags)
dest = ''
dest = +''
# This C API will (unlike primitive convert) not alter the source
# string, so we need to duplicate it.
status = ec.primitive_convert str.dup, dest, nil, nil
Expand Down
4 changes: 4 additions & 0 deletions spec/ruby/core/encoding/converter/primitive_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
-> { @ec.primitive_convert("","") }.should_not raise_error
end

it "raises FrozenError when the destination buffer is a frozen String" do
-> { @ec.primitive_convert("", "".freeze) }.should raise_error(FrozenError)
end

it "accepts nil for the destination byte offset" do
-> { @ec.primitive_convert("","", nil) }.should_not raise_error
end
Expand Down
2 changes: 2 additions & 0 deletions src/main/ruby/truffleruby/core/transcoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def primitive_convert(source, target, offset = nil, size = nil, options = 0)
source = source ? StringValue(source) : +''
target = StringValue(target)

Primitive.check_mutable_string target

if Primitive.nil? offset
offset = target.bytesize
else
Expand Down

0 comments on commit a4a32f1

Please sign in to comment.