Skip to contentSkip to conversation

checkpatch: Check for 0-length and 1-element arrays

PATCH Applied Kees Cook 3 participants 4 messages Jun 1, 2023 – Jun 2, 2023 Checkpatch

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v5 1/1 checkpatch: Check for 0-length and 1-element arrays

Fake flexible arrays have been deprecated since last millennium. Proper C99 flexible arrays must be used throughout the kernel so CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array bounds checking.

---
v5: - add __packed to outer struct regex too (joe)
v4: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]

---
 scripts/checkpatch.pl | 10 ++++++++++
 1 file changed, 10 insertions(+)
scripts/checkpatch.pl +10 -0
index 30b0b4fdb3bf..7bfa4d39d17f 100755
@@ -7430,6 +7430,16 @@ sub process {
 			}
 		}
 
+# check for array definition/declarations that should use flexible arrays instead
+		if ($sline =~ /^[\+ ]\s*\}(?:\s*__packed)?\s*;\s*$/ &&
+		    $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
+			if (ERROR("FLEXIBLE_ARRAY",
+				  "Use C99 flexible arrays - see https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays\n" . $hereprev) &&
+			    $1 == '0' && $fix) {
+				$fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;
+			}
+		}
+
 # nested likely/unlikely calls
 		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
 			WARN("LIKELY_MISUSE",
Signature · 1 line
2.34.1
maintainer Acked-by on v5 1/1
#1 lore raw
Quoted 5 lines · Kees Cook

Fake flexible arrays have been deprecated since last millennium. Proper C99 flexible arrays must be used throughout the kernel so CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array bounds checking.

Quoted 7 lines
Quoted 31 lines

---
v5: - add __packed to outer struct regex too (joe)
v4: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]

---
 scripts/checkpatch.pl | 10 ++++++++++
 1 file changed, 10 insertions(+)
scripts/checkpatch.pl +10 -0
index 30b0b4fdb3bf..7bfa4d39d17f 100755
@@ -7430,6 +7430,16 @@ sub process {
 			}
 		}
 
+# check for array definition/declarations that should use flexible arrays instead
+		if ($sline =~ /^[\+ ]\s*\}(?:\s*__packed)?\s*;\s*$/ &&
+		    $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
+			if (ERROR("FLEXIBLE_ARRAY",
+				  "Use C99 flexible arrays - see https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays\n" . $hereprev) &&
+			    $1 == '0' && $fix) {
+				$fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;
+			}
+		}
+
 # nested likely/unlikely calls
 		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
 			WARN("LIKELY_MISUSE",
#2 lore raw
In reply to Kees Cook
Quoted 9 lines · On Thu, 1 Jun 2023 09:07:46 -0700 Kees Cook <[email protected]> wrote:

Fake flexible arrays have been deprecated since last millennium. Proper C99 flexible arrays must be used throughout the kernel so CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array bounds checking.

Cc Andy Whitcroft <[email protected]>
Cc Dwaipayan Ray <[email protected]>
Cc Lukas Bulwahn <[email protected]>

You didn't checkpatch your checkpatch patch!

WARNING: Non-standard signature: Fixed-by:
#248:

I'll do my usual

[[email protected]: handle __packed for srtucts]

maintainer
#3 lore raw
Quoted 14 lines · Andrew Morton
Quoted 9 lines · On Thu, 1 Jun 2023 09:07:46 -0700 Kees Cook <[email protected]> wrote:

Fake flexible arrays have been deprecated since last millennium. Proper C99 flexible arrays must be used throughout the kernel so CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array bounds checking.

Cc Andy Whitcroft <[email protected]>
Cc Dwaipayan Ray <[email protected]>
Cc Lukas Bulwahn <[email protected]>

You didn't checkpatch your checkpatch patch!

Quoted 7 lines

WARNING: Non-standard signature: Fixed-by:
#248:

I'll do my usual

[[email protected]: handle __packed for srtucts]

With that spelling?

Anyway, I went back and forth a few times with Kees giving suggestions and corrections before sending it on to you for more than just __packed.

I didn't (and still don't) care much about anything other than the code.

cheers, Joe