From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [180.242.99.67]) by gnuweeb.org (Postfix) with ESMTPSA id 6CFA17E46C; Thu, 19 May 2022 17:21:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1652980909; bh=U+32yynyQuIUhRnsYtaxv6b9DYu44J416tkLENqnFlk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EW60Ay+9+/uG7TipPtUBtVq+c9U3PA8bNcrfVDF6vIT/QTXkb6KGnF5RgyOJgoaBC HaJ+nuWxSScNhZjpRK9Ufmo6f/DvfJ8qvlj1zoV5qtfnXh6+mojxEqxSWoiS7Fnuud swCOWOfBMDFFJOI+EiGz/vMK7JJsneHIDFTt3paUfce64yiTbV39x9WuW+9nHfb0ya uq1JW9EEliKT9HfLg7cuOBzt8sW4rqSu3oS4o9oUqL7bxNZrnNR9XdQKKja2SRoIX8 iVVnZiSxlfqsKsDFW66E/G0wRHZPPyQj5N/j7q8E/1g8JUO84kXlKmTRqYMw0HyjA2 rx/sPmqALsbEg== From: Ammar Faizi To: "Paul E. McKenney" , Willy Tarreau Cc: Alviro Iskandar Setiawan , Ammar Faizi , Linux Kernel Mailing List , GNU/Weeb Mailing List , Facebook Kernel Team Subject: [PATCH v1 2/2] tools/nolibc/stdio: Add format attribute to enable printf warnings Date: Fri, 20 May 2022 00:21:16 +0700 Message-Id: <20220519172116.283687-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220519172116.283687-1-ammarfaizi2@gnuweeb.org> References: <20220519172116.283687-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: From: Alviro Iskandar Setiawan 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 Signed-off-by: Ammar Faizi --- 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