Skip to content

Commit cbb7217

Browse files
XrXrchrisseaton
authored andcommitted
Kernel#sprintf: don't unconditionally grant encoding validity
The old code seems to grant strings with encoding validity when it does not have enough information to make that call. Be defensive and claim we don't know the code range instead.
1 parent 6c7d440 commit cbb7217

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

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)