Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be97fd8

Browse files
committed
Make invalid regexps blow up at definition time, JRUBY-283 and JRUBY-200
git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@2508 961051c9-f516-0410-bf72-c9f7e237a7b7
1 parent 7b23b70 commit be97fd8

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

src/org/jruby/RegexpTranslator.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@ public class RegexpTranslator {
1919
private static final Pattern OCTAL_MISSING_ZERO_PATTERN = Pattern.compile("\\\\([1-7][0-7]{1,2})");
2020
private static final Pattern POSIX_NAME = Pattern.compile("\\[:(\\w+):\\]");
2121

22-
public Pattern translate(String regex, int options, int javaRegexFlags) {
22+
public Pattern translate(String regex, int options, int javaRegexFlags) throws PatternSyntaxException {
2323
javaRegexFlags |= translateFlags(options);
2424
regex = translatePattern(regex, (javaRegexFlags & Pattern.COMMENTS) != 0);
2525

26-
// If we return null, rather than die this ends up generating a TypeError (which is better
27-
// than crashing).
28-
try {
29-
return Pattern.compile(regex, javaRegexFlags);
30-
} catch (PatternSyntaxException e) {}
31-
32-
return null;
26+
return Pattern.compile(regex, javaRegexFlags);
3327
}
3428

3529
// We do not check for pathological case of [:foo:] outside of [] (bug 1475096).
@@ -96,4 +90,4 @@ int translateFlags(int options) {
9690
return flags;
9791
}
9892

99-
}
93+
}

src/org/jruby/RubyRegexp.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ public static RubyClass createRegexpClass(IRuby runtime) {
152152
}
153153

154154
public void initialize(String regex, int options) {
155-
pattern = REGEXP_TRANSLATOR.translate(regex, options, code.flags());
155+
try {
156+
pattern = REGEXP_TRANSLATOR.translate(regex, options, code.flags());
157+
} catch(java.util.regex.PatternSyntaxException e) {
158+
throw getRuntime().newSyntaxError(e.getMessage());
159+
}
156160
}
157161

158162
public static String escapeSpecialChars(String original) {
@@ -224,7 +228,6 @@ public IRubyObject initialize(IRubyObject[] args) {
224228
} else {
225229
code = Code.create(getRuntime(), null);
226230
}
227-
228231
initialize(pat, opts);
229232
return getRuntime().getNil();
230233
}

src/org/jruby/ast/RegexpNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public String getValue() {
7979
return value;
8080
}
8181

82-
public Pattern getPattern() {
82+
public Pattern getPattern() throws java.util.regex.PatternSyntaxException {
8383
if (pattern == null) {
8484
pattern = translator.translate(value, options, Pattern.UNIX_LINES);
8585
}

src/org/jruby/evaluator/EvaluationState.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,11 @@ private static IRubyObject evalInternal(ThreadContext context, Node node, IRubyO
13091309
} else if((opts & 64) != 0) { // param s
13101310
lang = "u";
13111311
}
1312-
1313-
return RubyRegexp.newRegexp(runtime, iVisited.getPattern(), lang);
1312+
try {
1313+
return RubyRegexp.newRegexp(runtime, iVisited.getPattern(), lang);
1314+
} catch(java.util.regex.PatternSyntaxException e) {
1315+
throw runtime.newSyntaxError(e.getMessage());
1316+
}
13141317
}
13151318
case NodeTypes.RESCUEBODYNODE: {
13161319
RescueBodyNode iVisited = (RescueBodyNode) node;

0 commit comments

Comments
 (0)