checkpatch: Check for 0-length and 1-element arrays
PATCH v4 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.
---
v4:
- combine errors (joe)
- switch to kerndoc url (joe)
- add __packed for struct matching (joe)
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
@@ -7430,6 +7430,16 @@ sub process { } } +# check for array definition/declarations that should use flexible arrays instead + if ($sline =~ /^[\+ ]\s*}\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
Joe Perches
maintainerQuoted 29 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_SOURCEandCONFIG_UBSAN_BOUNDScan provide proper array bounds checking.Cc Andy Whitcroft <[email protected]>Cc Joe Perches <[email protected]>Cc Dwaipayan Ray <[email protected]>Cc Lukas Bulwahn <[email protected]>Cc Gustavo A. R. Silva <[email protected]>Signed-off-by Kees Cook <[email protected]>---
v4:
- combine errors (joe)
- switch to kerndoc url (joe)
- add__packedfor struct matching (joe)
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 +0
-0
scripts/checkpatch.pl sub process { · line 7435
Quoted 6 lines
scripts/checkpatch.pl +3
-0@@ -7430,6 +7430,16 @@ sub process { } } +# check for array definition/declarations that should use flexible arrays instead + if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ && + $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
__packed could be used when a struct is defined and so needs to also be added to $sline.
if ($sline =~ /^[\+ ]\s*}\s*(?:__packed\s*)?;\s*$/ &&
e.g.:
struct foo {
int a;
int b[0];
} __packed;Quoted 10 lines
scripts/checkpatch.pl +7
-0+ 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",
Quoted 33 lines · Joe Perches
Quoted 32 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_SOURCEandCONFIG_UBSAN_BOUNDScan provide proper array bounds checking.Cc Andy Whitcroft <[email protected]>Cc Joe Perches <[email protected]>Cc Dwaipayan Ray <[email protected]>Cc Lukas Bulwahn <[email protected]>Cc Gustavo A. R. Silva <[email protected]>Signed-off-by Kees Cook <[email protected]>---
v4:
- combine errors (joe)
- switch to kerndoc url (joe)
- add__packedfor struct matching (joe)
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 +0
-0@@ -7430,6 +7430,16 @@ sub process { } }
Quoted 6 lines
scripts/checkpatch.pl sub process { · line 7435
Quoted 3 lines
scripts/checkpatch.pl +3
-0+# check for array definition/declarations that should use flexible arrays instead + if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ && + $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
__packedcould be used when a struct is defined and so needs to also be added to $sline.
Agh. Yes, thank you. v5 on the way...
Signature · 1 line
Kees Cook