From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=199.89.1.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796165654; b=DLYMFjEktXzl3c0nr9b2FNTToBDeATct2KZJKl3c9itVBua6FxFwLXnuVgvGotcJW4HD mhXVGCoTaOsd6uRw5vKua+NLzxIUR2FnFujR9Bmb95k4GyGHT1vEoXVa3WZqFw8fZEmeP fug6WjiHJQc9D86kudvMhr77hLnbXWUpOIwyOYSxWrkeGois0vXKM24YZGUuzVnXz05WM CoL6iiRp2snH1rT5MZV8nTMW3EErnlxxgN/0a5/GnXRFbhdRGEEEqNiuUQmgiKxW0Puk8 QwUTfHNw9d2Y9PIQCEGkvYTa00fSt4yBreVarGGiGC78vbSv7vBmXO6+JVMBgAscD6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796165654; h=DMARC-Filter:Received:DKIM-Signature:X-Virus-Scanned:Received: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding; bh=WdtODvDSDWBzlIKKCcI/3NRwdwI154hCs8bxiGqvsYM=; b=BnehFnCFxED6J1eaX4Zx8BPjSj+6aX73U2+KHAWY1XNcjjNXuw/06IZ5hXDwqoAmPDWt Vpb/lCAXqqnlU+5FoXO5oPmCRcTcQG5knOqPjYqBvbRXWnvChUcuIrkF+4ej3r3mLBbCn zuY6BerJvGYCIa8Eut9hgqfvaqqNupdMUch83r8MHK0zKFh8fDq4epklxZsYVHEJBkJ2w rmLyuuZoGNIYxq3gYj46ZzqsBPX2bP6XjV3emqchQLEkTtrZ45+vzihzkAOSxAGGYbmxg rKR9ngX52YKvecLt472p/tLiiGSxjNssO/Zw3KhJk1EdZ6plJyaT9lk5PMovKkT8qBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=16ud6BAC; arc=none smtp.remote-ip=199.89.1.14 Received: from 011.lax.mailroute.net (011.lax.mailroute.net [199.89.1.14]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFD81606A6BE for ; Tue, 1 Dec 2026 22:54:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AFD81606A6BE Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=16ud6BAC Received: from localhost (localhost [127.0.0.1]) by 011.lax.mailroute.net (Postfix) with ESMTP id 4jfGR84d9Dz1XMYJb; Tue, 1 Dec 2026 22:54:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:references:in-reply-to :x-mailer:message-id:date:date:subject:subject:from:from :received:received; s=mr01; t=1796165647; x=1798757648; bh=WdtOD vDSDWBzlIKKCcI/3NRwdwI154hCs8bxiGqvsYM=; b=16ud6BACwns1q9PQEQoEB hElpjJkzYghMYDbktOglAQdop/oBSV+CNthJHykVLQU2j66FuJmM8+kHBp2azoNL PGlxTkn+pzWxNwwcdGG5p6vZLQdQWzUaEZqJHcBFG15t8Uuv8xN2LfuyjDs5dx8o 42UyykGzD5rz1rx4ci0EstTdSjXrEt5brcFOG1PS6RCuPC4wAXKWesMDgZV9N/Kl xyiIeuSpeo78+JTdiCFY2rRkrfMWpiHLPAdgPBf9JhY0bWeOuUdJa7M7SFg/WiUY 2NG9a3jYh7cPgVBMRkhhhi4jBfvzfyjPi0P4uLOQ7Q0eobKtiZYtVYChHRJVSzrE g== X-Virus-Scanned: by MailRoute Received: from 011.lax.mailroute.net ([127.0.0.1]) by localhost (011.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id cKKAdr7LE5kY; Tue, 1 Dec 2026 22:54:07 +0000 (UTC) Received: from bvanassche.c.googlers.com.com (57.237.82.34.bc.googleusercontent.com [34.82.237.57]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 011.lax.mailroute.net (Postfix) with ESMTPSA id 4jfGR014V0z1XMYJY; Tue, 1 Dec 2026 22:54:04 +0000 (UTC) From: Bart Van Assche To: Joe Perches , Andy Whitcroft Cc: Andrew Morton , linux-kernel@vger.kernel.org, Bart Van Assche Subject: [PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks Date: Tue, 1 Dec 2026 14:53:50 -0800 Message-ID: <4a83e98a65f3656d1b82467932e5c0c6f3a5aa8d.1796165270.git.bvanassche@acm.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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') >=3D 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') >=3D 0 and the weak patter= ns 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 th= e \b($typeC99Typedefs)\b match on the declared type. - BOOL_COMPARISON: require $check, '=3D', and \b(?:true|false)\b. - UNINITIALIZED_PTR_WITH_FREE: require index($line, '__free') >=3D 0. Skip backtracking across complex declaration patterns on lines that canno= t match or when --strict checks are disabled. Signed-off-by: Bart Van Assche --- 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 { } =20 # check for dereferences that span multiple lines - if ($prevline =3D~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && + if ($prevline =3D~ /(?:\.|->)\s*$/ && + $prevline =3D~ /^\+.*$Lval\s*(?:\.|->)\s*$/ && $line =3D~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) { $prevline =3D~ /($Lval\s*(?:\.|->))\s*$/; my $ref =3D $1; @@ -4779,7 +4780,8 @@ sub process { } =20 # check for declarations of signed or unsigned without int - while ($line =3D~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*(= $Ident)?\s*[=3D,;\[\)\(]}g) { + while (index($line, 'signed') >=3D 0 && + $line =3D~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*(= $Ident)?\s*[=3D,;\[\)\(]}g) { my $type =3D $1; my $var =3D $2; $var =3D "" if (!defined $var); @@ -5032,7 +5034,8 @@ sub process { } =20 # check for function declarations without arguments like "int foo()" - if ($line =3D~ /(\b$Type\s*$Ident)\s*\(\s*\)/) { + if ($line =3D~ /\(\s*\)/ && + $line =3D~ /(\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 { =20 # * goes on variable not on type # (char*[ const]) - while ($line =3D~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g)= { + my $has_ptr_or_mod =3D (index($line, '*') >=3D 0 || $line =3D~ /$Modif= ier\b/); + while ($has_ptr_or_mod && index($line, '(') >=3D 0 && + $line =3D~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g)= { #print "AA<$1>\n"; my ($ident, $from, $to) =3D ($1, $2, $2); =20 @@ -5078,7 +5083,8 @@ sub process { } } } - while ($line =3D~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Iden= t))}g) { + while ($has_ptr_or_mod && + $line =3D~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Iden= t))}g) { #print "BB<$1>\n"; my ($match, $from, $to, $ident) =3D ($1, $2, $2, $3); =20 @@ -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, '{') >=3D 0 && + index($sline, '(') >=3D 0 && $sline =3D~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ && $sline !~ /\#\s*define\b.*do\s*\{/ && $sline !~ /}/) { @@ -7019,7 +7027,8 @@ sub process { } =20 # check that the storage class is not after a type - if ($line =3D~ /\b($Type)\s+($Storage)\b/) { + if ($line =3D~ /\b$Storage\b/ && + $line =3D~ /\b($Type)\s+($Storage)\b/) { WARN("STORAGE_CLASS", "storage class '$2' should be located before type '$1'\n" . $her= ecurr); } @@ -7034,8 +7043,9 @@ sub process { =20 # check the location of the inline attribute, that it is between # storage class and type. - if ($line =3D~ /\b$Type\s+$Inline\b/ || - $line =3D~ /\b$Inline\s+$Storage\b/) { + if ($line =3D~ /\b$Inline\b/ && + ($line =3D~ /\b$Type\s+$Inline\b/ || + $line =3D~ /\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 { =20 # Check for __attribute__ weak, or __weak declarations (may have link is= sues) if ($perl_version_ok && - $line =3D~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_p= arens\s*(?:$Attribute)?\s*;/ && + index($line, 'weak') >=3D 0 && ($line =3D~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || - $line =3D~ /\b__weak\b/)) { + $line =3D~ /\b__weak\b/) && + $line =3D~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_p= arens\s*(?:$Attribute)?\s*;/) { ERROR("WEAK_DECLARATION", "Using weak declarations can have unintended link defects\n" . = $herecurr); } =20 # check for c99 types like uint8_t used outside of uapi/ and tools/ - if ($realfile !~ m@\binclude/uapi/@ && + if ($check && + $line =3D~ /\b$typeC99Typedefs/ && + $realfile !~ m@\binclude/uapi/@ && !is_userspace($realfile) && $line =3D~ /\b($Declare)\s*$Ident\s*[=3D;,\[]/) { my $type =3D $1; @@ -7691,7 +7704,10 @@ sub process { } =20 # check for comparisons against true and false - if ($line =3D~ /\+\s*(.*?)\b(true|false|$Lval)\s*(=3D=3D|\!=3D)\s*(tru= e|false|$Lval)\b(.*)$/i) { + if ($check && + index($line, '=3D') >=3D 0 && + $line =3D~ /\b(?:true|false)\b/i && + $line =3D~ /\+\s*(.*?)\b(true|false|$Lval)\s*(=3D=3D|\!=3D)\s*(tru= e|false|$Lval)\b(.*)$/i) { my $lead =3D $1; my $arg =3D $2; my $test =3D $3; @@ -8033,7 +8049,8 @@ sub process { } =20 # check for uninitialized pointers with __free attribute - while ($line =3D~ /\*\s*($Ident)\s+__free\s*\(\s*$Ident\s*\)\s*[,;]/g)= { + while (index($line, '__free') >=3D 0 && + $line =3D~ /\*\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); }