snprintf buggy in gcc 4.9.3-1

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

snprintf buggy in gcc 4.9.3-1

Peter Smulders
Using the conversion %02d or %2d should produce a string of (at
least) 2 characters.
However when the number to be printed has only one digit snprintf
produces a field width of 1
A workaround is to use _snprintf.

gcc.exe (GCC) 4.9.3-1 as obtained by mingw-get on juli 30 2016
windows-10 64-bit

Regards, Peter
==============================
testprogram:

#include <stdio.h>
#include <string.h>
int main()
{
   char string[8];

//snprintf() drops the leading 0 in mingw gcc 4.9.3-1 !!
   snprintf(string,7,"%02d",3);
   printf("snprintf:\tstring=%s len=%d\n",string,strlen(string));

// with _snprintf() no problem
   _snprintf(string,7,"%02d",3);
   printf("_snprintf:\tstring=%s len=%d\n",string,strlen(string));

   return 0;
}
================================
Program output

snprintf:       string=3 len=1
_snprintf:      string=03 len=2


------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: snprintf buggy in gcc 4.9.3-1

Keith Marshall
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/08/16 23:45, Peter Smulders wrote:
> Using the conversion %02d or %2d should produce a string of (at
> least) 2 characters.

Correct.  However, this is nothing whatsoever to do with gcc-4.9.3,
(nor indeed specific to *any* GCC version).

> However when the number to be printed has only one digit snprintf
> produces a field width of 1

What you report here, is https://sourceforge.net/p/mingw/bugs/2309/
This is specific to mingwrt-3.22, and is already fixed in 3.22.1

> A workaround is to use _snprintf.

Hardly!  That itself is utterly broken (by MS design), and mostly
useless.  The solution is to keep your MinGW installation up to date;
use the latest version of mingwrt, i.e. mingwrt-3.22.1

- --
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJXoI0EAAoJEMCtNsY0flo/1gMP/0GLjLmUEfMSZfFOZL+fJySi
aMe+vXJ/E8GritY/UlUFWu3dsBRQI/wJ7bGJ9nI0N/V3Z3OFHwOIl9XoSia2wV9K
8iZjHvtiJZ45/PBF7zH/D3xahdmTyTq4X4bQHAqVJZokmJCLXSPtcgyNa8oXqrmv
Uz4z+YIRnHQMN6TBSM6VktYDPlUqeU2Lw24JotPa6K5VpiaMcBngt8IFWKs9yV/Y
FEyjQsjGcEXeDdY2zoytknxkuF8t26ajoDxb6S9GGzSWQVEcj4vAy8np8QXJq/BN
PwzdvG2mcYRE7mrBi2VAR46GyCN3bgJJp0j4+rp8orgv5/aAfZ2kauDMfKSXV6Bp
SHVxBA11j1sJbd2zmsOIr/aNjP3W+xP4JkFY0GuSZ+4hmh8I6NyJgOObE2TV2nJ/
vsmvCYCumSZfSgkaSL3HIaCu8pCfdSwivWvpWg+N8xf7xHkpIclZJniKzSBlV1YY
gi1Xdt18IUN/fDt9l+lTRW3OA7BKwJH/KHbWb+DnEA5PRBL+yiV+jhBUYRHmoBpi
6mvYC0GNQL6NeaYknNCDfmNbG5IcNcktzcZ7uDW21Cg9qgCspoHD/x4FBjE3XrUU
vQsGDmo/V+0+cXKbVl3mGHj+P0GPoekHZ7J5e44ZKs9r3UVAxzJ6QwnGiirMu2hR
gX4W+pD4oH1QhHWGh21c
=COB9
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe