Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Fix a tautological-constant-out-of-range-compare compiler warning

When using clang to build selftests with LLVM=1 in make commandline,
I hit the following compiler warning:

benchs/bench_bloom_filter_map.c:84:46: warning: result of comparison of constant 256
with expression of type '__u8' (aka 'unsigned char') is always false
[-Wtautological-constant-out-of-range-compare]
if (args.value_size < 2 || args.value_size > 256) {
~~~~~~~~~~~~~~~ ^ ~~~

The reason is arg.vaue_size has type __u8, so comparison "args.value_size > 256"
is always false.

This patch fixed the issue by doing proper comparison before assigning the
value to args.value_size. The patch also fixed the same issue in two
other places.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211112204838.3579953-1-yhs@fb.com

authored by

Yonghong Song and committed by
Alexei Starovoitov
325d956d 21c6ec3d

+11 -6
+11 -6
tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c
··· 63 63 64 64 static error_t parse_arg(int key, char *arg, struct argp_state *state) 65 65 { 66 + long ret; 67 + 66 68 switch (key) { 67 69 case ARG_NR_ENTRIES: 68 - args.nr_entries = strtol(arg, NULL, 10); 69 - if (args.nr_entries == 0) { 70 + ret = strtol(arg, NULL, 10); 71 + if (ret < 1 || ret > UINT_MAX) { 70 72 fprintf(stderr, "Invalid nr_entries count."); 71 73 argp_usage(state); 72 74 } 75 + args.nr_entries = ret; 73 76 break; 74 77 case ARG_NR_HASH_FUNCS: 75 - args.nr_hash_funcs = strtol(arg, NULL, 10); 76 - if (args.nr_hash_funcs == 0 || args.nr_hash_funcs > 15) { 78 + ret = strtol(arg, NULL, 10); 79 + if (ret < 1 || ret > 15) { 77 80 fprintf(stderr, 78 81 "The bloom filter must use 1 to 15 hash functions."); 79 82 argp_usage(state); 80 83 } 84 + args.nr_hash_funcs = ret; 81 85 break; 82 86 case ARG_VALUE_SIZE: 83 - args.value_size = strtol(arg, NULL, 10); 84 - if (args.value_size < 2 || args.value_size > 256) { 87 + ret = strtol(arg, NULL, 10); 88 + if (ret < 2 || ret > 256) { 85 89 fprintf(stderr, 86 90 "Invalid value size. Must be between 2 and 256 bytes"); 87 91 argp_usage(state); 88 92 } 93 + args.value_size = ret; 89 94 break; 90 95 default: 91 96 return ARGP_ERR_UNKNOWN;