Skip to content

Commit 0af02eb

Browse files
FlorentRevestborkmann
authored andcommitted
bpf: Avoid using ARRAY_SIZE on an uninitialized pointer
The cppcheck static code analysis reported the following error: if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) { ^ ARRAY_SIZE is a macro that expands to sizeofs, so bufs is not actually dereferenced at runtime, and the code is actually safe. But to keep things tidy, this patch removes the need for a call to ARRAY_SIZE by extracting the size of the array into a macro. Cppcheck should no longer be confused and the code ends up being a bit cleaner. Fixes: e2d5b2b ("bpf: Fix nested bpf_bprintf_prepare with more per-cpu buffers") Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Florent Revest <revest@chromium.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Song Liu <song@kernel.org> Link: https://lore.kernel.org/bpf/20210517092830.1026418-2-revest@chromium.org
1 parent 8afcc19 commit 0af02eb

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

‎kernel/bpf/helpers.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,9 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype,
698698
#define MAX_BPRINTF_BUF_LEN 512
699699

700700
/* Support executing three nested bprintf helper calls on a given CPU */
701+
#define MAX_BPRINTF_NEST_LEVEL 3
701702
struct bpf_bprintf_buffers {
702-
char tmp_bufs[3][MAX_BPRINTF_BUF_LEN];
703+
char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN];
703704
};
704705
static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs);
705706
static DEFINE_PER_CPU(int, bpf_bprintf_nest_level);
@@ -711,7 +712,7 @@ static int try_get_fmt_tmp_buf(char **tmp_buf)
711712

712713
preempt_disable();
713714
nest_level = this_cpu_inc_return(bpf_bprintf_nest_level);
714-
if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) {
715+
if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) {
715716
this_cpu_dec(bpf_bprintf_nest_level);
716717
preempt_enable();
717718
return -EBUSY;

0 commit comments

Comments
 (0)