From 8e119e96f7e4c46d1ae268c2a198092b717409cb Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Fri, 26 Jul 2019 10:23:52 +0000 Subject: [PATCH] null-termiate the string from QByteArray(const char *data) constructor Signed-off-by: Ivailo Monev --- src/core/tools/qbytearray.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/core/tools/qbytearray.cpp b/src/core/tools/qbytearray.cpp index 1a7e776eb..72cec9fec 100644 --- a/src/core/tools/qbytearray.cpp +++ b/src/core/tools/qbytearray.cpp @@ -1386,20 +1386,21 @@ void QByteArray::chop(int n) QByteArray makes a deep copy of the string data. */ -QByteArray::QByteArray(const char *str) +QByteArray::QByteArray(const char *data) { - if (!str) { + int size = qstrlen(data); + if (!data) { d = &shared_null; - } else if (!*str) { + } else if (size <= 0) { d = &shared_empty; } else { - int len = qstrlen(str); - d = static_cast(malloc(sizeof(Data)+len)); + d = static_cast(malloc(sizeof(Data) + size)); Q_CHECK_PTR(d); - d->ref = 0;; - d->alloc = d->size = len; + d->ref = 0; + d->alloc = d->size = size; d->data = d->array; - memcpy(d->array, str, len+1); // include null terminator + memcpy(d->array, data, size); + d->array[size] = '\0'; } d->ref.ref(); }