From 5d11688d02e1f56722dce809cbe7ab5c49fea590 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 28 Jul 2014 20:01:23 -0700 Subject: Fix QByteArray::to{Upper,Lower} when the array contains embedded nulls [ChangeLog][QtCore][QByteArray] Fixed a bug that would cause QByteArray to stop converting toUpper or toLower at the first embedded null character. Change-Id: Ia369037206617813d86a8f1489589243c82aa51b Reviewed-by: Marc Mutz --- src/corelib/tools/qbytearray.cpp | 6 +++-- .../corelib/tools/qbytearray/tst_qbytearray.cpp | 27 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 52af67c86a..d57eeaf188 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -2697,8 +2697,9 @@ QByteArray QByteArray::toLower() const { QByteArray s(*this); uchar *p = reinterpret_cast(s.data()); + uchar *e = reinterpret_cast(s.end()); if (p) { - while (*p) { + while (p != e) { *p = QChar::toLower((ushort)*p); p++; } @@ -2720,8 +2721,9 @@ QByteArray QByteArray::toUpper() const { QByteArray s(*this); uchar *p = reinterpret_cast(s.data()); + uchar *e = reinterpret_cast(s.end()); if (p) { - while (*p) { + while (p != e) { *p = QChar::toUpper((ushort)*p); p++; } diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 8fac232962..52e1850c87 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -151,6 +151,8 @@ private slots: #if defined(Q_COMPILER_LAMBDA) void literals(); #endif + void toUpperLower_data(); + void toUpperLower(); void macTypes(); @@ -1999,6 +2001,31 @@ void tst_QByteArray::literals() } #endif +void tst_QByteArray::toUpperLower_data() +{ + QTest::addColumn("input"); + QTest::addColumn("upper"); + QTest::addColumn("lower"); + + QTest::newRow("empty") << QByteArray() << QByteArray() << QByteArray(); + QTest::newRow("ascii") << QByteArray("Hello World, this is a STRING") + << QByteArray("HELLO WORLD, THIS IS A STRING") + << QByteArray("hello world, this is a string"); + QTest::newRow("latin1") << QByteArray("R\311sum\351") + << QByteArray("R\311SUM\311") + << QByteArray("r\351sum\351"); + QTest::newRow("nul") << QByteArray("a\0B", 3) << QByteArray("A\0B", 3) << QByteArray("a\0b", 3); +} + +void tst_QByteArray::toUpperLower() +{ + QFETCH(QByteArray, input); + QFETCH(QByteArray, upper); + QFETCH(QByteArray, lower); + QCOMPARE(input.toUpper(), upper); + QCOMPARE(input.toLower(), lower); +} + void tst_QByteArray::macTypes() { #ifndef Q_OS_MAC -- cgit v1.2.3