Skip to content

8300207: Add a pre-check for the number of canonical equivalent permutations in j.u.r.Pattern #3765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/java.base/share/classes/java/util/regex/Pattern.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2023, 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
Expand Down Expand Up @@ -1090,6 +1090,10 @@ public static Pattern compile(String regex) {
*
* @throws PatternSyntaxException
* If the expression's syntax is invalid
*
* @implNote If {@link #CANON_EQ} is specified and the number of combining
* marks for any character is too large, an {@link java.lang.OutOfMemoryError}
* is thrown.
*/
public static Pattern compile(String regex, int flags) {
return new Pattern(regex, flags);
Expand Down Expand Up @@ -1123,6 +1127,13 @@ public String toString() {
* The character sequence to be matched
*
* @return A new matcher for this pattern
*
* @implNote When a {@link Pattern} is deserialized, compilation is deferred
* until a direct or indirect invocation of this method. Thus, if a
* deserialized pattern has {@link #CANON_EQ} among its flags and the number
* of combining marks for any character is too large, an
* {@link java.lang.OutOfMemoryError} is thrown,
* as in {@link #compile(String, int)}.
*/
public Matcher matcher(CharSequence input) {
if (!compiled) {
Expand Down Expand Up @@ -1596,14 +1607,30 @@ private static String[] producePermutations(String input) {
return result;
}

int length = 1;
/*
* Since
* 12! = 479_001_600 < Integer.MAX_VALUE
* 13! = 6_227_020_800 > Integer.MAX_VALUE
* the computation of n! using int arithmetic will overflow iff
* n < 0 or n > 12
*
* Here, nCodePoints! is computed in the next for-loop below.
* As nCodePoints >= 0, the computation overflows iff nCodePoints > 12.
* In that case, throw OOME to simulate length > Integer.MAX_VALUE.
*/
int nCodePoints = countCodePoints(input);
for(int x=1; x<nCodePoints; x++)
length = length * (x+1);
if (nCodePoints > 12) {
throw new OutOfMemoryError("Pattern too complex");
}

/* Compute length = nCodePoints! */
int length = 1;
for (int x = 2; x <= nCodePoints; ++x) {
length *= x;
}
String[] temp = new String[length];

int combClass[] = new int[nCodePoints];
int[] combClass = new int[nCodePoints];
for(int x=0, i=0; x<nCodePoints; x++) {
int c = Character.codePointAt(input, i);
combClass[x] = getClass(c);
Expand Down