msys2 / MINGW-packages

Package scripts for MinGW-w64 targets to build under MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
2.28k stars 1.22k forks source link

gcc: `__attribute__ ((__format__ (__printf__, ...)))` doesn't recognize C99 specifiers #20660

Open Kojoley opened 6 months ago

Kojoley commented 6 months ago

Description / Steps to reproduce the issue

gcc -Wall -c bug.c -std=c99

#include <stdio.h>

#define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
#define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)

ATTRIBUTE_PRINTF_1 void _bfd_error_handler (const char *fmt, ...) {}

int main(void)
{
    printf("%zu\n", sizeof(0));
    _bfd_error_handler("%zu\n", sizeof(0));
    return 0;
}

Expected behavior

No warnings

Actual behavior

bug.c: In function 'main':
bug.c:11:26: warning: unknown conversion type character 'z' in format [-Wformat=]
   11 |     _bfd_error_handler("%zu\n", sizeof(0));
      |                          ^
bug.c:11:24: warning: too many arguments for format [-Wformat-extra-args]
   11 |     _bfd_error_handler("%zu\n", sizeof(0));
      |                        ^~~~~~~

Verification

Windows Version

MINGW64_NT-10.0-19045

MINGW environments affected

Are you willing to submit a PR?

No response

Biswa96 commented 6 months ago

Try __attribute__((__format__ (gnu_printf, ..., ...)))

Kojoley commented 6 months ago

Try __attribute__((__format__ (gnu_printf, ..., ...)))

Thanks, I know that gnu_printf works (I've checked why regular printf doesn't produce a warning), I will definitely have an infinity amount of time and luck trying to submit mingw workaround to binutils and every other project.

Kojoley commented 6 months ago

File upstream bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114775.

Jamaika1 commented 3 months ago

I use gcc 11.5.0 with MinGW-w64 12.0.0 and have strange messages https://github.com/brechtsanders/winlibs_mingw/releases/download/11.5.0posix-12.0.0-msvcrt-r1/winlibs-x86_64-posix-seh-gcc-11.5.0-mingw-w64msvcrt-12.0.0-r1.zip

common.c: In function 'xavs2_malloc':
common.c:255:42: warning: unknown conversion type character 'z' in format [-Wformat=]
  255 |         fprintf(stderr, "malloc of size %zu failed\n", i_size);
      |                                          ^
common.c:255:25: warning: too many arguments for format [-Wformat-extra-args]
  255 |         fprintf(stderr, "malloc of size %zu failed\n", i_size);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parameters.c: In function 'xavs2_encoder_opt_set':
parameters.c:404:23: warning: '%d' directive writing between 1 and 10 bytes into a region of size 4 [-Wformat-overflow=]
  404 |         sprintf(str, "%d", ++i);
      |                       ^~
parameters.c:404:22: note: directive argument in the range [2, 2147483647]
  404 |         sprintf(str, "%d", ++i);
      |                      ^~~~
parameters.c:404:9: note: 'sprintf' output between 2 and 11 bytes into a destination of size 4
  404 |         sprintf(str, "%d", ++i);
      |         ^~~~~~~~~~~~~~~~~~~~~~~

In version 11.3.1 these errors were not present http://msystem.waw.pl/x265/mingw-gcc1131-20221227.7z

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114775 Which attributes should use for

void my_printf(const char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  printf(fmt, ap);
  va_end(ap);
}

so it will behave correctly with any combination of -std=c89, -std=c99, __USE_MINGW_ANSI_STDIO=0, __USE_MINGW_ANSI_STDIO=1, _UCRT

Andrew Pinski that is a good question but that is a question for the mingw forums really rather than inside gcc bugzilla.

What version of gcc/mingw should I use on Windows10? Why is winlibs gcc 11.5.0 working slowly? What it depends on?