Skip to contentSkip to conversation

scripts: recordmcount: fix incorrect use of sprintf

PATCH In review Colin King 4 participants 4 messages Dec 30, 2015 – Jan 6, 2016

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 scripts: recordmcount: fix incorrect use of sprintf

From: Colin Ian King <[email protected]>

Fix build warning:

scripts/recordmcount.c:589:4: warning: format not a string literal and no format arguments [-Wformat-security]

    sprintf("%s: failed\n", file);
Fixes a50bd43935586 ("ftrace/scripts: Have recordmcount copy the object file")
---
 scripts/recordmcount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/recordmcount.c +1 -1
index 301d70b..e1675927 100644
@@ -586,7 +586,7 @@ main(int argc, char *argv[])
 			do_file(file);
 			break;
 		case SJ_FAIL:    /* error in do_file or below */
-			sprintf("%s: failed\n", file);
+			fprintf(stderr, "%s: failed\n", file);
 			++n_error;
 			break;
 		case SJ_SUCCEED:    /* premature success */
Signature · 1 line
2.6.4
#1 lore raw
Quoted 19 lines · On Wed, 30 Dec 2015 23:06:41 +0000 Colin King <[email protected]> wrote:

From: Colin Ian King <[email protected]>

Fix build warning:

scripts/recordmcount.c:589:4: warning: format not a string literal and no format arguments [-Wformat-security]

    sprintf("%s: failed\n", file);
Fixes a50bd43935586 ("ftrace/scripts: Have recordmcount copy the object file")
---
 scripts/recordmcount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/recordmcount.c +0 -0
index 301d70b..e1675927 100644

scripts/recordmcount.c main(int argc, char *argv[]) · line 589

Quoted 5 lines
scripts/recordmcount.c +1 -1
@@ -586,7 +586,7 @@ main(int argc, char *argv[])
 			do_file(file);
 			break;
 		case SJ_FAIL:    /* error in do_file or below */
-			sprintf("%s: failed\n", file);
+			fprintf(stderr, "%s: failed\n", file);

Paper bag bug. I'm not sure how this passed my tests? My tests check for warnings. And I even got a "BUILD SUCCESS" from Fengguang Wu's kbuild test robot.

I'll apply it, test it, and push it out soon.

Thanks!

-- Steve

Quoted 3 lines
scripts/recordmcount.c +0 -0
 			++n_error;
 			break;
 		case SJ_SUCCEED:    /* premature success */
#2 lore raw

Hi Steven,

Quoted 17 lines · Steven Rostedt
Quoted 14 lines · On Wed, 30 Dec 2015 23:06:41 +0000 Colin King <[email protected]> wrote:

From: Colin Ian King <[email protected]>

Fix build warning:

scripts/recordmcount.c:589:4: warning: format not a string literal and no format arguments [-Wformat-security]

    sprintf("%s: failed\n", file);
Fixes a50bd43935586 ("ftrace/scripts: Have recordmcount copy the object file")
---
 scripts/recordmcount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

scripts/recordmcount.c main(int argc, char *argv[]) · line 589

Quoted 10 lines
scripts/recordmcount.c +1 -1
index 301d70b..e1675927 100644
@@ -586,7 +586,7 @@ main(int argc, char *argv[])
 			do_file(file);
 			break;
 		case SJ_FAIL:    /* error in do_file or below */
-			sprintf("%s: failed\n", file);
+			fprintf(stderr, "%s: failed\n", file);

Paper bag bug. I'm not sure how this passed my tests? My tests check for warnings. And I even got a "BUILD SUCCESS" from Fengguang Wu's kbuild test robot.

Because the error will only show up on "gcc -Wformat-security".

It tend to raise false positives, so Kees setup a dedicated tree which enables -Wformat-security as well as quieting the common false positives. In that way Kees can catch such problems from time to time, however the limitation is, only upstreamed code can be tested in Kees' tree.

Quoted 1 line

I'll apply it, test it, and push it out soon.

Thanks,
Fengguang

#3 lore raw
Quoted 35 lines · Fengguang Wu

Hi Steven,

Quoted 17 lines · Steven Rostedt
Quoted 14 lines · On Wed, 30 Dec 2015 23:06:41 +0000 Colin King <[email protected]> wrote:

From: Colin Ian King <[email protected]>

Fix build warning:

scripts/recordmcount.c:589:4: warning: format not a string literal and no format arguments [-Wformat-security]

    sprintf("%s: failed\n", file);
Fixes a50bd43935586 ("ftrace/scripts: Have recordmcount copy the object file")
---
 scripts/recordmcount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

scripts/recordmcount.c main(int argc, char *argv[]) · line 589

Quoted 10 lines
scripts/recordmcount.c +1 -1
index 301d70b..e1675927 100644
@@ -586,7 +586,7 @@ main(int argc, char *argv[])
                    do_file(file);
                    break;
            case SJ_FAIL:    /* error in do_file or below */
-                   sprintf("%s: failed\n", file);
+                   fprintf(stderr, "%s: failed\n", file);

Paper bag bug. I'm not sure how this passed my tests? My tests check for warnings. And I even got a "BUILD SUCCESS" from Fengguang Wu's kbuild test robot.

Quoted 1 line

Because the error will only show up on "gcc -Wformat-security".

I wish GCC were smarter about this. I would have hoped at least ONE of the various -Wformat... options would warn about "you did not actually specify a buffer for sprintf". But yeah, this comes from -Wformat-security (a subset of -Wformat-nonliteral), but can't be turned on by default, as mentioned by Fengguang.

Quoted 5 lines

It tend to raise false positives, so Kees setup a dedicated tree which enables -Wformat-security as well as quieting the common false positives. In that way Kees can catch such problems from time to time, however the limitation is, only upstreamed code can be tested in Kees' tree.

-Kees

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security