From c531171d995728f55e5775d354a21dceed03edce Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Wed, 30 Mar 2022 08:17:56 +0200 Subject: [PATCH] Fix invalid overflow check in flang Statically checking for overflow with if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) { return static_cast(length); } Doesn't work if `sizeof(std::size_t) == sizeof(std::int64_t)` because std::size_t is unsigned. if `length == std::numeric_limits` 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 --- flang/runtime/command.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp index 311107ee7dfa..fe54da2e6d68 100644 --- a/flang/runtime/command.cpp +++ b/flang/runtime/command.cpp @@ -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(length); } else { std::size_t max{std::numeric_limits::max()};