Skip to content

Commit

Permalink
checkpatch: Avoid catastrophic backtracking.
Browse files Browse the repository at this point in the history
As Frode Nordahl points out in [0], it is possible for the
python regex module to enter a case of catastrophic backtracking
which causes oscillation between states and hangs the checkpatch
script.

One suggested solution to these cases is to use an anchor[1] in
the regex, which should force the backtrack to exit early.
However, when I tested this, it didn't seem to improve anything
(since the start is already anchored, and trying to anchor the
end results in the same hang).

Instead, we explicitly check that the line ends with '\\' before
trying to match on the 'if-inside-a-macro' check.  A new check
is added to catch the case in checkpatch.

0: https://mail.openvswitch.org/pipermail/ovs-dev/2021-August/386881.html
1: https://stackoverflow.com/questions/22072406/preventing-any-backtracking-in-regex-past-a-specific-pattern

Signed-off-by: Aaron Conole <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
apconole authored and igsilya committed Sep 8, 2021
1 parent 372b790 commit 00d3d4a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 16 additions & 0 deletions tests/checkpatch.at
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ done
AT_CLEANUP


AT_SETUP([checkpatch - catastrophic backtracking])
dnl Special case this rather than using the above construct because sometimes a
dnl warning needs to be generated for line lengths (f.e. when the 'while'
dnl keyword is used).
try_checkpatch \
"COMMON_PATCH_HEADER
+ if (!b_ctx_in->chassis_rec || !b_ctx_in->br_int || !b_ctx_in->ovs_idl_txn)
" \
"ERROR: Inappropriate bracing around statement
#8 FILE: A.c:1:
if (!b_ctx_in->chassis_rec || !b_ctx_in->br_int || !b_ctx_in->ovs_idl_txn)
"

AT_CLEANUP


AT_SETUP([checkpatch - parenthesized constructs - for])
try_checkpatch \
"COMMON_PATCH_HEADER
Expand Down
10 changes: 7 additions & 3 deletions utilities/checkpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ def balanced_parens(line):
if not balanced_parens(line):
return True

if __regex_ends_with_bracket.search(line) is None and \
__regex_if_macros.match(line) is None:
return False
if __regex_ends_with_bracket.search(line) is None:
if line.endswith("\\") and \
__regex_if_macros.match(line) is not None:
return True
else:
return False

if __regex_conditional_else_bracing.match(line) is not None:
return False
if __regex_conditional_else_bracing2.match(line) is not None:
Expand Down

0 comments on commit 00d3d4a

Please sign in to comment.