diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2022-07-03 14:10:23 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2022-07-03 14:10:23 +0000 |
commit | 145449b1e420787bb99721a429341fa6be3adfb6 (patch) | |
tree | 1d56ae694a6de602e348dd80165cf881a36600ed /llvm/lib/Support/JSON.cpp | |
parent | ecbca9f5fb7d7613d2b94982c4825eb0d33d6842 (diff) |
Diffstat (limited to 'llvm/lib/Support/JSON.cpp')
-rw-r--r-- | llvm/lib/Support/JSON.cpp | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/llvm/lib/Support/JSON.cpp b/llvm/lib/Support/JSON.cpp index 20babbe56d86..b87e39f0a963 100644 --- a/llvm/lib/Support/JSON.cpp +++ b/llvm/lib/Support/JSON.cpp @@ -509,13 +509,25 @@ bool Parser::parseNumber(char First, Value &Out) { S.push_back(next()); char *End; // Try first to parse as integer, and if so preserve full 64 bits. - // strtoll returns long long >= 64 bits, so check it's in range too. - auto I = std::strtoll(S.c_str(), &End, 10); - if (End == S.end() && I >= std::numeric_limits<int64_t>::min() && - I <= std::numeric_limits<int64_t>::max()) { + // We check for errno for out of bounds errors and for End == S.end() + // to make sure that the numeric string is not malformed. + errno = 0; + int64_t I = std::strtoll(S.c_str(), &End, 10); + if (End == S.end() && errno != ERANGE) { Out = int64_t(I); return true; } + // strtroull has a special handling for negative numbers, but in this + // case we don't want to do that because negative numbers were already + // handled in the previous block. + if (First != '-') { + errno = 0; + uint64_t UI = std::strtoull(S.c_str(), &End, 10); + if (End == S.end() && errno != ERANGE) { + Out = UI; + return true; + } + } // If it's not an integer Out = std::strtod(S.c_str(), &End); return End == S.end() || parseError("Invalid JSON value (number?)"); |