Skip to content

Commit 85ee630

Browse files
chagongCopilot
andcommitted
Address review feedback: fix regex ReDoS, finally-block state leaks, and typo
- Use possessive quantifiers in META_TAG_PATTERN and META_CHARSET_PATTERN to eliminate polynomial backtracking flagged by CodeQL - Wrap charset reset in try/finally so classpath restoration always executes even if setDefaultCharset throws - Fix remaining 'Shouldhave' typo in testBug426058 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 10cde8e commit 85ee630

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AttachedJavadocTests.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ public void testBug394382() throws JavaModelException {
10961096
} catch(JavaModelException e) {
10971097
assertTrue("Should not happen", false);
10981098
}
1099-
assertNotNull("Shouldhave a javadoc", javadoc); //$NON-NLS-1$
1099+
assertNotNull("Should have a javadoc", javadoc); //$NON-NLS-1$
11001100
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
11011101
char[] charArray = encodedContents.toCharArray();
11021102
encodedContents = new String(CharOperation.remove(charArray, '\r'));
@@ -1170,7 +1170,7 @@ public void testBug426058() throws Exception {
11701170
} catch(JavaModelException e) {
11711171
assertTrue("Should not happen", false);
11721172
}
1173-
assertNotNull("Shouldhave a javadoc", javadoc); //$NON-NLS-1$
1173+
assertNotNull("Should have a javadoc", javadoc); //$NON-NLS-1$
11741174
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
11751175
char[] charArray = encodedContents.toCharArray();
11761176
encodedContents = new String(CharOperation.remove(charArray, '\r'));
@@ -1179,8 +1179,11 @@ public void testBug426058() throws Exception {
11791179
assertTrue("Sources should be decoded the same way", encodedContents.equals(javadoc));
11801180
}
11811181
finally {
1182-
this.project.getProject().setDefaultCharset(oldEncoding, null);
1183-
this.project.setRawClasspath(oldClasspath, null);
1182+
try {
1183+
this.project.getProject().setDefaultCharset(oldEncoding, null);
1184+
} finally {
1185+
this.project.setRawClasspath(oldClasspath, null);
1186+
}
11841187
}
11851188
}
11861189
public void testAttachedJavadocWithModernMetaCharset() throws Exception {
@@ -1241,8 +1244,11 @@ public void testAttachedJavadocWithModernMetaCharset() throws Exception {
12411244
assertEquals("Sources should be decoded the same way", encodedContents, javadoc);
12421245
}
12431246
finally {
1244-
this.project.getProject().setDefaultCharset(oldEncoding, null);
1245-
this.project.setRawClasspath(oldClasspath, null);
1247+
try {
1248+
this.project.getProject().setDefaultCharset(oldEncoding, null);
1249+
} finally {
1250+
this.project.setRawClasspath(oldClasspath, null);
1251+
}
12461252
}
12471253
}
12481254
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=403154

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public abstract class JavaElement extends PlatformObject implements IJavaElement
6363
// private static final QualifiedName PROJECT_JAVADOC= new QualifiedName(JavaCore.PLUGIN_ID, "project_javadoc_location"); //$NON-NLS-1$
6464

6565
private static final int HTML_HEAD_PREFIX_LENGTH = 4096;
66-
private static final Pattern META_TAG_PATTERN = Pattern.compile("<meta\\s+[^>]*>", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
67-
private static final Pattern META_CHARSET_PATTERN = Pattern.compile("(?<![A-Za-z0-9_-])charset\\s*=\\s*([\\\"']?)([^\\s\\\"'/>;]+)\\1", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
66+
private static final Pattern META_TAG_PATTERN = Pattern.compile("<meta\\s[^>]*+>", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
67+
private static final Pattern META_CHARSET_PATTERN = Pattern.compile("(?<![A-Za-z0-9_-])charset\\s*+=\\s*+([\"']?)([^\\s\"'/>;]+)\\1", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
6868
public static final char JEM_ESCAPE = '\\';
6969
public static final char JEM_JAVAPROJECT = '=';
7070
public static final char JEM_PACKAGEFRAGMENTROOT = '/';

0 commit comments

Comments
 (0)