Fri, 08 Jun 2018 11:11:06 +0200
8204290: Add check to limit number of capture groups
Reviewed-by: sundar, jlaskey
1.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/Config.java Fri Nov 08 16:49:37 2019 +0000 1.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/Config.java Fri Jun 08 11:11:06 2018 +0200 1.3 @@ -45,6 +45,7 @@ 1.4 1.5 final int NREGION = 10; 1.6 final int MAX_BACKREF_NUM = 1000; 1.7 + final int MAX_CAPTURE_GROUP_NUM = 0x8000; 1.8 final int MAX_REPEAT_NUM = 100000; 1.9 final int MAX_MULTI_BYTE_RANGES_NUM = 10000; 1.10
2.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ScanEnvironment.java Fri Nov 08 16:49:37 2019 +0000 2.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ScanEnvironment.java Fri Jun 08 11:11:06 2018 +0200 2.3 @@ -62,6 +62,9 @@ 2.4 } 2.5 2.6 public int addMemEntry() { 2.7 + if (numMem >= Config.MAX_CAPTURE_GROUP_NUM) { 2.8 + throw new InternalException(ErrorMessages.ERR_TOO_MANY_CAPTURE_GROUPS); 2.9 + } 2.10 if (numMem++ == 0) { 2.11 memNodes = new Node[SCANENV_MEMNODES_SIZE]; 2.12 } else if (numMem >= memNodes.length) {
3.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/exception/ErrorMessages.java Fri Nov 08 16:49:37 2019 +0000 3.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/exception/ErrorMessages.java Fri Jun 08 11:11:06 2018 +0200 3.3 @@ -31,6 +31,7 @@ 3.4 final String ERR_PARSER_BUG = "internal parser error (bug)"; 3.5 final String ERR_UNDEFINED_BYTECODE = "undefined bytecode (bug)"; 3.6 final String ERR_UNEXPECTED_BYTECODE = "unexpected bytecode (bug)"; 3.7 + final String ERR_TOO_MANY_CAPTURE_GROUPS = "too many capture groups"; 3.8 3.9 /* syntax error */ 3.10 final String ERR_END_PATTERN_AT_LEFT_BRACE = "end pattern at left brace";
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/script/basic/JDK-8204290.js Fri Jun 08 11:11:06 2018 +0200 4.3 @@ -0,0 +1,40 @@ 4.4 +/* 4.5 + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/** 4.28 + * JDK-8204290: Add check to limit number of capture groups 4.29 + * 4.30 + * @test 4.31 + * @run 4.32 + */ 4.33 + 4.34 +try { 4.35 + var captureGroups = ""; 4.36 + for (i=0; i < 0x8001; i++) { captureGroups += "()"; } 4.37 + new RegExp(captureGroups); 4.38 + fail("Expected exception"); 4.39 +} catch (e) { 4.40 + Assert.assertTrue(e instanceof SyntaxError); 4.41 + Assert.assertEquals(e.message, "too many capture groups"); 4.42 +} 4.43 +