Skip to content

Commit 823b794

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add check for sscanf without return use
Naked use sscanf can be troublesome because the pointed to variables may not have been set. Add a warning when the sscanf return value is not used. For now, do not add __must_check to the sscanf prototype because that will cause a couple of hundred new warnings when compiling a kernel. Signed-off-by: Joe Perches <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2b7ab45 commit 823b794

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

scripts/checkpatch.pl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,6 +4019,23 @@ sub process {
40194019
}
40204020
}
40214021

4022+
# check for naked sscanf
4023+
if ($^V && $^V ge 5.10.0 &&
4024+
defined $stat &&
4025+
$stat =~ /\bsscanf\b/ &&
4026+
($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4027+
$stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4028+
$stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4029+
my $lc = $stat =~ tr@\n@@;
4030+
$lc = $lc + $linenr;
4031+
my $stat_real = raw_line($linenr, 0);
4032+
for (my $count = $linenr + 1; $count <= $lc; $count++) {
4033+
$stat_real = $stat_real . "\n" . raw_line($count, 0);
4034+
}
4035+
WARN("NAKED_SSCANF",
4036+
"unchecked sscanf return value\n" . "$here\n$stat_real\n");
4037+
}
4038+
40224039
# check for new externs in .h files.
40234040
if ($realfile =~ /\.h$/ &&
40244041
$line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {

0 commit comments

Comments
 (0)