Skip to content

Commit 959e2e6

Browse files
bonzinigitster
authored andcommitted
avoid exponential regex match for java and objc function names
In the old regex ^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\([^;]*)$ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you can backtrack arbitrarily from [A-Za-z_0-9]* into [A-Za-z_], thus causing an exponential number of backtracks. Ironically it also causes the regex not to work as intended; for example "catch" can match the underlined part of the regex, the first repetition matching "c" and the second matching "atch". The replacement regex avoids this problem, because it makes sure that at least a space/tab is eaten on each repetition. In other words, a suffix of a repetition can never be a prefix of the next repetition. Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b7dc71 commit 959e2e6

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

userdiff.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
1313
"[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"),
1414
PATTERNS("java",
1515
"!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
16-
"^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
16+
"^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
17+
/* -- */
1718
"[a-zA-Z_][a-zA-Z0-9_]*"
1819
"|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
1920
"|[-+*/<>%&^|=!]="
@@ -25,7 +26,7 @@ PATTERNS("objc",
2526
/* Objective-C methods */
2627
"^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
2728
/* C functions */
28-
"^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$\n"
29+
"^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
2930
/* Objective-C class/protocol definitions */
3031
"^(@(implementation|interface|protocol)[ \t].*)$",
3132
/* -- */

0 commit comments

Comments
 (0)