[PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks
From: Bart Van Assche
Date: Tue Dec 01 2026 - 17:54:42 EST
Add cheap literal substring, keyword, or $check guards ahead of expensive
$Declare, $Type, $NonptrType, $Lval, and $balanced_parens regular
expressions in process():
- MULTILINE_DEREFERENCE: require /(?:\.|->)\s*$/ on $prevline first.
- UNSPECIFIED_INT: require index($line, 'signed') >= 0.
- FUNCTION_WITHOUT_ARGS: require /\(\s*\)/.
- POINTER_LOCATION: require '*' or $Modifier\b (and '(' for casts).
- OPEN_BRACE: require '{' and '(' in $sline.
- STORAGE_CLASS and INLINE_LOCATION: require \b$Storage\b / \b$Inline\b.
- WEAK_DECLARATION: check index($line, 'weak') >= 0 and the weak patterns
before matching (?:$Declare|$DeclareMisordered).
- PREFER_KERNEL_TYPES: require $check and \b$typeC99Typedefs. Omit the
trailing \b since the guard only has to be a necessary condition for the
\b($typeC99Typedefs)\b match on the declared type.
- BOOL_COMPARISON: require $check, '=', and \b(?:true|false)\b.
- UNINITIALIZED_PTR_WITH_FREE: require index($line, '__free') >= 0.
Skip backtracking across complex declaration patterns on lines that cannot
match or when --strict checks are disabled.
Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
scripts/checkpatch.pl | 43 ++++++++++++++++++++++++++++++-------------
1 file changed, 30 insertions(+), 13 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index fbde43ed1767..64c965c84d7d 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -4767,7 +4767,8 @@ sub process {
}
# check for dereferences that span multiple lines
- if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
+ if ($prevline =~ /(?:\.|->)\s*$/ &&
+ $prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
$line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
my $ref = $1;
@@ -4779,7 +4780,8 @@ sub process {
}
# check for declarations of signed or unsigned without int
- while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
+ while (index($line, 'signed') >= 0 &&
+ $line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
my $type = $1;
my $var = $2;
$var = "" if (!defined $var);
@@ -5032,7 +5034,8 @@ sub process {
}
# check for function declarations without arguments like "int foo()"
- if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
+ if ($line =~ /\(\s*\)/ &&
+ $line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
if (ERROR("FUNCTION_WITHOUT_ARGS",
"Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
$fix) {
@@ -5053,7 +5056,9 @@ sub process {
# * goes on variable not on type
# (char*[ const])
- while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
+ my $has_ptr_or_mod = (index($line, '*') >= 0 || $line =~ /$Modifier\b/);
+ while ($has_ptr_or_mod && index($line, '(') >= 0 &&
+ $line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
#print "AA<$1>\n";
my ($ident, $from, $to) = ($1, $2, $2);
@@ -5078,7 +5083,8 @@ sub process {
}
}
}
- while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
+ while ($has_ptr_or_mod &&
+ $line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
#print "BB<$1>\n";
my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
@@ -5187,6 +5193,8 @@ sub process {
# function brace can't be on same line, except for #defines of do while,
# or if closed on same line
if ($perl_version_ok &&
+ index($sline, '{') >= 0 &&
+ index($sline, '(') >= 0 &&
$sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
$sline !~ /\#\s*define\b.*do\s*\{/ &&
$sline !~ /}/) {
@@ -7019,7 +7027,8 @@ sub process {
}
# check that the storage class is not after a type
- if ($line =~ /\b($Type)\s+($Storage)\b/) {
+ if ($line =~ /\b$Storage\b/ &&
+ $line =~ /\b($Type)\s+($Storage)\b/) {
WARN("STORAGE_CLASS",
"storage class '$2' should be located before type '$1'\n" . $herecurr);
}
@@ -7034,8 +7043,9 @@ sub process {
# check the location of the inline attribute, that it is between
# storage class and type.
- if ($line =~ /\b$Type\s+$Inline\b/ ||
- $line =~ /\b$Inline\s+$Storage\b/) {
+ if ($line =~ /\b$Inline\b/ &&
+ ($line =~ /\b$Type\s+$Inline\b/ ||
+ $line =~ /\b$Inline\s+$Storage\b/)) {
ERROR("INLINE_LOCATION",
"inline keyword should sit between storage class and type\n" . $herecurr);
}
@@ -7118,15 +7128,18 @@ sub process {
# Check for __attribute__ weak, or __weak declarations (may have link issues)
if ($perl_version_ok &&
- $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
+ index($line, 'weak') >= 0 &&
($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
- $line =~ /\b__weak\b/)) {
+ $line =~ /\b__weak\b/) &&
+ $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/) {
ERROR("WEAK_DECLARATION",
"Using weak declarations can have unintended link defects\n" . $herecurr);
}
# check for c99 types like uint8_t used outside of uapi/ and tools/
- if ($realfile !~ m@\binclude/uapi/@ &&
+ if ($check &&
+ $line =~ /\b$typeC99Typedefs/ &&
+ $realfile !~ m@\binclude/uapi/@ &&
!is_userspace($realfile) &&
$line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
my $type = $1;
@@ -7691,7 +7704,10 @@ sub process {
}
# check for comparisons against true and false
- if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
+ if ($check &&
+ index($line, '=') >= 0 &&
+ $line =~ /\b(?:true|false)\b/i &&
+ $line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
my $lead = $1;
my $arg = $2;
my $test = $3;
@@ -8033,7 +8049,8 @@ sub process {
}
# check for uninitialized pointers with __free attribute
- while ($line =~ /\*\s*($Ident)\s+__free\s*\(\s*$Ident\s*\)\s*[,;]/g) {
+ while (index($line, '__free') >= 0 &&
+ $line =~ /\*\s*($Ident)\s+__free\s*\(\s*$Ident\s*\)\s*[,;]/g) {
ERROR("UNINITIALIZED_PTR_WITH_FREE",
"pointer '$1' with __free attribute should be initialized\n" . $herecurr);
}