Menu

#407 printf bug in mingw64

v1.0 (example)
closed
nobody
None
5
2014-09-05
2014-06-29
Lelanthran
No

Strange bug when using both %zu and %s in the same formatting string:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
   const char const input[] = "Hello World";
   char *str = malloc (33517 + 1);
   for (size_t i=0; i<33517; i++) {
      str[i % sizeof input] = input [i % sizeof input];
   }
   str[33517] = 0;
   printf ("%zu bytes\n%s\n\n", strlen (str), str);
   return 0;
}

The output never appears (it crashes and windows displays a dialog box) so I've included the gdb output:

bash$ gcc -std=c99 -DUSE_MINGW_ANSI_STDIO=1 -o test.exe mingw64-printf-problem.c
bash$ gdb ./test
GNU gdb (GDB) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...done.
(gdb) run
Starting program: C:\tmp\test.exe
[New Thread 3076.0x69c]

Program received signal SIGSEGV, Segmentation fault.
0x000007feff95716e in msvcrt!_beginthreadex ()
   from C:\Windows\system32\msvcrt.dll
(gdb) bt
#0  0x000007feff95716e in msvcrt!_beginthreadex ()
   from C:\Windows\system32\msvcrt.dll
#1  0x000007feff957a1d in printf () from C:\Windows\system32\msvcrt.dll
#2  0x00000000004015ce in main ()
(gdb)

Removing either the %zu (and its argument) or the %s (and its argument) makes it rnu successfully, although instead of the string length a zu is displayed instead.

Discussion

  • Ozkan Sezer

    Ozkan Sezer - 2014-06-29

    I think you missed two leading underscores from -DUSE_MINGW_ANSI_STDIO=1, i.e. you need "-D__USE_MINGW_ANSI_STDIO=1"

     
    • Lelanthran

      Lelanthran - 2014-06-29

      Thanks, that's fixed it. The example code I saw did not have two leading underscores to tell mingw64 to use its own runtime

       
  • Ozkan Sezer

    Ozkan Sezer - 2014-06-29

    OK then, closing.

    If the example code is from our site/documentation, then please point us to it so that we can correct it.

     
    • Lelanthran

      Lelanthran - 2014-06-29

      When I searched the tickets for printf bugs with respect to %zu, one of the first relevant results was this: https://sourceforge.net/p/mingw-w64/bugs/377/

      This is where I got the #define without the two leading underscores.

      Once again, thanks for helping me.

       
  • Ozkan Sezer

    Ozkan Sezer - 2014-06-29
    • status: open --> closed
     

Log in to post a comment.