From ff20c264df4167943fff6247fec4b0c0ce6227fb Mon Sep 17 00:00:00 2001 From: Rajendra Singh Date: Sat, 20 Apr 2019 21:53:37 +0530 Subject: [PATCH] Fix crash while sorting lines with numbers longer than 20 digits Fix #5261, close #5555 --- PowerEditor/src/MISC/Common/Sorters.h | 32 +++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Sorters.h b/PowerEditor/src/MISC/Common/Sorters.h index 836f2fa13..365192d51 100644 --- a/PowerEditor/src/MISC/Common/Sorters.h +++ b/PowerEditor/src/MISC/Common/Sorters.h @@ -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