Skip to content

Commit

Permalink
Fix overflow issue on parsing min-qint64 with its minus sign repeated
Browse files Browse the repository at this point in the history
The call to std::from_chars() accepts a sign, but we've already dealt
with a sign, so that would be a second sign. Check the first character
after any prefix is in fact a digit (for the base in use). This is a
follow-up to commit 5644af6.

Fixes: QTBUG-97521
Change-Id: I65fb144bf6a8430da90ec5f65088ca20e79bf02f
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
ediosyncratic committed Oct 26, 2021
1 parent 25e85ec commit bb220f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/corelib/text/qlocale_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,19 @@ static auto scanPrefix(const char *p, const char *stop, int base)
return R{p, base};
}

static bool isDigitForBase(char d, int base)
{
if (d < '0')
return false;
if (d - '0' < qMin(base, 10))
return true;
if (base > 10) {
d |= 0x20; // tolower
return d >= 'a' && d < 'a' + base - 10;
}
return false;
}

unsigned long long
qstrntoull(const char *begin, qsizetype size, const char **endptr, int base, bool *ok)
{
Expand Down Expand Up @@ -479,7 +492,9 @@ qstrntoll(const char *begin, qsizetype size, const char **endptr, int base, bool
++p;

const auto prefix = scanPrefix(p, stop, base);
if (!prefix.base || prefix.next >= stop) {
// Must check for digit, as from_chars() will accept a sign, which would be
// a second sign, that we should reject.
if (!prefix.base || prefix.next >= stop || !isDigitForBase(*prefix.next, prefix.base)) {
if (endptr)
*endptr = begin;
*ok = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,7 @@ void tst_QStringApiSymmetry::toNumber_data()
QTest::addRow("-32768") << QString::fromUtf8("-32768") << qint64(-32768) << true;
QTest::addRow("100x") << QString::fromUtf8("100x") << qint64(0) << false;
QTest::addRow("-100x") << QString::fromUtf8("-100x") << qint64(0) << false;
QTest::addRow("-min64") << QString::fromUtf8("--9223372036854775808") << qint64(0) << false;
}

template<typename T>
Expand Down

0 comments on commit bb220f2

Please sign in to comment.