Skip to content

Commit 09fedc7

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Fix compilation warning with -Wparentheses
The kernel test robot reported compilation warnings when -Wparentheses is added to KBUILD_CFLAGS with gcc compiler. The following is the error message: .../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_reg_to_size_sx’: .../bpf-next/kernel/bpf/verifier.c:5901:14: error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses] if (s64_max >= 0 == s64_min >= 0) { ~~~~~~~~^~~~ .../bpf-next/kernel/bpf/verifier.c: In function ‘coerce_subreg_to_size_sx’: .../bpf-next/kernel/bpf/verifier.c:5965:14: error: suggest parentheses around comparison in operand of ‘==’ [-Werror=parentheses] if (s32_min >= 0 == s32_max >= 0) { ~~~~~~~~^~~~ To fix the issue, add proper parentheses for the above '>=' condition to silence the warning/error. I tried a few clang compilers like clang16 and clang18 and they do not emit such warnings with -Wparentheses. Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202307281133.wi0c4SqG-lkp@intel.com/ Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/r/20230728055740.2284534-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent f7e6bd3 commit 09fedc7

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

kernel/bpf/core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
18771877
case 1:
18781878
AX = abs((s32)DST);
18791879
do_div(AX, abs((s32)SRC));
1880-
if ((s32)DST < 0 == (s32)SRC < 0)
1880+
if (((s32)DST < 0) == ((s32)SRC < 0))
18811881
DST = (u32)AX;
18821882
else
18831883
DST = (u32)-AX;
@@ -1904,7 +1904,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)
19041904
case 1:
19051905
AX = abs((s32)DST);
19061906
do_div(AX, abs((s32)IMM));
1907-
if ((s32)DST < 0 == (s32)IMM < 0)
1907+
if (((s32)DST < 0) == ((s32)IMM < 0))
19081908
DST = (u32)AX;
19091909
else
19101910
DST = (u32)-AX;

kernel/bpf/verifier.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5898,7 +5898,7 @@ static void coerce_reg_to_size_sx(struct bpf_reg_state *reg, int size)
58985898
s64_min = min(init_s64_max, init_s64_min);
58995899

59005900
/* both of s64_max/s64_min positive or negative */
5901-
if (s64_max >= 0 == s64_min >= 0) {
5901+
if ((s64_max >= 0) == (s64_min >= 0)) {
59025902
reg->smin_value = reg->s32_min_value = s64_min;
59035903
reg->smax_value = reg->s32_max_value = s64_max;
59045904
reg->umin_value = reg->u32_min_value = s64_min;
@@ -5962,7 +5962,7 @@ static void coerce_subreg_to_size_sx(struct bpf_reg_state *reg, int size)
59625962
s32_max = max(init_s32_max, init_s32_min);
59635963
s32_min = min(init_s32_max, init_s32_min);
59645964

5965-
if (s32_min >= 0 == s32_max >= 0) {
5965+
if ((s32_min >= 0) == (s32_max >= 0)) {
59665966
reg->s32_min_value = s32_min;
59675967
reg->s32_max_value = s32_max;
59685968
reg->u32_min_value = (u32)s32_min;

0 commit comments

Comments
 (0)