Fix invalid overflow check in flang

Statically checking for overflow with

    if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) {
         return static_cast<std::int64_t>(length);
    }

Doesn't work if `sizeof(std::size_t) == sizeof(std::int64_t)` because std::size_t
is unsigned.

if `length == std::numeric_limits<size_t>` casting it to `int64_t` is going to overflow.

This code would be much simpler if returning a `uint64_t` instead of a signed
value...

Differential Revision: https://reviews.llvm.org/D122705
This commit is contained in:
serge-sans-paille 2022-03-30 08:17:56 +02:00
parent 5b38292d5d
commit c531171d99
1 changed files with 1 additions and 1 deletions

View File

@ -28,7 +28,7 @@ std::int32_t RTNAME(ArgumentCount)() {
// Returns the length of the \p string. Assumes \p string is valid.
static std::int64_t StringLength(const char *string) {
std::size_t length{std::strlen(string)};
if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) {
if constexpr (sizeof(std::size_t) < sizeof(std::int64_t)) {
return static_cast<std::int64_t>(length);
} else {
std::size_t max{std::numeric_limits<std::int64_t>::max()};