Skip to content

Commit c531171

Browse files
author
serge-sans-paille
committed
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
1 parent 5b38292 commit c531171

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

flang/runtime/command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ std::int32_t RTNAME(ArgumentCount)() {
2828
// Returns the length of the \p string. Assumes \p string is valid.
2929
static std::int64_t StringLength(const char *string) {
3030
std::size_t length{std::strlen(string)};
31-
if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) {
31+
if constexpr (sizeof(std::size_t) < sizeof(std::int64_t)) {
3232
return static_cast<std::int64_t>(length);
3333
} else {
3434
std::size_t max{std::numeric_limits<std::int64_t>::max()};

0 commit comments

Comments
 (0)