Strange behaviour with g++
I have a strange bug if I use AlpineLinux v3.6 and current edge with g.
Example:
int main(int argc, char** argv)
{
const QString pVersion = QStringLiteral("1.12.2");
int idx = 0;
const auto& version = QVersionNumber::fromString(pVersion, &idx);
qDebug() << "idx before:" << idx;
const auto& suffix = pVersion.mid(idx).trimmed();
qDebug() << "idx after" << idx;
qDebug() << "version:" << version;
qDebug() << "suffix" << suffix;
return 0;
}
Strange is this “idx before 0” as “idx after” is 6 now.
idx before 0
idx after 6
version: 1.12.2
suffix ""
If I remove the output line “qDebug() << ”idx before" <<
idx;"
I get a complete other output as it seems it uses “0” for idx:
idx after 6
version: 1.12.2
suffix "1.12.2"
If I use “int idx;” instead of 0 init it will work again:
idx after 6
version: 1.12.2
suffix ""
This will only work in “release builds” with gcc.
clang is correct in any alternative.
Looks like a gcc bug.
Just to be complete. This is the source of Qt function:
QVersionNumber QVersionNumber::fromString(const QString &string, int *suffixIndex)
{
QVector<int> seg;
const QByteArray cString(string.toLatin1());
const char *start = cString.constData();
const char *end = start;
const char *lastGoodEnd = start;
const char *endOfString = cString.constData() + cString.size();
do {
bool ok = false;
const qulonglong value = qstrtoull(start, &end, 10, &ok);
if (!ok || value > qulonglong(std::numeric_limits<int>::max()))
break;
seg.append(int(value));
start = end + 1;
lastGoodEnd = end;
} while (start < endOfString && (end < endOfString && *end == '.'));
if (suffixIndex)
*suffixIndex = int(lastGoodEnd - cString.constData());
return QVersionNumber(qMove(seg));
}
(from redmine: issue id 7584, created on 2017-07-21)