Skip to contentSkip to conversation

checkpatch: Warn about 0-length and 1-element arrays

PATCH Superseded Kees Cook 2 participants 3 messages May 26, 2023 – May 27, 2023 Checkpatch

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 checkpatch: Warn about 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.

---
v2: find using "end of struct" marking instead of $context_function (joe)
v1: https://lore.kernel.org/all/[email protected]/

---
 scripts/checkpatch.pl | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
scripts/checkpatch.pl +15 -0
index 30b0b4fdb3bf..cfa19bfc857c 100755
@@ -7430,6 +7430,21 @@ sub process {
 			}
 		}
 
+# check for array definition/declarations that should use flexible arrays instead
+		if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ &&
+		    $prevline =~ /^\+\s*(?:(?:struct|union|enum)\s+$Ident|\}|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
+			if ($1 == '0') {
+				if (WARN("ZERO_LENGTH_ARRAY",
+					 "Use C99 flexible arrays instead of zero-length arrays - see https://github.com/KSPP/linux/issues/78\n" . $hereprev) &&
+				    $fix) {
+					$fixed[$fixlinenr - 1] =~ s/\[0\]/[]/g;
+				}
+			} else {
+				WARN("ONE_ELEMENT_ARRAY",
+				     "Use C99 flexible arrays instead of one-element arrays - see https://github.com/KSPP/linux/issues/79\n" . $hereprev);
+			}
+		}
+
 # 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
#1 lore raw
Quoted 4 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.

[]

scripts/checkpatch.pl sub process { · line 7435

Quoted 9 lines
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl +3 -0
@@ -7430,6 +7430,21 @@ sub process {
 			}
 		}
 
+# check for array definition/declarations that should use flexible arrays instead
+		if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ &&
+		    $prevline =~ /^\+\s*(?:(?:struct|union|enum)\s+$Ident|\}|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {

I think this is overly complicated and not necessary

		    $prevline =~ /^\+\s*$Type\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {

should work no?

($Type already includes this from @typeList):

	qr{struct\s+$Ident},
	qr{union\s+$Ident},
	qr{enum\s+$Ident},

scripts/checkpatch.pl sub process { · line 7440

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+			if ($1 == '0') {
+				if (WARN("ZERO_LENGTH_ARRAY",
+					 "Use C99 flexible arrays instead of zero-length arrays - see https://github.com/KSPP/linux/issues/78\n" . $hereprev) &&
+				    $fix) {
+					$fixed[$fixlinenr - 1] =~ s/\[0\]/[]/g;

And this $fix doesn't work if the line is struct foo bar[ 0 ]; and the use of /g is odd.

Because the message is a WARN and not an ERR, please use "Prefer/over" and not "Use/instead of"

					$fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;

scripts/checkpatch.pl sub process { · line 7445

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+				}
+			} else {
+				WARN("ONE_ELEMENT_ARRAY",
+				     "Use C99 flexible arrays instead of one-element arrays - see https://github.com/KSPP/linux/issues/79\n" . $hereprev);
+			}

And this could have a $fix change too

				if (WARN("ONE_ELEMENT_ARRAY",
					 ...) &&
				    $fix) {
					$fixed[$fixlinenr - 1] =~ s/\[\s*1\s*\]/[]/;
Quoted 5 lines
scripts/checkpatch.pl +2 -0
+		}
+
 # nested likely/unlikely calls
 		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
 			WARN("LIKELY_MISUSE",
author
#2 lore raw
Quoted 18 lines · Joe Perches
Quoted 4 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.

[]

scripts/checkpatch.pl sub process { · line 7435

Quoted 9 lines
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl +3 -0
@@ -7430,6 +7430,21 @@ sub process {
 			}
 		}
 
+# check for array definition/declarations that should use flexible arrays instead
+		if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ &&
+		    $prevline =~ /^\+\s*(?:(?:struct|union|enum)\s+$Ident|\}|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {

I think this is overly complicated and not necessary

Quoted 8 lines
		    $prevline =~ /^\+\s*$Type\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {

should work no?

($Type already includes this from @typeList):

	qr{struct\s+$Ident},
	qr{union\s+$Ident},
	qr{enum\s+$Ident},

Hm, I didn't when I originally tried it. I will double-check.

Quoted 9 lines

scripts/checkpatch.pl sub process { · line 7440

Quoted 6 lines
scripts/checkpatch.pl +5 -0
+			if ($1 == '0') {
+				if (WARN("ZERO_LENGTH_ARRAY",
+					 "Use C99 flexible arrays instead of zero-length arrays - see https://github.com/KSPP/linux/issues/78\n" . $hereprev) &&
+				    $fix) {
+					$fixed[$fixlinenr - 1] =~ s/\[0\]/[]/g;

And this $fix doesn't work if the line is struct foo bar[ 0 ]; and the use of /g is odd.

Thanks!

Quoted 2 lines

Because the message is a WARN and not an ERR, please use "Prefer/over" and not "Use/instead of"

I will change to ERR, then. We must not use [0]-sized arrays ever.

Quoted 10 lines
					$fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;

scripts/checkpatch.pl sub process { · line 7445

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+				}
+			} else {
+				WARN("ONE_ELEMENT_ARRAY",
+				     "Use C99 flexible arrays instead of one-element arrays - see https://github.com/KSPP/linux/issues/79\n" . $hereprev);
+			}

And this could have a $fix change too

Quoted 4 lines
				if (WARN("ONE_ELEMENT_ARRAY",
					 ...) &&
				    $fix) {
					$fixed[$fixlinenr - 1] =~ s/\[\s*1\s*\]/[]/;

Almost never is it possible to only fix the struct when it's a 1-element array (due to the impact of the size changes that be present in other code), so I left it out.

I'll send a v3.

-Kees

Signature · 1 line
Kees Cook