kcov: avoid clang out-of-range warning
authorArnd Bergmann <arnd@arndb.de>
Thu, 28 Mar 2024 14:30:42 +0000 (15:30 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 26 Apr 2024 04:07:04 +0000 (21:07 -0700)
The area_size is never larger than the maximum on 64-bit architectutes:

kernel/kcov.c:634:29: error: result of comparison of constant 1152921504606846975 with expression of type '__u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
                if (remote_arg->area_size > LONG_MAX / sizeof(unsigned long))
                    ~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The compiler can correctly optimize the check away and the code appears
correct to me, so just add a cast to avoid the warning.

Link: https://lkml.kernel.org/r/20240328143051.1069575-5-arnd@kernel.org
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Justin Stitt <justinstitt@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
kernel/kcov.c

index f9ac2e9e460fc816beb087f2d3ffb40dc063b10e..c3124f6d5536b7edd0f723e0dbff5d63d0a7766b 100644 (file)
@@ -627,7 +627,8 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
                mode = kcov_get_mode(remote_arg->trace_mode);
                if (mode < 0)
                        return mode;
-               if (remote_arg->area_size > LONG_MAX / sizeof(unsigned long))
+               if ((unsigned long)remote_arg->area_size >
+                   LONG_MAX / sizeof(unsigned long))
                        return -EINVAL;
                kcov->mode = mode;
                t->kcov = kcov;