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

Configure Feed

Select the types of activity you want to include in your feed.

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 <thorsten.blum@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250428210638.30219-2-thorsten.blum@linux.dev

authored by

Thorsten Blum and committed by
Andrii Nakryiko
7b05f431 358b1c0f

+3 -2
+3 -2
kernel/bpf/syscall.c
··· 36 36 #include <linux/memcontrol.h> 37 37 #include <linux/trace_events.h> 38 38 #include <linux/tracepoint.h> 39 + #include <linux/overflow.h> 39 40 40 41 #include <net/netfilter/nf_bpf_link.h> 41 42 #include <net/netkit.h> ··· 694 693 695 694 if (IS_ERR_OR_NULL(rec)) 696 695 return NULL; 697 - size = offsetof(struct btf_record, fields[rec->cnt]); 696 + size = struct_size(rec, fields, rec->cnt); 698 697 new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN); 699 698 if (!new_rec) 700 699 return ERR_PTR(-ENOMEM); ··· 749 748 return false; 750 749 if (rec_a->cnt != rec_b->cnt) 751 750 return false; 752 - size = offsetof(struct btf_record, fields[rec_a->cnt]); 751 + size = struct_size(rec_a, fields, rec_a->cnt); 753 752 /* btf_parse_fields uses kzalloc to allocate a btf_record, so unused 754 753 * members are zeroed out. So memcmp is safe to do without worrying 755 754 * about padding/unused fields.