https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110834

            Bug ID: 110834
           Summary: Incorrect format-nonliteral warning when wrapping a
                    printf-family function using __builtin_va_arg_pack
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ksperling at apple dot com
  Target Milestone: ---

Created attachment 55650
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55650&action=edit
pre-processed example code

When wrapping a printf-family function (e.g. printf, sprintf, …) with an inline
wrapper that uses __builtin_va_arg_pack() to forward the format arguments a
format-nonliteral warning is incorrectly generated, even thought the format
string argument is annotated with the correct format attribute, e.g. this code

  #include <stdio.h>

  __inline__ __attribute__((__always_inline__, __format__(printf, 1, 2)))
  int myprintf(char const *fmt, ...)
  {
    return printf(fmt, __builtin_va_arg_pack());
  }

results in the following warning (or error with -Werror)

  $ gcc -c -Wformat -Werror=format-nonliteral format_va_arg_pack.c 
  format_va_arg_pack.c: In function ‘myprintf’:
  format_va_arg_pack.c:6:5: error: format not a string literal, argument types
not checked [-Werror=format-nonliteral]
   6 | return printf(fmt, __builtin_va_arg_pack());
   | ^~~~~~
  cc1: some warnings being treated as errors

The gcc output below and the attached .i file were generated with gcc 10.2.1 on
Debian, but I have verified that the issue also reproduces on gcc 13.2 and gcc
trunk on godbolt.org (https://godbolt.org/z/q6xMs5481).

For a real-world manifestation of this issue, see
https://github.com/openwrt/openwrt/issues/13016 where this issue is triggered
by wrapper functions of this style that are part of the fortify-headers library
(https://git.2f30.org/fortify-headers/).


Full compiler output with -v:

Using built-in specs.
COLLECT_GCC=gcc
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6'
--with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-10
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--enable-libphobos-checking=release --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-gcn/usr,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20210110 (Debian 10.2.1-6) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-Wformat=1'
'-Werror=format-nonliteral' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/10/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu
format_va_arg_pack.c -mtune=generic -march=x86-64 -Wformat=1
-Werror=format-nonliteral -fpch-preprocess -fasynchronous-unwind-tables -o
format_va_arg_pack.i
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/10/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/10/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-c' '-Wformat=1'
'-Werror=format-nonliteral' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/10/cc1 -fpreprocessed format_va_arg_pack.i
-quiet -dumpbase format_va_arg_pack.c -mtune=generic -march=x86-64 -auxbase
format_va_arg_pack -Wformat=1 -Werror=format-nonliteral -version
-fasynchronous-unwind-tables -o format_va_arg_pack.s
GNU C17 (Debian 10.2.1-6) version 10.2.1 20210110 (x86_64-linux-gnu)
        compiled by GNU C version 10.2.1 20210110, GMP version 6.2.1, MPFR
version 4.1.0, MPC version 1.2.0, isl version isl-0.23-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C17 (Debian 10.2.1-6) version 10.2.1 20210110 (x86_64-linux-gnu)
        compiled by GNU C version 10.2.1 20210110, GMP version 6.2.1, MPFR
version 4.1.0, MPC version 1.2.0, isl version isl-0.23-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 1f803793fa2e3418c492b25e7d3eac2f
format_va_arg_pack.c: In function ‘myprintf’:
format_va_arg_pack.c:6:5: error: format not a string literal, argument types
not checked [-Werror=format-nonliteral]
    6 |     return printf(fmt, __builtin_va_arg_pack());
      |     ^~~~~~
cc1: some warnings being treated as errors

Reply via email to