Skip to content

Commit

Permalink
Fix invalid overflow check in flang
Browse files Browse the repository at this point in the history
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
  • Loading branch information
serge-sans-paille committed Mar 30, 2022
1 parent 5b38292 commit c531171
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion flang/runtime/command.cpp
Expand Up @@ -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()};
Expand Down

0 comments on commit c531171

Please sign in to comment.