[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220509065445.3912334-2-alviro.iskandar@gnuweeb.org>
Date: Mon, 9 May 2022 06:54:45 +0000
From: Alviro Iskandar Setiawan <alviro.iskandar@...weeb.org>
To: Ammar Faizi <ammarfaizi2@...weeb.org>
Cc: GNU/Weeb Mailing List <gwml@...r.gnuweeb.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
Alviro Iskandar Setiawan <alviro.iskandar@...weeb.org>
Subject: [PATCH 1/1] tools/nolibc/stdio: Add format attribute to enable printf warnings
When we use printf and fprintf functions from nolibc, we don't get any
warning from the compiler if we have wrong arguments, for example the
following calls will compile silently:
```
printf("%s %s\n", "aaa");
fprintf(stdout, "%s %s\n", "xxx", 1);
```
Those calls are undefined behavior. We can catch it at compile time by
adding format attribute to those function declarations. After this
patch, we get the following warnings:
```
warning: format `%s` expects a matching `char *` argument [-Wformat=]
warning: format `%s` expects argument of type `char *`, but argument 4 has type `int` [-Wformat=]
```
Signed-off-by: Alviro Iskandar Setiawan <alviro.iskandar@...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..009dd6ae68f2 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)) __attribute__((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)) __attribute__((format(printf, 1, 2)))
int printf(const char *fmt, ...)
{
va_list args;
--
Alviro Iskandar Setiawan
Powered by blists - more mailing lists