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:
Vadim Zeitlin
2020-11-30 18:02:16 +01:00
parent 7a2786bf11
commit 8fb4ab99f1
3 changed files with 11 additions and 25 deletions

View File

@@ -52,11 +52,10 @@ int r;
// Another helper which takes the size explicitly instead of using MAX_TEST_LEN
//
// NOTE: this macro is used also with too-small buffers (see Miscellaneous())
// test function, thus the return value can be > size and thus we
// test function, thus the return value can be either -1 or > size and we
// cannot check if r == (int)wxStrlen(buf)
#define CMPTOSIZE(buffer, size, failuremsg, expected, fmt, ...) \
r=wxSnprintf(buffer, size, fmt, ##__VA_ARGS__); \
CHECK( r > 0 ); \
INFO(failuremsg); \
CHECK( buffer == wxString(expected).Left(size - 1) )
@@ -340,8 +339,10 @@ TEST_CASE_METHOD(VsnprintfTestCase, "Vsnprintf::WrongFormatStrings", "[vsnprintf
wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3) );
// positional and non-positionals in the same format string:
errno = 0;
r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %d %3$d"), 1, 2, 3);
CHECK( r == -1 );
CHECK( errno == EINVAL );
}
// BigToSmallBuffer() test case helper:
@@ -392,7 +393,6 @@ void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
wxString expected =
wxString(wxT("unicode string/char: unicode/U -- ansi string/char: ansi/A")).Left(size - 1);
CHECK( r != -1 );
CHECK( expected == buffer );
}