bpf: Avoid uninitialized value in BPF_CORE_READ_BITFIELD
authorJose E. Marchesi <jose.marchesi@oracle.com>
Wed, 8 May 2024 10:13:13 +0000 (12:13 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 8 May 2024 22:00:55 +0000 (15:00 -0700)
commit009367099eb61a4fc2af44d4eb06b6b4de7de6db
treefe2af65273e1e7981a195649bf2770c2cac00298
parent911edc69c832161b62a8ad10a6972290157a7bd3
bpf: Avoid uninitialized value in BPF_CORE_READ_BITFIELD

[Changes from V1:
 - Use a default branch in the switch statement to initialize `val'.]

GCC warns that `val' may be used uninitialized in the
BPF_CRE_READ_BITFIELD macro, defined in bpf_core_read.h as:

[...]
unsigned long long val;       \
[...]       \
switch (__CORE_RELO(s, field, BYTE_SIZE)) {       \
case 1: val = *(const unsigned char *)p; break;       \
case 2: val = *(const unsigned short *)p; break;       \
case 4: val = *(const unsigned int *)p; break;       \
case 8: val = *(const unsigned long long *)p; break;       \
        }              \
[...]
val;       \
}       \

This patch adds a default entry in the switch statement that sets
`val' to zero in order to avoid the warning, and random values to be
used in case __builtin_preserve_field_info returns unexpected values
for BPF_FIELD_BYTE_SIZE.

Tested in bpf-next master.
No regressions.

Signed-off-by: Jose E. Marchesi <jose.marchesi@oracle.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240508101313.16662-1-jose.marchesi@oracle.com
tools/lib/bpf/bpf_core_read.h