Skip to content

Commit f86e439

Browse files
committed
[GR-19220] Kernel#sprintf: don't unconditionally grant encoding validity (#1852).
PullRequest: truffleruby/1179
2 parents fcc4403 + 7fee3cf commit f86e439

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Bug fixes:
3636
* Do not leak TruffleRuby specific method Array#swap (#1816)
3737
* Fixed `#inspect` on broken UTF-8 sequences (#1842, @chrisseaton).
3838
* `Truffle::Interop.keys` should report methods of String and Symbol (#1817)
39+
* `Kernel#sprintf` encoding validity has been fixed (#1852, @XrXr).
3940

4041
Compatibility:
4142

spec/ruby/core/kernel/shared/sprintf.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,20 @@ def obj.to_str; end
865865
end
866866
end
867867

868+
describe "encoding" do
869+
it "can produce a string with valid encoding" do
870+
string = format("good day %{valid}", valid: "e")
871+
string.encoding.should == Encoding::UTF_8
872+
string.valid_encoding?.should be_true
873+
end
874+
875+
it "can produce a string with invalid encoding" do
876+
string = format("good day %{invalid}", invalid: "\x80")
877+
string.encoding.should == Encoding::UTF_8
878+
string.valid_encoding?.should be_false
879+
end
880+
end
881+
868882
describe "faulty key" do
869883
before :all do
870884
@base_method = @method

src/main/java/org/truffleruby/core/format/FormatNode.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
import java.nio.ByteBuffer;
1313
import java.util.Arrays;
1414

15-
import org.jcodings.specific.USASCIIEncoding;
1615
import org.truffleruby.core.array.ArrayUtils;
1716
import org.truffleruby.core.format.exceptions.TooFewArgumentsException;
1817
import org.truffleruby.core.rope.CodeRange;
19-
import org.truffleruby.core.rope.RopeOperations;
2018
import org.truffleruby.language.RubyBaseNode;
2119

2220
import com.oracle.truffle.api.CompilerDirectives;
@@ -162,7 +160,6 @@ protected void writeByte(VirtualFrame frame, byte value) {
162160
final int outputPosition = getOutputPosition(frame);
163161
output[outputPosition] = value;
164162
setOutputPosition(frame, outputPosition + 1);
165-
setStringCodeRange(frame, value >= 0 ? CodeRange.CR_7BIT : CodeRange.CR_VALID);
166163
increaseStringLength(frame, 1);
167164
}
168165

@@ -175,9 +172,6 @@ protected void writeBytes(VirtualFrame frame, byte[] values, int valuesLength) {
175172
final int outputPosition = getOutputPosition(frame);
176173
System.arraycopy(values, 0, output, outputPosition, valuesLength);
177174
setOutputPosition(frame, outputPosition + valuesLength);
178-
setStringCodeRange(
179-
frame,
180-
RopeOperations.isAsciiOnly(values, USASCIIEncoding.INSTANCE) ? CodeRange.CR_7BIT : CodeRange.CR_VALID);
181175
increaseStringLength(frame, valuesLength);
182176
}
183177

src/main/java/org/truffleruby/core/format/FormatRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Object execute(VirtualFrame frame) {
5252
frame.setObject(FormatFrameDescriptor.OUTPUT_SLOT, new byte[expectedLength]);
5353
frame.setInt(FormatFrameDescriptor.OUTPUT_POSITION_SLOT, 0);
5454
frame.setInt(FormatFrameDescriptor.STRING_LENGTH_SLOT, 0);
55-
frame.setInt(FormatFrameDescriptor.STRING_CODE_RANGE_SLOT, CodeRange.CR_7BIT.toInt());
55+
frame.setInt(FormatFrameDescriptor.STRING_CODE_RANGE_SLOT, CodeRange.CR_UNKNOWN.toInt());
5656
frame.setBoolean(FormatFrameDescriptor.TAINT_SLOT, false);
5757
frame.setObject(FormatFrameDescriptor.ASSOCIATED_SLOT, null);
5858

0 commit comments

Comments
 (0)