From 088eef57566cb59b9c3b10e218a80b0516cb7e8a Mon Sep 17 00:00:00 2001
From: Marc Mutz <marc.mutz@kdab.com>
Date: Mon, 25 Jan 2016 19:59:51 +0100
Subject: [PATCH] QStringBuilder: fix appending QLatin1String to QByteArray

The old code did the equivalent of strcpy(), thus stopping
at the first NUL byte, ignoring the QLatin1String's size().

That is not acceptable, for two reasons:

1. Appending QLatin1String to a QString uses the size(), too.

2. The QConcatenable claims an ExactSize = true, so
   it cannot go and write less data than it's own
   size() said it would. Even worse, it will happily
   write _more_ data, too, if the QLatin1String is
   not properly zero-terminated.

This change has low risk, because the equivalent change
to the QString branch has been applied between 5.2 and
5.3 (in fd0f1bc3), with no complaints from the user base.

It is also in a branch that is very unlikely to be taken:
Since QConcatenable<QLatin1String> is setting ConvertTo
to QString, any QStringBuilder expression containing it
will only implicitly convert to QString, not QByteArray.
In fact, I don't even know how to make it invoke the
changed code in normal operation...

Change-Id: I486a76352af7f318ba05da845d3afee7d826c92a
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
---
 src/corelib/tools/qstringbuilder.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h
index 3d41aeee18e..69bd344f478 100644
--- a/src/corelib/tools/qstringbuilder.h
+++ b/src/corelib/tools/qstringbuilder.h
@@ -236,9 +236,9 @@ template <> struct QConcatenable<QLatin1String> : private QAbstractConcatenable
     }
     static inline void appendTo(const QLatin1String a, char *&out)
     {
-        if (a.data()) {
-            for (const char *s = a.data(); *s; )
-                *out++ = *s++;
+        if (const char *data = a.data()) {
+            memcpy(out, data, a.size());
+            out += a.size();
         }
     }
 };
-- 
GitLab