public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Alviro Iskandar Setiawan <[email protected]>
Cc: GNU/Weeb Mailing List <[email protected]>,
	Linux Kernel Mailing List <[email protected]>
Subject: Re: [PATCH 1/1] tools/nolibc/stdio: Add format attribute to enable printf warnings
Date: Thu, 12 May 2022 23:14:35 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

I will send it to Willy soon.

Anyway, __attribute__ can have multiple arguments, so no need
to write it twice. I simplified it, the end result patch below...

Thank you!

---
 From 7998cda9acdbfec6d6ba73642c27d710996c27ed Mon Sep 17 00:00:00 2001
From: Alviro Iskandar Setiawan <[email protected]>
Subject: tools/nolibc/stdio: Add format attribute to enable printf warnings

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 <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
  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

      reply	other threads:[~2022-05-12 16:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-09  6:54 [PATCH 0/1] Add format attribute to enable printf warnings Alviro Iskandar Setiawan
2022-05-09  6:54 ` [PATCH 1/1] tools/nolibc/stdio: " Alviro Iskandar Setiawan
2022-05-12 16:14   ` Ammar Faizi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox