mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-16 02:03:07 -07:00
Fix wrong comparison result
The QString::toInt() might overflow when the string is long. Closes #10706.
This commit is contained in:
parent
4df1bca8bb
commit
36cf689432
1 changed files with 17 additions and 6 deletions
|
@ -96,18 +96,29 @@ namespace
|
||||||
}
|
}
|
||||||
else if (leftChar.isDigit() && rightChar.isDigit()) {
|
else if (leftChar.isDigit() && rightChar.isDigit()) {
|
||||||
// Both are digits, compare the numbers
|
// Both are digits, compare the numbers
|
||||||
const auto consumeNumber = [](const QString &str, int &pos) -> int
|
|
||||||
|
const auto numberView = [](const QString &str, int &pos) -> QStringRef
|
||||||
{
|
{
|
||||||
const int start = pos;
|
const int start = pos;
|
||||||
while ((pos < str.size()) && str[pos].isDigit())
|
while ((pos < str.size()) && str[pos].isDigit())
|
||||||
++pos;
|
++pos;
|
||||||
return str.midRef(start, (pos - start)).toInt();
|
return str.midRef(start, (pos - start));
|
||||||
};
|
};
|
||||||
|
|
||||||
const int numL = consumeNumber(left, posL);
|
const QStringRef numViewL = numberView(left, posL);
|
||||||
const int numR = consumeNumber(right, posR);
|
const QStringRef numViewR = numberView(right, posR);
|
||||||
|
|
||||||
|
if (numViewL.length() != numViewR.length())
|
||||||
|
return (numViewL.length() - numViewR.length());
|
||||||
|
|
||||||
|
// both string/view has the same length
|
||||||
|
for (int i = 0; i < numViewL.length(); ++i) {
|
||||||
|
const QChar numL = numViewL[i];
|
||||||
|
const QChar numR = numViewR[i];
|
||||||
|
|
||||||
if (numL != numR)
|
if (numL != numR)
|
||||||
return (numL - numR);
|
return (numL.unicode() - numR.unicode());
|
||||||
|
}
|
||||||
|
|
||||||
// String + digits do match and we haven't hit the end of both strings
|
// String + digits do match and we haven't hit the end of both strings
|
||||||
// then continue to consume the remainings
|
// then continue to consume the remainings
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue