8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException

Reviewed-by: sherman
This commit is contained in:
Ivan Gerasimov 2014-07-16 13:02:24 +04:00
parent 1ebe2a40a9
commit e03728427a
2 changed files with 40 additions and 2 deletions

View File

@ -917,6 +917,13 @@ public final class Pattern
*/
public static final int UNICODE_CHARACTER_CLASS = 0x100;
/**
* Contains all possible flags for compile(regex, flags).
*/
private static final int ALL_FLAGS = CASE_INSENSITIVE | MULTILINE |
DOTALL | UNICODE_CASE | CANON_EQ | UNIX_LINES | LITERAL |
UNICODE_CHARACTER_CLASS | COMMENTS;
/* Pattern has only two serialized components: The pattern string
* and the flags, which are all that is needed to recompile the pattern
* when it is deserialized.
@ -1336,6 +1343,10 @@ public final class Pattern
* only a Start node and a LastNode node.
*/
private Pattern(String p, int f) {
if ((f & ~ALL_FLAGS) != 0) {
throw new IllegalArgumentException("Unknown flag 0x"
+ Integer.toHexString(f));
}
pattern = p;
flags = f;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,7 +32,7 @@
* 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
* 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
* 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
* 8027645 8035076 8039124
* 8027645 8035076 8039124 8035975
*/
import java.util.regex.*;
@ -150,6 +150,7 @@ public class RegExTest {
groupCurlyNotFoundSuppTest();
groupCurlyBackoffTest();
patternAsPredicate();
invalidFlags();
if (failure) {
throw new
@ -4457,4 +4458,30 @@ public class RegExTest {
}
report("Pattern.asPredicate");
}
// This test is for 8035975
private static void invalidFlags() throws Exception {
for (int flag = 1; flag != 0; flag <<= 1) {
switch (flag) {
case Pattern.CASE_INSENSITIVE:
case Pattern.MULTILINE:
case Pattern.DOTALL:
case Pattern.UNICODE_CASE:
case Pattern.CANON_EQ:
case Pattern.UNIX_LINES:
case Pattern.LITERAL:
case Pattern.UNICODE_CHARACTER_CLASS:
case Pattern.COMMENTS:
// valid flag, continue
break;
default:
try {
Pattern.compile(".", flag);
failCount++;
} catch (IllegalArgumentException expected) {
}
}
}
report("Invalid compile flags");
}
}