Skip to content

Commit 554e165

Browse files
Andy Whitcrofttorvalds
authored andcommitted
checkpatch: check for common memset parameter issues against statments
Move the memset checks over to work against the statement. Also add checks for 0 and 1 used as lengths. Generally these indicate badly ordered parameters. Signed-off-by: Andy Whitcroft <apw@canonical.com> Cc: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f74bd19 commit 554e165

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

scripts/checkpatch.pl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,6 +3120,28 @@ sub process {
31203120
"Avoid line continuations in quoted strings\n" . $herecurr);
31213121
}
31223122

3123+
# Check for misused memsets
3124+
if (defined $stat && $stat =~ /\bmemset\s*\((.*)\)/s) {
3125+
my $args = $1;
3126+
3127+
# Flatten any parentheses and braces
3128+
while ($args =~ s/\([^\(\)]*\)/10/s ||
3129+
$args =~ s/\{[^\{\}]*\}/10/s ||
3130+
$args =~ s/\[[^\[\]]*\]/10/s)
3131+
{
3132+
}
3133+
# Extract the simplified arguments.
3134+
my ($ms_addr, $ms_val, $ms_size) =
3135+
split(/\s*,\s*/, $args);
3136+
if ($ms_size =~ /^(0x|)0$/i) {
3137+
ERROR("MEMSET",
3138+
"memset size is 3rd argument, not the second.\n" . $herecurr);
3139+
} elsif ($ms_size =~ /^(0x|)1$/i) {
3140+
WARN("MEMSET",
3141+
"single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . $herecurr);
3142+
}
3143+
}
3144+
31233145
# check for new externs in .c files.
31243146
if ($realfile =~ /\.c$/ && defined $stat &&
31253147
$stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
@@ -3291,12 +3313,6 @@ sub process {
32913313
WARN("EXPORTED_WORLD_WRITABLE",
32923314
"Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
32933315
}
3294-
3295-
# Check for memset with swapped arguments
3296-
if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3297-
ERROR("MEMSET",
3298-
"memset size is 3rd argument, not the second.\n" . $herecurr);
3299-
}
33003316
}
33013317

33023318
# If we have no input at all, then there is nothing to report on

0 commit comments

Comments
 (0)