[PATCH v2 6/6] stdio-common: Suppress Clang warnings on scanf13.c

Sam James sam@gentoo.org
Sun Dec 22 22:26:14 GMT 2024


"H.J. Lu" <hjl.tools@gmail.com> writes:

> Suppress Clang warnings on stdio-common/scanf13.c:
>
> 1. Before this commit:
>
> scanf13.c:43:17: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>    43 |               "A%mS%10mls%4ml[bcd]%4mCB", &lsp1, &lsp2, &lsp3, &lsp4) != 4)
>       |                    ~~~~^
> scanf13.c:43:22: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>    43 |               "A%mS%10mls%4ml[bcd]%4mCB", &lsp1, &lsp2, &lsp3, &lsp4) != 4)
>       |                         ~~~~^
> scanf13.c:43:50: error: data argument not used by format string [-Werror,-Wformat-extra-args]
>    43 |               "A%mS%10mls%4ml[bcd]%4mCB", &lsp1, &lsp2, &lsp3, &lsp4) != 4)
>       |               ~~~~~~~~~~~~~~~~~~~~~~~~~~                ^
> scanf13.c:145:27: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>   145 |   if (sscanf (buf, "%2048mls%mlc", &lsp3, &lsp4) != 2)
>       |                     ~~~~~~^
> scanf13.c:145:31: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>   145 |   if (sscanf (buf, "%2048mls%mlc", &lsp3, &lsp4) != 2)
>       |                            ~~~^
> scanf13.c:145:43: error: data argument not used by format string [-Werror,-Wformat-extra-args]
>   145 |   if (sscanf (buf, "%2048mls%mlc", &lsp3, &lsp4) != 2)
>       |                    ~~~~~~~~~~~~~~         ^
> scanf13.c:161:31: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>   161 |   if (sscanf (buf, "%4mC%1500ml[dr/]%548ml[abc/d]%3mlc",
>       |                         ~~~~~~^
> scanf13.c:161:42: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>   161 |   if (sscanf (buf, "%4mC%1500ml[dr/]%548ml[abc/d]%3mlc",
>       |                                ~~~~~~~~~~^
> scanf13.c:161:53: error: invalid conversion specifier 'l' [-Werror,-Wformat-invalid-specifier]
>   161 |   if (sscanf (buf, "%4mC%1500ml[dr/]%548ml[abc/d]%3mlc",
>       |                                           ~~~~~~~~~~^
> scanf13.c:162:15: error: data argument not used by format string [-Werror,-Wformat-extra-args]
>   161 |   if (sscanf (buf, "%4mC%1500ml[dr/]%548ml[abc/d]%3mlc",
>       |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   162 |               &lsp1, &lsp2, &lsp3, &lsp4) != 4)
>       |                      ^
> 10 errors generated.
>
> 2. With DIAG_IGNORE_NEEDS_COMMENT_CLANG changes in stdio-common/scanf13.c:
>
> scanf13.c:28:40: error: 'sscanf' may overflow; destination buffer in argument 4 has size 8, but the corresponding specifier may require size 11 [-Werror,-Wfortify-source]
>    28 |               "A%ms%10ms%4m[bcd]%4mcB", &sp1, &sp2, &sp3, &sp4) != 4)
>       |                                               ^
> scanf13.c:94:34: error: 'sscanf' may overflow; destination buffer in argument 3 has size 8, but the corresponding specifier may require size 2049 [-Werror,-Wfortify-source]
>    94 |   if (sscanf (buf, "%2048ms%mc", &sp3, &sp4) != 2)
>       |                                  ^
> scanf13.c:110:61: error: 'sscanf' may overflow; destination buffer in argument 4 has size 8, but the corresponding specifier may require size 1501 [-Werror,-Wfortify-source]
>   110 |   if (sscanf (buf, "%4mc%1500m[dr/]%548m[abc/d]%3mc", &sp1, &sp2, &sp3, &sp4)
>       |                                                             ^
> scanf13.c:110:67: error: 'sscanf' may overflow; destination buffer in argument 5 has size 8, but the corresponding specifier may require size 549 [-Werror,-Wfortify-source]
>   110 |   if (sscanf (buf, "%4mc%1500m[dr/]%548m[abc/d]%3mc", &sp1, &sp2, &sp3, &sp4)
>       |                                                                   ^
> 4 errors generated.
>
> Co-Authored-By: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>

Reviewed-by: Sam James <sam@gentoo.org>

> ---
>  stdio-common/Makefile  | 2 ++
>  stdio-common/scanf13.c | 9 +++++++++
>  2 files changed, 11 insertions(+)
>
> diff --git a/stdio-common/Makefile b/stdio-common/Makefile
> index e5b4ce9e00..3f84de3e36 100644
> --- a/stdio-common/Makefile
> +++ b/stdio-common/Makefile
> @@ -646,6 +646,8 @@ CFLAGS-dprintf.c += $(config-cflags-wno-ignored-attributes)
>  CFLAGS-_itoa.o = $(no-stack-protector)
>  CFLAGS-_itoa.op = $(no-stack-protector)
>  
> +CFLAGS-scanf13.c += $(test-config-cflags-wno-fortify-source)
> +
>  # scanf18.c and scanf19.c test a deprecated extension which is no
>  # longer visible under most conformance levels; see the source files
>  # for more detail.
> diff --git a/stdio-common/scanf13.c b/stdio-common/scanf13.c
> index 60aa62a26f..65b1429720 100644
> --- a/stdio-common/scanf13.c
> +++ b/stdio-common/scanf13.c
> @@ -3,6 +3,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <wchar.h>
> +#include <libc-diag.h>
>  
>  int
>  main (void)
> @@ -20,6 +21,9 @@ main (void)
>    } while (0)
>  
>    setlocale (LC_ALL, "de_DE.UTF-8");
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wformat-invalid-specifier");
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wformat-extra-args");
>    if (sscanf ("A  \xc3\x84-\t\t\xc3\x84-abcdefbcd\t\xc3\x84-B",
>  	      "A%ms%10ms%4m[bcd]%4mcB", &sp1, &sp2, &sp3, &sp4) != 4)
>      FAIL ();
> @@ -57,6 +61,7 @@ main (void)
>  	FAIL ();
>        free (lsp4);
>      }
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>  
>    memset (buf, '/', sizeof (buf));
>    buf[0] = '\t';
> @@ -142,6 +147,9 @@ main (void)
>  	FAIL ();
>        free (lsp2);
>      }
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wformat-invalid-specifier");
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wformat-extra-args");
>    if (sscanf (buf, "%2048mls%mlc", &lsp3, &lsp4) != 2)
>      FAIL ();
>    else
> @@ -182,6 +190,7 @@ main (void)
>  	FAIL ();
>        free (lsp4);
>      }
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>  
>    return result;
>  }


More information about the Libc-alpha mailing list