[flac-dev] vsnprintf_s and vsnprintf
lvqcl
lvqcl.mail at gmail.com
Fri Sep 19 13:11:31 PDT 2014
Erik de Castro Lopo wrote:
>> Oops. It seems that vsnprintf_s isn't always available on MinGW platform:
>> MinGW declares this function only if MINGW_HAS_SECURE_API macro is defined.
>> That's because WinXP version of msvcrt.dll doesn't contain secure functions
>> like vsnprintf_s.
>>
>> Maybe it's better to revert vsnprintf_s to vsprintf or to use vnsprintf?
>
> Ok, we need to drop vsnprintf_s to support WinXP. I'd prefer vsnprintf
> over vsprintf but have no way of testing any of these options.
I wrote a small program that fills a buffer[] with "abcdefghijklmnopqrstuvwxyz\0"
pattern and then tries to write "0123456789" string into it.
It calls either
ret = vsnprintf_s(buffer, buf_size, _TRUNCATE, fmt, va);
or
ret = vsnprintf(buffer, buf_size, fmt, va);
The results are:
---------------------------------------------------------------------------
vsnprintf_s (MSVC, MinGW):
buf_size = 8; ret = -1; buffer = "0123456"
buf_size = 9; ret = -1; buffer = "01234567"
buf_size = 10; ret = -1; buffer = "012345678"
buf_size = 11; ret = 10; buffer = "0123456789"
buf_size = 12; ret = 10; buffer = "0123456789"
---------------------------------------------------------------------------
vsnprintf (MSVC, MinGW):
buf_size = 8; ret = -1; buffer = "01234567ijklmnopqrstuvwxyz"
buf_size = 9; ret = -1; buffer = "012345678jklmnopqrstuvwxyz"
buf_size = 10; ret = 10; buffer = "0123456789klmnopqrstuvwxyz"
buf_size = 11; ret = 10; buffer = "0123456789"
buf_size = 12; ret = 10; buffer = "0123456789"
---------------------------------------------------------------------------
vsnprintf (MinGW with "#define __USE_MINGW_ANSI_STDIO 1"):
buf_size = 8; ret = 10; buffer = "0123456"
buf_size = 9; ret = 10; buffer = "01234567"
buf_size = 10; ret = 10; buffer = "012345678"
buf_size = 11; ret = 10; buffer = "0123456789"
buf_size = 12; ret = 10; buffer = "0123456789"
---------------------------------------------------------------------------
By the way... according to MSDN page for CreateProcess() function
<http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx>:
"The maximum length of [the command line] is 32,768 characters, including the Unicode terminating null character".
UTF-8 uses up to 4 bytes for a character, so the maximum length of
a temporary buffer for a call like
flac_fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
should in theory exceed 4*32768 = 131072 bytes (plus some space for
the rest of the message).
Currently *printf_utf8() functions allocate 32768 bytes buffer.
More information about the flac-dev
mailing list