Hi,

the problem here is that Microsoft doesn't implement snprintf C99
conforming. When the buffer is not big enough, Microsoft's
implementation returns a negative value (see
http://msdn.microsoft.com/en-us/library/2ts7cx93%28v=vs.110%29.aspx).

Now mingw-w64 per default delegates a call to snprintf to Microsoft's C
runtime and hence you get the surprising result.

If you want to use an ISO C99 conforming implementation of the C
standard library, you have to define the Macro _ISOC99_SOURCE. Then
everything should work as expected.

Marcel

Am 01.08.2013 02:58, schrieb Luis Lavena:
> Hello folks,
> 
> As maintainer of RubyInstaller (binary distribution of Ruby on Windows), we
> opted to use Ruben's mingw-w64 binary packages as compiler toolchain and
> also as part of our DevKit, to allow users install and compile extensions
> to the language.
> 
> A few days ago, a user reported an interesting case that shows issues with
> snprintf:
> 
> https://github.com/oneclick/rubyinstaller/issues/186
> 
> The test case is simple, the following script:
> 
> #include <stdio.h>
> #include <string.h>
> #include <assert.h>
> 
> char small_buffer[10];
> char *small_text = "123";
> char *big_text = "12345678901234567890";
> 
> int main(int argc, char *argv[])
> {
>   int rc;
> 
>   // Text fits into buffer: snprintf returns length.
>   rc = snprintf(small_buffer, sizeof(small_buffer), small_text);
>   printf("small_text rc = %d\n", rc);
>   assert(rc == strlen(small_text));
> 
>   // Text too big for buffer: ANSI C99 snprintf should return required
> length.
>   // Gnu/Linux follows ANSI C99.
>   // MinGW is supposed to follow ANSI C99.
>   // Windows is broken and fails this test.
>   rc = snprintf(small_buffer, sizeof(small_buffer), big_text);
>   printf("big_text rc = %d\n", rc);
>   assert(rc > 0);
> 
>   return(0);
> }
> 
> 
> Compiled with mingw 4.7.2 (std=c99 or ANSI), this is the output:
> 
> C:\Users\Luis\Code\_sandbox\scripts>gcc --version
> gcc (GCC) 4.7.2
> 
> C:\Users\Luis\Code\_sandbox\scripts>test-472-mingw.exe
> small_text rc = 3
> big_text rc = 20
> 
> While either Ruben's or mingw-builds resulted in:
> 
> C:\Users\Luis\Code\_sandbox\scripts>test-472-32.exe
> small_text rc = 3
> big_text rc = -1
> Assertion failed!
> 
> Program: C:\Users\Luis\Code\_sandbox\scripts\test-472-32.exe
> File: test.snprintf.c, Line 24
> 
> Expression: rc > 0
> 
> Tested against:
> 
> Ruben's 4.7.2 (packages):
> i686-w64-mingw32-gcc-4.7.2-release-win32_rubenvb.7z
> i686_64-w64-mingw32-mingw-w64-update-v2.0.7_rubenvb.7z
> 
> mingw-builds 4.7.3 (packages):
> x32-4.7.3-release-win32-sjlj-rev1.7z
> 
> Definitely I'm missing something here, so will appreciate any hint and
> suggestion.
> 
> Thank you in advance.
> 
> 
> 
> ------------------------------------------------------------------------------
> Get your SQL database under version control now!
> Version control is standard for application code, but databases havent 
> caught up. So what steps can you take to put your SQL databases under 
> version control? Why should you start doing it? Read more to find out.
> http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
> 
> 
> 
> _______________________________________________
> Mingwbuilds-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mingwbuilds-users
> 

------------------------------------------------------------------------------
Get your SQL database under version control now!
Version control is standard for application code, but databases havent 
caught up. So what steps can you take to put your SQL databases under 
version control? Why should you start doing it? Read more to find out.
http://pubads.g.doubleclick.net/gampad/clk?id=49501711&iu=/4140/ostg.clktrk
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to