[ovs-dev] [PATCH] checkpatch: avoid catastrophic backtracking

Aaron Conole aconole at redhat.com
Fri Sep 3 14:28:02 UTC 2021


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.

Signed-off-by: Aaron Conole <aconole at redhat.com>

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
---
NOTE: [0] above is accepted in OVN upstream and may require a slightly
      modified fix.

 tests/checkpatch.at     | 16 ++++++++++++++++
 utilities/checkpatch.py | 10 +++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/tests/checkpatch.at b/tests/checkpatch.at
index 0718acd995..f382149163 100755
--- a/tests/checkpatch.at
+++ b/tests/checkpatch.at
@@ -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
diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index 699fb4b027..16f46c78e8 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -274,9 +274,13 @@ def if_and_for_end_with_bracket_check(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:
-- 
2.31.1



More information about the dev mailing list