[flang-commits] [flang] c531171 - Fix invalid overflow check in flang

via flang-commits flang-commits at lists.llvm.org
Wed Mar 30 07:47:47 PDT 2022


Author: serge-sans-paille
Date: 2022-03-30T16:47:33+02:00
New Revision: c531171d995728f55e5775d354a21dceed03edce

URL: https://github.com/llvm/llvm-project/commit/c531171d995728f55e5775d354a21dceed03edce
DIFF: https://github.com/llvm/llvm-project/commit/c531171d995728f55e5775d354a21dceed03edce.diff

LOG: 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

Added: 
    

Modified: 
    flang/runtime/command.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp
index 311107ee7dfad..fe54da2e6d68b 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<std::int64_t>(length);
   } else {
     std::size_t max{std::numeric_limits<std::int64_t>::max()};


        


More information about the flang-commits mailing list