Fix crash while sorting lines with numbers longer than 20 digits

Fix #5261, close #5555
pull/5718/head
Rajendra Singh 2019-04-20 21:53:37 +05:30 committed by Don HO
parent 3e993ff4c7
commit ff20c264df
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
1 changed files with 30 additions and 2 deletions

View File

@ -156,7 +156,21 @@ public:
else if (aChunkIsNum)
{
size_t delta = 0;
compareResult = std::stoll(a.substr(i)) - std::stoll(b.substr(i), &delta);
// stoll crashes if number exceeds the limit for unsigned long long
// Maximum value for a variable of type unsigned long long | 18446744073709551615
// So take the max length 18 to convert the number
const size_t maxLen = 18;
size_t aLen = a.length() - i, bLen = b.length() - i;
if (aLen > maxLen || bLen > maxLen)
{
delta = min(min(aLen, bLen), maxLen);
compareResult = std::stoll(a.substr(i, delta)) - std::stoll(b.substr(i, delta));
}
else
{
compareResult = std::stoll(a.substr(i)) - std::stoll(b.substr(i), &delta);
}
i += delta;
}
// Both are strings
@ -206,7 +220,21 @@ public:
else if (aChunkIsNum)
{
size_t delta = 0;
compareResult = std::stoll(a.substr(i)) - std::stoll(b.substr(i), &delta);
// stoll crashes if number exceeds the limit for unsigned long long
// Maximum value for a variable of type unsigned long long | 18446744073709551615
// So take the max length 18 to convert the number
const size_t maxLen = 18;
size_t aLen = a.length() - i, bLen = b.length() - i;
if (aLen > maxLen || bLen > maxLen)
{
delta = min(min(aLen, bLen), maxLen);
compareResult = std::stoll(a.substr(i, delta)) - std::stoll(b.substr(i, delta));
}
else
{
compareResult = std::stoll(a.substr(i)) - std::stoll(b.substr(i), &delta);
}
i += delta;
}
// Both are strings