Web lists-archives.com

Re: [Mingw-users] snprintf and __USE_MINGW_ANSI_STDIO




On 16/11/16 13:50, Burkhardt, Glenn B        UTAS wrote:
> It looks like "snprintf" and friends don't have the same definitions 
> to use the ANSI stdio functions like printf, fprintf, and sprintf do. 
> The result is that if __USE_MINGW_ANSI_STDIO is defined, and -Wall is 
> used, there are compile time warnings if specifications like "%lld" 
> are used.

On Wed 11/16/2016 11:28, Keith Marshall wrote:
> This is nothing whatsoever to do with __USE_MINGW_ANSI_STDIO, (which you really SHOULDN'T define yourself, BTW; use appropriate
> _POSIX_C_SOURCE, or _XOPEN_SOURCE definitions instead).  This is just evidence of the inherent dumbness of GCC's printf format warnings.

I have to disagree with you.  The problem is the omission of the correct definition for "snprintf" with __USE_MINGW_ANSI_STDIO defined.  It works for "printf", and "sprintf".  Look at stdio.h - the static definition for "sprintf" is there to use __mingw_vsprintf(), but is missing for "snprintf".

This small program:

#define __USE_MINGW_ANSI_STDIO 1
#include <stdio.h>

int main()
{
    long long x = 0x100000000L;
    char buff[128];

    printf("%lld\n", x);
    sprintf(buff, "%lld\n", x);
    printf("%s", buff);
    snprintf(buff, sizeof(buff), "%lld\n", x);
    printf("%s", buff);

    return 0;
}

gives these warnings:
$ gcc -g -Wall tt.c -o tt
tt.c: In function 'main':
tt.c:12:5: warning: unknown conversion type character 'l' in format [-Wformat]
tt.c:12:5: warning: too many arguments for format [-Wformat-extra-args]

But the "snprintf" call works properly.  The output is:
4294967296
4294967296
4294967296

> > The function actually works correctly with "%lld". Is this a bug, or 
> > am I missing something?

> If the function works correctly, it's hardly a bug, is it?  GCC's printf format warning feature is unlikely to deliver sane diagnostics, while it assumes that 
> only MSVC semantics are applicable.  (FWIW, the second most prevalent diagnostic emitted during a build of GCC itself, is a swathe of bogus 
> warnings about MSVC format specifiers).

Call me fastidious, but I find that compiler warnings often alert me to real errors in the code.  False positives waste my time.

> If they bother you, disable the -Wformat (-Wno-format) messages, (or, maybe better, use the format specifier macros from <inttypes.h>).

BTW, the correct format specifiers for int64_t, viz, PRId64, are wrong when using __USE_MINGW_ANSI_STDIO.  That's another bug.

Also, for what it's worth, this all works properly with the headers from the MinGW-w64 fork of the MinGW code, and on every Linux system I use.   :-)


------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

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:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe