Skip to content

Commit 7b05f43

Browse files
tobluxanakryiko
authored andcommitted
bpf: Replace offsetof() with struct_size()
Compared to offsetof(), struct_size() provides additional compile-time checks for structs with flexible arrays (e.g., __must_be_array()). No functional changes intended. Signed-off-by: Thorsten Blum <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 358b1c0 commit 7b05f43

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

kernel/bpf/syscall.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/memcontrol.h>
3737
#include <linux/trace_events.h>
3838
#include <linux/tracepoint.h>
39+
#include <linux/overflow.h>
3940

4041
#include <net/netfilter/nf_bpf_link.h>
4142
#include <net/netkit.h>
@@ -693,7 +694,7 @@ struct btf_record *btf_record_dup(const struct btf_record *rec)
693694

694695
if (IS_ERR_OR_NULL(rec))
695696
return NULL;
696-
size = offsetof(struct btf_record, fields[rec->cnt]);
697+
size = struct_size(rec, fields, rec->cnt);
697698
new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN);
698699
if (!new_rec)
699700
return ERR_PTR(-ENOMEM);
@@ -748,7 +749,7 @@ bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *r
748749
return false;
749750
if (rec_a->cnt != rec_b->cnt)
750751
return false;
751-
size = offsetof(struct btf_record, fields[rec_a->cnt]);
752+
size = struct_size(rec_a, fields, rec_a->cnt);
752753
/* btf_parse_fields uses kzalloc to allocate a btf_record, so unused
753754
* members are zeroed out. So memcmp is safe to do without worrying
754755
* about padding/unused fields.

0 commit comments

Comments
 (0)