Always return -1 but set errno in our wxVsnprintf() on error
This makes it more compatible with the standard behaviour of vswprintf() and allows to use the same logic in the builds using our version of this function and the normal ones in DoStringPrintfV(), simplifying its (already rather hairy) logic. Update the tests to not require any particular return value in case of buffer overflow, as this differs between Unicode and non-Unicode builds. When we finally drop the latter, we should just check that it always returns -1 in this case. Note that ideal would be to return the actually needed size of the buffer in case of the error due to buffer being too small, but this isn't that simple to do and it's probably not worth spending time on improving this code as long as we still need to use the buffer doubling strategy in DoStringPrintfV() when using the standard vswprintf().
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "wx/private/wxprintf.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
// ============================================================================
|
||||
// printf() implementation
|
||||
@@ -106,6 +107,9 @@ static int wxDoVsnprintf(CharType *buf, size_t lenMax,
|
||||
if (parser.posarg_present && parser.nonposarg_present)
|
||||
{
|
||||
buf[0] = 0;
|
||||
// Indicate to the caller that it's an unrecoverable error and not just
|
||||
// due to the buffer being too small.
|
||||
errno = EINVAL;
|
||||
return -1; // format strings with both positional and
|
||||
} // non-positional conversion specifier are unsupported !!
|
||||
|
||||
@@ -131,6 +135,7 @@ static int wxDoVsnprintf(CharType *buf, size_t lenMax,
|
||||
if (!ok)
|
||||
{
|
||||
buf[0] = 0;
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -154,7 +159,7 @@ static int wxDoVsnprintf(CharType *buf, size_t lenMax,
|
||||
if (lenCur == lenMax)
|
||||
{
|
||||
buf[lenMax - 1] = 0;
|
||||
return lenMax+1; // not enough space in the output buffer !
|
||||
return -1; // not enough space in the output buffer !
|
||||
}
|
||||
|
||||
// process this specifier directly in the output buffer
|
||||
@@ -163,7 +168,7 @@ static int wxDoVsnprintf(CharType *buf, size_t lenMax,
|
||||
if (n == -1)
|
||||
{
|
||||
buf[lenMax-1] = wxT('\0'); // be sure to always NUL-terminate the string
|
||||
return lenMax+1; // not enough space in the output buffer !
|
||||
return -1; // not enough space in the output buffer !
|
||||
}
|
||||
lenCur += n;
|
||||
|
||||
@@ -183,7 +188,7 @@ static int wxDoVsnprintf(CharType *buf, size_t lenMax,
|
||||
if (buf[lenCur])
|
||||
{
|
||||
buf[lenCur] = 0;
|
||||
return lenMax+1; // not enough space in the output buffer !
|
||||
return -1; // not enough space in the output buffer !
|
||||
}
|
||||
|
||||
// Don't do:
|
||||
|
||||
Reference in New Issue
Block a user