tools/nolibc/stdio: Add format attribute to enable printf warnings
authorAlviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Thu, 19 May 2022 17:21:16 +0000 (00:21 +0700)
committerPaul E. McKenney <paulmck@kernel.org>
Mon, 20 Jun 2022 16:43:19 +0000 (09:43 -0700)
When we use printf and fprintf functions from the nolibc, we don't
get any warning from the compiler if we have the wrong arguments.
For example, the following calls will compile silently:
```
  printf("%s %s\n", "aaa");
  fprintf(stdout, "%s %s\n", "xxx", 1);
```
(Note the wrong arguments).

Those calls are undefined behavior. The compiler can help us warn
about the above mistakes by adding a `printf` format attribute to
those functions declaration. This patch adds it, and now it yields
these warnings for those mistakes:
```
  warning: format `%s` expects a matching `char *` argument [-Wformat=]
  warning: format `%s` expects argument of type `char *`, but argument 4 has type `int` [-Wformat=]
```

  [ ammarfaizi2: Simplify the attribute placement. ]

Signed-off-by: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/stdio.h

index 15dedf8d0902dfdb2a054da7b533ae1b0fa0e45f..a3cebc4bc3ac4d997431db4fd2ce72fb5fd64387 100644 (file)
@@ -273,7 +273,7 @@ int vfprintf(FILE *stream, const char *fmt, va_list args)
        return written;
 }
 
-static __attribute__((unused))
+static __attribute__((unused, format(printf, 2, 3)))
 int fprintf(FILE *stream, const char *fmt, ...)
 {
        va_list args;
@@ -285,7 +285,7 @@ int fprintf(FILE *stream, const char *fmt, ...)
        return ret;
 }
 
-static __attribute__((unused))
+static __attribute__((unused, format(printf, 1, 2)))
 int printf(const char *fmt, ...)
 {
        va_list args;