Skip to content

Commit a4a32f1

Browse files
committed
[GR-18163] Raise FrozenError when a frozen String passed as a destination buffer to Encoding::Convertor#primitive_convert method
PullRequest: truffleruby/3909
2 parents 2b40141 + e0e6da7 commit a4a32f1

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Compatibility:
2929
* Add `Module#refinements` (#3039, @itarato).
3030
* Add `Refinement#refined_class` (#3039, @itarato).
3131
* Add `rb_hash_new_capa` function (#3039, @itarato).
32+
* Fix `Encoding::Converter#primitive_convert` and raise `FrozenError` when a destination buffer argument is frozen (@andrykonchin).
3233

3334
Performance:
3435

lib/truffle/truffle/cext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def rb_str_conv_enc_opts(str, from, to, ecflags, ecopts)
881881
end
882882

883883
ec = Encoding::Converter.new(from, to, ecopts || ecflags)
884-
dest = ''
884+
dest = +''
885885
# This C API will (unlike primitive convert) not alter the source
886886
# string, so we need to duplicate it.
887887
status = ec.primitive_convert str.dup, dest, nil, nil

spec/ruby/core/encoding/converter/primitive_convert_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
-> { @ec.primitive_convert("","") }.should_not raise_error
1515
end
1616

17+
it "raises FrozenError when the destination buffer is a frozen String" do
18+
-> { @ec.primitive_convert("", "".freeze) }.should raise_error(FrozenError)
19+
end
20+
1721
it "accepts nil for the destination byte offset" do
1822
-> { @ec.primitive_convert("","", nil) }.should_not raise_error
1923
end

src/main/ruby/truffleruby/core/transcoding.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ def primitive_convert(source, target, offset = nil, size = nil, options = 0)
185185
source = source ? StringValue(source) : +''
186186
target = StringValue(target)
187187

188+
Primitive.check_mutable_string target
189+
188190
if Primitive.nil? offset
189191
offset = target.bytesize
190192
else

0 commit comments

Comments
 (0)