Uoti Urpala <[email protected]> writes:

> On Thu, 2012-07-26 at 07:32 +0200, Luca Barbato wrote:
>> +int snprintf(char *buffer, size_t bufsize, const char *fmt, ...)
>> +{
>> +    va_list ap;
>> +    int ret;
>> +
>> +    if ((int)bufsize <= 0) return -1;
>
> bufsize == 0 is valid.

Of course zero is a perfectly valid value to pass to standard snprintf()...
size > INT_MAX causes EOVERFLOW on POSIX and probably does something
nasty on windows, so catching that here might be a good idea.  No sane
code would intentionally pass such a large value anyway.

>> +    ret = vsnprintf(buffer, bufsize-1, fmt, ap);
>
> IIRC MSVC (or the C library) does not support all standard format
> modifiers, so unless this vsnprintf comes from elsewhere, you need more
> to get fully working printf formatting.
>
> I'm not sure if this is working in current MinGW-compiled Libav either -
> you need to set __USE_MINGW_ANSI_STDIO at least; the problem cases
> probably occur rarely enough that problems may go unnoticed.
>
>> +    if (ret < 0) {
>> +        buffer[bufsize - 1] = '\0';
>> +        ret = bufsize - 1;
>
> Wrong return value. It's supposed to be the number of bytes the output
> would take without restriction. The following pattern is supposed to
> work:
>
> int r = snprintf(NULL, 0, ...);
> if (r < 0) return -1;
> char *p = malloc(r + 1);
> snprintf(p, r, ...);

It is not possible to get those semantics using the regular Windows
functions.

-- 
Måns Rullgård
[email protected]
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to