QString: fix append(const QStringRef &str)

Use QStringRef::isNull instead of QStringRef::string()
for validation. Non-NULL str.string() may yet leave us
with a useless str.unicode(), which is the actual problem here;
whereas !str.isNull() does really confirm that str.unicode()
is sensible.

Such test prevents situation like:

const QString a;
QString b;
b.append(a); // b.isNull() == true
b.append(QStringRef(&a)); // b.isNull() == false

Auto test updated: create QStringRef from QString directly, without
any condition.

Change-Id: I082cd58ef656d8a53e3c1223aca01feea82fffb9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2016-07-11 17:23:21 +03:00 committed by Simon Hausmann
parent 35117590c8
commit 9c8a8e90a6
2 changed files with 2 additions and 2 deletions

View File

@ -9375,7 +9375,7 @@ QString &QString::append(const QStringRef &str)
{ {
if (str.string() == this) { if (str.string() == this) {
str.appendTo(this); str.appendTo(this);
} else if (str.string()) { } else if (!str.isNull()) {
int oldSize = size(); int oldSize = size();
resize(oldSize + str.size()); resize(oldSize + str.size());
memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar)); memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar));

View File

@ -131,7 +131,7 @@ template <>
class Arg<QStringRef> : ArgBase class Arg<QStringRef> : ArgBase
{ {
QStringRef ref() const QStringRef ref() const
{ return this->pinned.isNull() ? QStringRef() : this->pinned.midRef(0) ; } { return QStringRef(&pinned); }
public: public:
explicit Arg(const char *str) : ArgBase(str) {} explicit Arg(const char *str) : ArgBase(str) {}