Skip to contentSkip to conversation

checkpatch: Add check for sscanf without return use

PATCH In review Joe Perches 4 participants 6 messages Oct 7, 2013 – Oct 8, 2013 Checkpatch

lore
Share & tools
lore

Joe Perches

author maintainer
#0 lore raw

PATCH 1/1 checkpatch: Add check for sscanf without return use

Naked use sscanf can be troublesome.
Add a warning when the sscanf return value is not used.

---
 scripts/checkpatch.pl | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
scripts/checkpatch.pl +17 -0
index 23d55bf..ba8af49 100755
@@ -4012,6 +4012,23 @@ sub process {
 			}
 		}
 
+# check for naked sscanf
+		if ($^V && $^V ge 5.10.0 &&
+		    defined $stat &&
+		    $stat =~ /\bsscanf\b/ &&
+		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
+		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:==|\!=)/ &&
+		     $stat !~ /(?:==|\!=)\s*\bsscanf\s*$balanced_parens/)) {
+			my $lc = $stat =~ tr@\n@@;
+			$lc = $lc + $linenr;
+			my $stat_real = raw_line($linenr, 0);
+		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
+				$stat_real = $stat_real . "\n" . raw_line($count, 0);
+			}
+			WARN("NAKED_SSCANF",
+			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
+		}
+
 # check for new externs in .h files.
 		if ($realfile =~ /\.h$/ &&
 		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
#1 lore raw
Quoted 1 line · On Mon, 07 Oct 2013 16:45:23 -0700 Joe Perches <[email protected]> wrote:

Naked use sscanf can be troublesome.

It would be helpful to expand on "troublesome"?

Quoted 1 line

Add a warning when the sscanf return value is not used.

Maybe it should be __must_check?

author maintainer
#2 lore raw
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Mon, 07 Oct 2013 16:45:23 -0700 Joe Perches <[email protected]> wrote:

Naked use sscanf can be troublesome.

It would be helpful to expand on "troublesome"?

Dunno, you either know or the couple of paragraphs it takes to explain with examples won't really be too helpful in a commit log.

Quoted 2 lines
Quoted 1 line

Add a warning when the sscanf return value is not used.

Maybe it should be __must_check?

Maybe.

That might be easier but there'll be a couple hundred new build warnings.

#3 lore raw
Quoted 10 lines · Joe Perches
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Mon, 07 Oct 2013 16:45:23 -0700 Joe Perches <[email protected]> wrote:

Naked use sscanf can be troublesome.

It would be helpful to expand on "troublesome"?

Dunno, you either know or the couple of paragraphs it takes to explain with examples won't really be too helpful in a commit log.

It would be helpful to me, since I don't write code with sscanf every day. A URL to an LKML description or something would be enough if the commit log size is the concern.

Bjorn

author maintainer
#4 lore raw
Quoted 8 lines · Bjorn Helgaas
Quoted 7 lines · Joe Perches
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Mon, 07 Oct 2013 16:45:23 -0700 Joe Perches <[email protected]> wrote:

Naked use sscanf can be troublesome.

It would be helpful to expand on "troublesome"?

Quoted 7 lines
Quoted 3 lines

Dunno, you either know or the couple of paragraphs it takes to explain with examples won't really be too helpful in a commit log.

It would be helpful to me, since I don't write code with sscanf every day. A URL to an LKML description or something would be enough if the commit log size is the concern.

I sent a V2 patch to Andrew and mm using this commit log:

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.

---
V2: Use the $Compare variable instead of just == or !=
There are some tests that use < and >

#5 lore raw

2013/10/7 Joe Perches <[email protected]>:

Quoted 9 lines · Bjorn Helgaas
Quoted 4 lines · Joe Perches
Quoted 3 lines · Andrew Morton
Quoted 2 lines · On Mon, 07 Oct 2013 16:45:23 -0700 Joe Perches <[email protected]> wrote:

Add a warning when the sscanf return value is not used.

It'll be nice.