From b2e678bfd4f190ab27cb380201207d8f227f0739 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 27 Jun 2011 13:09:23 +0200 Subject: QStringBuilder: do not crash with null char* This is supported by the others operator+ Change-Id: I9a1d1a0afb63acf32935948111d43ca6da370363 Reviewed-on: http://codereview.qt.nokia.com/764 Reviewed-by: hjk (cherry picked from commit 53a16752c257d2f4f99ecef2cde1580b817fc12a) --- src/corelib/tools/qstringbuilder.cpp | 2 ++ src/corelib/tools/qstringbuilder.h | 2 ++ tests/auto/qstringbuilder1/stringbuilder.cpp | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/src/corelib/tools/qstringbuilder.cpp b/src/corelib/tools/qstringbuilder.cpp index 45de6bc..1cc7e5d 100644 --- a/src/corelib/tools/qstringbuilder.cpp +++ b/src/corelib/tools/qstringbuilder.cpp @@ -162,6 +162,8 @@ void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out } #endif if (len == -1) { + if (!a) + return; while (*a) *out++ = QLatin1Char(*a++); } else { diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 709d84a..594ab2f 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -352,6 +352,8 @@ template <> struct QConcatenable : private QAbstractConcatenable #endif static inline void appendTo(const char *a, char *&out) { + if (!a) + return; while (*a) *out++ = *a++; } diff --git a/tests/auto/qstringbuilder1/stringbuilder.cpp b/tests/auto/qstringbuilder1/stringbuilder.cpp index 2327ef5..de7ad65 100644 --- a/tests/auto/qstringbuilder1/stringbuilder.cpp +++ b/tests/auto/qstringbuilder1/stringbuilder.cpp @@ -133,6 +133,12 @@ void runScenario() QCOMPARE(r, string); r = string P ba; QCOMPARE(r, string); + + const char *zero = 0; + r = string P zero; + QCOMPARE(r, string); + r = zero P string; + QCOMPARE(r, string); #endif string = QString::fromLatin1(LITERAL); @@ -161,6 +167,12 @@ void runScenario() QCOMPARE(r, r2); r2 = QByteArray("hello\0") P UTF8_LITERAL; QCOMPARE(r, r2); + + const char *zero = 0; + r = ba P zero; + QCOMPARE(r, ba); + r = zero P ba; + QCOMPARE(r, ba); } //operator QString += -- cgit v0.12