Skip to content

Commit 7ca2f63

Browse files
committed
[Truffle] Fixed an infinite loop in String due to not checking invalid input.
1 parent 0c6fedc commit 7ca2f63

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
fails:String#split with String splits on multibyte characters
22
fails:String#split with String taints the resulting strings if self is tainted
33
fails:String#split with Regexp taints the resulting strings if self is tainted
4-
fails:String#split with Regexp returns an ArgumentError if an invalid UTF-8 string is supplied

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/RegexpPrimitiveNodes.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
*/
1010
package org.jruby.truffle.nodes.rubinius;
1111

12+
import com.oracle.truffle.api.CompilerDirectives;
1213
import org.joni.Matcher;
1314
import org.jruby.truffle.runtime.RubyContext;
15+
import org.jruby.truffle.runtime.control.RaiseException;
1416
import org.jruby.truffle.runtime.core.*;
1517

1618
import com.oracle.truffle.api.dsl.Specialization;
1719
import com.oracle.truffle.api.source.SourceSection;
20+
import org.jruby.util.StringSupport;
1821

1922
/**
2023
* Rubinius primitives associated with the Ruby {@code Regexp} class.
@@ -59,6 +62,17 @@ public RegexpSearchRegionPrimitiveNode(RegexpSearchRegionPrimitiveNode prev) {
5962
public Object searchRegion(RubyRegexp regexp, RubyString string, int start, int end, boolean forward) {
6063
notDesignedForCompilation();
6164

65+
if (regexp.getRegex() == null) {
66+
CompilerDirectives.transferToInterpreter();
67+
throw new RaiseException(getContext().getCoreLibrary().typeError("uninitialized Regexp", this));
68+
}
69+
70+
if (string.scanForCodeRange() == StringSupport.CR_BROKEN) {
71+
CompilerDirectives.transferToInterpreter();
72+
throw new RaiseException(getContext().getCoreLibrary().argumentError(
73+
String.format("invalid byte sequence in %s", string.getByteList().getEncoding()), this));
74+
}
75+
6276
final Matcher matcher = regexp.getRegex().matcher(string.getBytes().bytes());
6377

6478
return regexp.matchCommon(string, false, false, matcher, start, end);

0 commit comments

Comments
 (0)