Skip to content

Commit dd0e3e4

Browse files
committed
anchor oracle hint pattern to avoid quadratic backtracking
1 parent 44c0fe5 commit dd0e3e4

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/main/java/net/sf/jsqlparser/expression/OracleHint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class OracleHint extends ASTNodeAccessImpl implements Expression {
2424

2525
private static final Pattern SINGLE_LINE = Pattern.compile("--\\+ *([^ ].*[^ ])");
2626
private static final Pattern MULTI_LINE =
27-
Pattern.compile("/\\*\\+ *([^ ].*[^ ]) *\\*+/", Pattern.MULTILINE | Pattern.DOTALL);
27+
Pattern.compile("\\A/\\*\\+ *([^ ].*[^ ]) *\\*+/", Pattern.MULTILINE | Pattern.DOTALL);
2828

2929
private String value;
3030
private boolean singleLine = false;

src/test/java/net/sf/jsqlparser/expression/OracleHintTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
14+
15+
import java.time.Duration;
1216
import net.sf.jsqlparser.JSQLParserException;
1317
import net.sf.jsqlparser.test.TestUtils;
1418
import org.junit.jupiter.api.Test;
@@ -46,4 +50,18 @@ void testMerge() throws JSQLParserException {
4650
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlString, true);
4751
}
4852

53+
@Test
54+
void testCraftedHintCommentDoesNotBacktrack() {
55+
final StringBuilder sb = new StringBuilder("-- /*+ ");
56+
for (int i = 0; i < 100000; i++) {
57+
sb.append('*');
58+
}
59+
final String crafted = sb.toString();
60+
61+
// a line comment carrying an unterminated /*+ marker (no closing */) used to make the
62+
// block hint pattern backtrack quadratically, and it is not an oracle hint anyway
63+
assertTimeoutPreemptively(Duration.ofSeconds(2),
64+
() -> assertFalse(OracleHint.isHintMatch(crafted)));
65+
}
66+
4967
}

0 commit comments

Comments
 (0)