Skip to content

Commit

Permalink
Trim hex strings before converting to U64
Browse files Browse the repository at this point in the history
This ensures that we trim leading zeros off the string such that
the primitive checks for converting into a U64 that do a bounds check
against the length of the string vs the largest possible number to
be stored work more permissively with large 0 padded strings.
  • Loading branch information
Doy-lee committed Jun 5, 2024
1 parent 8241f14 commit 1b3b5bc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ std::string_view utils::trimLeadingZeros(std::string_view src) {
}

uint64_t utils::hexStringToU64(std::string_view hexStr) {
hexStr = trimPrefix(hexStr, "0x");
hexStr = trimPrefix(hexStr, "0X");
hexStr = trimLeadingZeros(hexStr);

uint64_t val;
if (parseInt(trimPrefix(hexStr, "0x"), val, 16))
if (parseInt(hexStr, val, 16))
return val;

throw std::invalid_argument{"failed to parse integer from hex input"};
throw std::invalid_argument{"failed to parse integer from hex input: " + std::string(hexStr)};
}

template std::vector<char> utils::fromHexString<char>(std::string_view);
Expand Down

0 comments on commit 1b3b5bc

Please sign in to comment.