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=-1.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,NO_DNS_FOR_FROM, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from [192.168.88.87] (unknown [180.242.99.67]) by gnuweeb.org (Postfix) with ESMTPSA id 02E317F628; Thu, 12 May 2022 16:14:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1652372089; bh=3U63MfFe6NNlA7+9OBTVpsTNg4BhGFmC0heC1eTNPn0=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=HHlioj8YxvRo7x+f4nbXrHcO5+g3bxMx7SRXW142HTO3mHdehBK/a8rmh2dZeFKYn XhLlqi/qE2AvWMJEUpVX/S50PYuFgakb+Ob9XOnwWRRakPLYjEmQCB8q1IP3OGCuwj +vZ883/d311UUy85Yab3+b8Gxj1x8I+Q7KoIgR6INIwJonBawsKD4oC/mnpDE5mvAt MvBLJ4yXTesfbMl07SLVYdOO/3LXcLSJm1T9ldruIs/qN0o7Wol4jwofXm1wM79NJf RIBIwIDE78ze1OKHfdxj89R0jqb5MXWB1boS8Ab9Q0x2Wtb6h3j5JhEkiAX8PskeOs 4BWfm9SPsUmSQ== Message-ID: Date: Thu, 12 May 2022 23:14:35 +0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: Re: [PATCH 1/1] tools/nolibc/stdio: Add format attribute to enable printf warnings Content-Language: en-US To: Alviro Iskandar Setiawan Cc: GNU/Weeb Mailing List , Linux Kernel Mailing List References: <20220509065445.3912334-1-alviro.iskandar@gnuweeb.org> <20220509065445.3912334-2-alviro.iskandar@gnuweeb.org> From: Ammar Faizi In-Reply-To: <20220509065445.3912334-2-alviro.iskandar@gnuweeb.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: 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 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 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