Merge branch 'long-string-format'

Fix using "%s" with long (>65KiB) strings when using our own printf()
implementation.

See https://github.com/wxWidgets/wxWidgets/pull/1654
This commit is contained in:
Vadim Zeitlin
2019-11-18 15:07:00 +01:00
2 changed files with 55 additions and 90 deletions

View File

@@ -19,6 +19,7 @@
#include "wx/log.h" #include "wx/log.h"
#include "wx/utils.h" #include "wx/utils.h"
#include <limits.h>
#include <string.h> #include <string.h>
// prefer snprintf over sprintf // prefer snprintf over sprintf
@@ -189,7 +190,7 @@ template<typename CharType>
void wxPrintfConvSpec<CharType>::Init() void wxPrintfConvSpec<CharType>::Init()
{ {
m_nMinWidth = 0; m_nMinWidth = 0;
m_nMaxWidth = 0xFFFF; m_nMaxWidth = INT_MAX;
m_pos = 0; m_pos = 0;
m_bAlignLeft = false; m_bAlignLeft = false;
m_pArgPos = m_pArgEnd = NULL; m_pArgPos = m_pArgEnd = NULL;

View File

@@ -24,81 +24,38 @@
#include "wx/string.h" #include "wx/string.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// test class // tests themselves
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class VarArgTestCase : public CppUnit::TestCase TEST_CASE("StringPrintf", "[wxString][Printf][vararg]")
{
public:
VarArgTestCase() {}
private:
CPPUNIT_TEST_SUITE( VarArgTestCase );
CPPUNIT_TEST( StringPrintf );
CPPUNIT_TEST( CharPrintf );
CPPUNIT_TEST( SizetPrintf );
#if wxUSE_STD_STRING
CPPUNIT_TEST( StdString );
#endif
#if wxUSE_LONGLONG
CPPUNIT_TEST( LongLongPrintf );
#endif
CPPUNIT_TEST( Sscanf );
CPPUNIT_TEST( RepeatedPrintf );
CPPUNIT_TEST( ArgsValidation );
CPPUNIT_TEST_SUITE_END();
void StringPrintf();
void CharPrintf();
void SizetPrintf();
#if wxUSE_STD_STRING
void StdString();
#endif
#if wxUSE_LONGLONG
void LongLongPrintf();
#endif
void Sscanf();
void RepeatedPrintf();
void ArgsValidation();
wxDECLARE_NO_COPY_CLASS(VarArgTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( VarArgTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VarArgTestCase, "VarArgTestCase" );
void VarArgTestCase::StringPrintf()
{ {
wxString s, s2; wxString s, s2;
// test passing literals: // test passing literals:
s.Printf("%s %i", "foo", 42); s.Printf("%s %i", "foo", 42);
CPPUNIT_ASSERT( s == "foo 42" ); CHECK( s == "foo 42" );
s.Printf("%s %s %i", wxT("bar"), "=", 11); s.Printf("%s %s %i", wxT("bar"), "=", 11);
// test passing c_str(): // test passing c_str():
CPPUNIT_ASSERT( s == "bar = 11" ); CHECK( s == "bar = 11" );
s2.Printf("(%s)", s.c_str()); s2.Printf("(%s)", s.c_str());
CPPUNIT_ASSERT( s2 == "(bar = 11)" ); CHECK( s2 == "(bar = 11)" );
s2.Printf(wxT("[%s](%s)"), s.c_str(), "str"); s2.Printf(wxT("[%s](%s)"), s.c_str(), "str");
CPPUNIT_ASSERT( s2 == "[bar = 11](str)" ); CHECK( s2 == "[bar = 11](str)" );
s2.Printf("%s mailbox", wxString("Opening").c_str()); s2.Printf("%s mailbox", wxString("Opening").c_str());
CPPUNIT_ASSERT( s2 == "Opening mailbox" ); CHECK( s2 == "Opening mailbox" );
// test passing wxString directly: // test passing wxString directly:
s2.Printf(wxT("[%s](%s)"), s, "str"); s2.Printf(wxT("[%s](%s)"), s, "str");
CPPUNIT_ASSERT( s2 == "[bar = 11](str)" ); CHECK( s2 == "[bar = 11](str)" );
// test passing wxCharBufferType<T>: // test passing wxCharBufferType<T>:
s = "FooBar"; s = "FooBar";
s2.Printf(wxT("(%s)"), s.mb_str()); s2.Printf(wxT("(%s)"), s.mb_str());
CPPUNIT_ASSERT( s2 == "(FooBar)" ); CHECK( s2 == "(FooBar)" );
s2.Printf(wxT("value=%s;"), s.wc_str()); s2.Printf(wxT("value=%s;"), s.wc_str());
CPPUNIT_ASSERT( s2 == "value=FooBar;" ); CHECK( s2 == "value=FooBar;" );
// this tests correct passing of wxCStrData constructed from string // this tests correct passing of wxCStrData constructed from string
// literal (and we disable the warnings related to the use of a literal // literal (and we disable the warnings related to the use of a literal
@@ -113,23 +70,23 @@ void VarArgTestCase::StringPrintf()
wxCLANG_WARNING_RESTORE(c++11-compat-deprecated-writable-strings) wxCLANG_WARNING_RESTORE(c++11-compat-deprecated-writable-strings)
} }
void VarArgTestCase::CharPrintf() TEST_CASE("CharPrintf", "[wxString][Printf][vararg]")
{ {
wxString foo("foo"); wxString foo("foo");
wxString s; wxString s;
// test using wchar_t: // test using wchar_t:
s.Printf("char=%c", L'c'); s.Printf("char=%c", L'c');
CPPUNIT_ASSERT_EQUAL( "char=c", s ); CHECK( s == "char=c" );
// test wxUniCharRef: // test wxUniCharRef:
s.Printf("string[1] is %c", foo[1]); s.Printf("string[1] is %c", foo[1]);
CPPUNIT_ASSERT_EQUAL( "string[1] is o", s ); CHECK( s == "string[1] is o" );
// test char // test char
char c = 'z'; char c = 'z';
s.Printf("%c to %c", 'a', c); s.Printf("%c to %c", 'a', c);
CPPUNIT_ASSERT_EQUAL( "a to z", s ); CHECK( s == "a to z" );
// test char used as integer: // test char used as integer:
#ifdef _MSC_VER #ifdef _MSC_VER
@@ -143,34 +100,28 @@ void VarArgTestCase::CharPrintf()
#endif #endif
#ifndef __CHAR_UNSIGNED__ #ifndef __CHAR_UNSIGNED__
s.Printf("value is %i (int)", c); s.Printf("value is %i (int)", c);
CPPUNIT_ASSERT_EQUAL( wxString("value is -16 (int)"), s ); CHECK( s == wxString("value is -16 (int)") );
#endif #endif
unsigned char u = 240; unsigned char u = 240;
s.Printf("value is %i (int)", u); s.Printf("value is %i (int)", u);
CPPUNIT_ASSERT_EQUAL( "value is 240 (int)", s ); CHECK( s == "value is 240 (int)" );
} }
void VarArgTestCase::SizetPrintf() TEST_CASE("SizetPrintf", "[wxString][Printf][vararg]")
{ {
size_t i = 1; size_t i = 1;
ssize_t j = -2; ssize_t j = -2;
CPPUNIT_ASSERT_EQUAL CHECK( wxString::Format("size_t=%zu ssize_t=%zd", i, j)
( == "size_t=1 ssize_t=-2" );
"size_t=1 ssize_t=-2",
wxString::Format("size_t=%zu ssize_t=%zd", i, j)
);
CPPUNIT_ASSERT_EQUAL CHECK( wxString::Format("size_t=0x%zX", static_cast<size_t>(160))
( == "size_t=0xA0" );
"size_t=0xA0",
wxString::Format("size_t=0x%zX", static_cast<size_t>(160))
);
} }
#if wxUSE_STD_STRING #if wxUSE_STD_STRING
void VarArgTestCase::StdString() TEST_CASE("StdString", "[wxString][Printf][vararg]")
{ {
// test passing std::[w]string // test passing std::[w]string
wxString s; wxString s;
@@ -179,26 +130,26 @@ void VarArgTestCase::StdString()
std::string wc("widechar"); std::string wc("widechar");
s.Printf("string %s(%i).", mb, 1); s.Printf("string %s(%i).", mb, 1);
CPPUNIT_ASSERT_EQUAL( "string multi-byte(1).", s ); CHECK( s == "string multi-byte(1)." );
s.Printf("string %s(%i).", wc, 2); s.Printf("string %s(%i).", wc, 2);
CPPUNIT_ASSERT_EQUAL( "string widechar(2).", s ); CHECK( s == "string widechar(2)." );
} }
#endif // wxUSE_STD_STRING #endif // wxUSE_STD_STRING
#if wxUSE_LONGLONG #if wxUSE_LONGLONG
void VarArgTestCase::LongLongPrintf() TEST_CASE("LongLongPrintf", "[wxString][Printf][vararg]")
{ {
const char * const llfmt = "%" wxLongLongFmtSpec "d"; const char * const llfmt = "%" wxLongLongFmtSpec "d";
CPPUNIT_ASSERT_EQUAL( "17", wxString::Format(llfmt, wxLL(17)) ); CHECK( wxString::Format(llfmt, wxLL(17)) == "17" );
wxLongLong ll = 1234567890; wxLongLong ll = 1234567890;
CPPUNIT_ASSERT_EQUAL( "1234567890", wxString::Format(llfmt, ll) ); CHECK( wxString::Format(llfmt, ll) == "1234567890" );
} }
#endif // wxUSE_LONGLONG #endif // wxUSE_LONGLONG
void VarArgTestCase::Sscanf() TEST_CASE("Sscanf", "[wxSscanf][vararg]")
{ {
int i = 0; int i = 0;
char str[20]; char str[20];
@@ -206,8 +157,8 @@ void VarArgTestCase::Sscanf()
wxString input("42 test"); wxString input("42 test");
wxSscanf(input, "%d %s", &i, &str); wxSscanf(input, "%d %s", &i, &str);
CPPUNIT_ASSERT( i == 42 ); CHECK( i == 42 );
CPPUNIT_ASSERT( wxString(str) == "test" ); CHECK( wxString(str) == "test" );
#if !(defined(__MINGW32__) && \ #if !(defined(__MINGW32__) && \
defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 1) defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 1)
@@ -218,12 +169,12 @@ void VarArgTestCase::Sscanf()
i = 0; i = 0;
wxSscanf(input, L"%d %s", &i, &wstr); wxSscanf(input, L"%d %s", &i, &wstr);
CPPUNIT_ASSERT( i == 42 ); CHECK( i == 42 );
CPPUNIT_ASSERT( wxString(wstr) == "test" ); CHECK( wxString(wstr) == "test" );
#endif #endif
} }
void VarArgTestCase::RepeatedPrintf() TEST_CASE("RepeatedPrintf", "[wxString][Printf][vararg]")
{ {
wxCharBuffer buffer(2); wxCharBuffer buffer(2);
char *p = buffer.data(); char *p = buffer.data();
@@ -233,16 +184,16 @@ void VarArgTestCase::RepeatedPrintf()
wxString s; wxString s;
s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer)); s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer));
CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s); CHECK( s == "buffer hi, len 2" );
s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer)); s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer));
CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s); CHECK( s == "buffer hi, len 2" );
} }
void VarArgTestCase::ArgsValidation() TEST_CASE("ArgsValidation", "[wxString][vararg][error]")
{ {
void *ptr = this;
int written; int written;
void *ptr = &written;
short int swritten; short int swritten;
// these are valid: // these are valid:
@@ -278,12 +229,12 @@ void VarArgTestCase::ArgsValidation()
#ifndef wxNO_PRINTF_PERCENT_N #ifndef wxNO_PRINTF_PERCENT_N
wxString::Format("foo%i%n", 42, &written); wxString::Format("foo%i%n", 42, &written);
CPPUNIT_ASSERT_EQUAL( 5, written ); CHECK( written == 5 );
#endif #endif
// but these are not: // but these are not:
WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%i", "foo") ); WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%i", "foo") );
WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", (void*)this) ); WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", (void*)&written) );
WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%d", ptr) ); WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%d", ptr) );
// we don't check wxNO_PRINTF_PERCENT_N here as these expressions should // we don't check wxNO_PRINTF_PERCENT_N here as these expressions should
@@ -306,10 +257,23 @@ void VarArgTestCase::ArgsValidation()
wxString::Format("%c", wxChar(80) + wxChar(1)); wxString::Format("%c", wxChar(80) + wxChar(1));
// check size_t handling // check size_t handling
size_t len = sizeof(*this); size_t len = sizeof(ptr);
#ifdef __WINDOWS__ #ifdef __WINDOWS__
wxString::Format("%Iu", len); wxString::Format("%Iu", len);
#else #else
wxString::Format("%zu", len); wxString::Format("%zu", len);
#endif #endif
} }
TEST_CASE("VeryLongArg", "[wxString][Format][vararg]")
{
const size_t LENGTH = 70000;
wxString veryLongString('.', LENGTH);
REQUIRE( veryLongString.length() == LENGTH );
const wxString s = wxString::Format("%s", veryLongString);
// Check the length first to avoid very long output if this fails.
REQUIRE( s.length() == LENGTH );
CHECK( s == veryLongString );
}