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/20250503151513.343931-2-thorsten.blum@linux.dev

authored by

Thorsten Blum and committed by
Andrii Nakryiko
41948afc 41d4ce6d

+9 -10
+9 -10
kernel/bpf/btf.c
··· 26 26 #include <linux/bsearch.h> 27 27 #include <linux/kobject.h> 28 28 #include <linux/sysfs.h> 29 + #include <linux/overflow.h> 29 30 30 31 #include <net/netfilter/nf_bpf_link.h> 31 32 ··· 3958 3957 /* This needs to be kzalloc to zero out padding and unused fields, see 3959 3958 * comment in btf_record_equal. 3960 3959 */ 3961 - rec = kzalloc(offsetof(struct btf_record, fields[cnt]), GFP_KERNEL | __GFP_NOWARN); 3960 + rec = kzalloc(struct_size(rec, fields, cnt), GFP_KERNEL | __GFP_NOWARN); 3962 3961 if (!rec) 3963 3962 return ERR_PTR(-ENOMEM); 3964 3963 ··· 5584 5583 if (id < 0) 5585 5584 continue; 5586 5585 5587 - new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]), 5586 + new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1), 5588 5587 GFP_KERNEL | __GFP_NOWARN); 5589 5588 if (!new_aof) { 5590 5589 ret = -ENOMEM; ··· 5611 5610 if (ret != BTF_FIELD_FOUND) 5612 5611 continue; 5613 5612 5614 - new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]), 5613 + new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1), 5615 5614 GFP_KERNEL | __GFP_NOWARN); 5616 5615 if (!new_aof) { 5617 5616 ret = -ENOMEM; ··· 5648 5647 continue; 5649 5648 parse: 5650 5649 tab_cnt = tab ? tab->cnt : 0; 5651 - new_tab = krealloc(tab, offsetof(struct btf_struct_metas, types[tab_cnt + 1]), 5650 + new_tab = krealloc(tab, struct_size(new_tab, types, tab_cnt + 1), 5652 5651 GFP_KERNEL | __GFP_NOWARN); 5653 5652 if (!new_tab) { 5654 5653 ret = -ENOMEM; ··· 8560 8559 8561 8560 /* Grow set */ 8562 8561 set = krealloc(tab->sets[hook], 8563 - offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]), 8562 + struct_size(set, pairs, set_cnt + add_set->cnt), 8564 8563 GFP_KERNEL | __GFP_NOWARN); 8565 8564 if (!set) { 8566 8565 ret = -ENOMEM; ··· 8846 8845 } 8847 8846 8848 8847 tab = krealloc(btf->dtor_kfunc_tab, 8849 - offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]), 8848 + struct_size(tab, dtors, tab_cnt + add_cnt), 8850 8849 GFP_KERNEL | __GFP_NOWARN); 8851 8850 if (!tab) { 8852 8851 ret = -ENOMEM; ··· 9404 9403 9405 9404 tab = btf->struct_ops_tab; 9406 9405 if (!tab) { 9407 - tab = kzalloc(offsetof(struct btf_struct_ops_tab, ops[4]), 9408 - GFP_KERNEL); 9406 + tab = kzalloc(struct_size(tab, ops, 4), GFP_KERNEL); 9409 9407 if (!tab) 9410 9408 return -ENOMEM; 9411 9409 tab->capacity = 4; ··· 9417 9417 9418 9418 if (tab->cnt == tab->capacity) { 9419 9419 new_tab = krealloc(tab, 9420 - offsetof(struct btf_struct_ops_tab, 9421 - ops[tab->capacity * 2]), 9420 + struct_size(tab, ops, tab->capacity * 2), 9422 9421 GFP_KERNEL); 9423 9422 if (!new_tab) 9424 9423 return -ENOMEM;