This is an archive of the discontinued LLVM Phabricator instance.

Fix invalid overflow check in flang
ClosedPublic

Authored by serge-sans-paille on Mar 29 2022, 11:47 PM.

Details

Summary

tatically 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...

Diff Detail

Event Timeline

Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMar 29 2022, 11:47 PM
serge-sans-paille requested review of this revision.Mar 29 2022, 11:47 PM
rovka accepted this revision.Mar 30 2022, 2:02 AM

Right, good catch, thanks! I think it might be slightly clearer if we actually used std::numeric_limits::max in the comparison, but this also works.

This revision is now accepted and ready to land.Mar 30 2022, 2:02 AM
This revision was landed with ongoing or failed builds.Mar 30 2022, 7:47 AM
This revision was automatically updated to reflect the committed changes.