[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220519172116.283687-3-ammarfaizi2@gnuweeb.org>
Date: Fri, 20 May 2022 00:21:16 +0700
From: Ammar Faizi <ammarfaizi2@...weeb.org>
To: "Paul E. McKenney" <paulmck@...nel.org>, Willy Tarreau <w@....eu>
Cc: Alviro Iskandar Setiawan <alviro.iskandar@...weeb.org>,
Ammar Faizi <ammarfaizi2@...weeb.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
GNU/Weeb Mailing List <gwml@...r.gnuweeb.org>,
Facebook Kernel Team <kernel-team@...com>
Subject: [PATCH v1 2/2] tools/nolibc/stdio: Add format attribute to enable printf warnings
From: Alviro Iskandar Setiawan <alviro.iskandar@...weeb.org>
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@...weeb.org>
Signed-off-by: Ammar Faizi <ammarfaizi2@...weeb.org>
---
tools/include/nolibc/stdio.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 15dedf8d0902..a3cebc4bc3ac 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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;
--
Ammar Faizi
Powered by blists - more mailing lists