From f6c10c72ee9cc8ecbdc69c671e1a673dc686bb49 Mon Sep 17 00:00:00 2001 From: James Ring Date: Tue, 23 Oct 2018 08:17:12 -0700 Subject: [PATCH] Detect invalid unicode class specification (#74) Previously, the parser would unconditionally attempt to read the unicode class spec after a \p or \P, even if the pattern is truncated and invalid. Fixes issue #72. --- java/com/google/re2j/Parser.java | 4 ++++ javatests/com/google/re2j/RE2CompileTest.java | 2 ++ 2 files changed, 6 insertions(+) diff --git a/java/com/google/re2j/Parser.java b/java/com/google/re2j/Parser.java index d1870c69..2b965052 100644 --- a/java/com/google/re2j/Parser.java +++ b/java/com/google/re2j/Parser.java @@ -1539,6 +1539,10 @@ private boolean parseUnicodeClass(StringIterator t, CharClass cc) throws Pattern if (c == 'P') { sign = -1; } + if (!t.more()) { + t.rewindTo(startPos); + throw new PatternSyntaxException(ERR_INVALID_CHAR_RANGE, t.rest()); + } c = t.pop(); String name; if (c != '{') { diff --git a/javatests/com/google/re2j/RE2CompileTest.java b/javatests/com/google/re2j/RE2CompileTest.java index 3fc7f0dd..fb31f982 100644 --- a/javatests/com/google/re2j/RE2CompileTest.java +++ b/javatests/com/google/re2j/RE2CompileTest.java @@ -44,6 +44,8 @@ public static String[][] testData() { {"a**", "invalid nested repetition operator: `**`"}, {"a*+", "invalid nested repetition operator: `*+`"}, {"\\x", "invalid escape sequence: `\\x`"}, + {"\\p", "invalid character class range: `\\p`"}, + {"\\p{", "invalid character class range: `\\p{`"} }; }