[PATCH] D38132: [Support] mapped_file_region: avoid tautological comparison.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 21 06:56:36 PDT 2017


I would move this logic into the init function.

But there's another problem. mmap doesn't even accept a size_t, it accepts
an off_t. Also, as far as I'm aware, off_t is a signed type. So perhaps you
can compare against std::numeric_limits<off_t>::max() which will never be a
tautological comparison
On Thu, Sep 21, 2017 at 6:41 AM Roman Lebedev via Phabricator <
reviews at reviews.llvm.org> wrote:

> lebedev.ri created this revision.
>
> Found when testing stage-2 build with https://reviews.llvm.org/D38101.
>
>   In file included from /build/llvm/lib/Support/Path.cpp:1045:
>   /build/llvm/lib/Support/Unix/Path.inc:648:14: error: comparison
> 'uint64_t' (aka 'unsigned long') > 18446744073709551615 is always false
> [-Werror,-Wtautological-constant-compare]
>     if (length > std::numeric_limits<size_t>::max()) {
>         ~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> `size_t` is `uint64_t` here, apparently, thus any `uint64_t` value
> always fits into `size_t`.
>
> I suppose there may be cases where `size_t` is smaller (32-bit?),
> so i wrapped the `if()` into preprocessor `#if`. Not sure what
> better solution there could be.
>
>
> Repository:
>   rL LLVM
>
> https://reviews.llvm.org/D38132
>
> Files:
>   lib/Support/Unix/Path.inc
>
>
> Index: lib/Support/Unix/Path.inc
> ===================================================================
> --- lib/Support/Unix/Path.inc
> +++ lib/Support/Unix/Path.inc
> @@ -644,11 +644,16 @@
>  mapped_file_region::mapped_file_region(int fd, mapmode mode, uint64_t
> length,
>                                         uint64_t offset, std::error_code
> &ec)
>      : Size(length), Mapping() {
> +  static_assert(std::numeric_limits<size_t>::max() == SIZE_MAX, "");
> +  static_assert(std::numeric_limits<uint64_t>::max() == UINT64_MAX, "");
> +
> +#if UINT64_MAX > SIZE_MAX
>    // Make sure that the requested size fits within SIZE_T.
>    if (length > std::numeric_limits<size_t>::max()) {
>      ec = make_error_code(errc::invalid_argument);
>      return;
>    }
> +#endif
>
>    ec = init(fd, offset, mode);
>    if (ec)
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170921/12208350/attachment.html>


More information about the llvm-commits mailing list