Skip to content

Commit 7413ab7

Browse files
keesKent Overstreet
authored andcommitted
bcachefs: Refactor memcpy into direct assignment
The memcpy() in bch2_bkey_append_ptr() is operating on an embedded fake flexible array which looks to the compiler like it has 0 size. This causes W=1 builds to emit warnings due to -Wstringop-overflow: In file included from include/linux/string.h:254, from include/linux/bitmap.h:11, from include/linux/cpumask.h:12, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/radix-tree.h:14, from include/linux/backing-dev-defs.h:6, from fs/bcachefs/bcachefs.h:182: fs/bcachefs/extents.c: In function 'bch2_bkey_append_ptr': include/linux/fortify-string.h:57:33: warning: writing 8 bytes into a region of size 0 [-Wstringop-overflow=] 57 | #define __underlying_memcpy __builtin_memcpy | ^ include/linux/fortify-string.h:648:9: note: in expansion of macro '__underlying_memcpy' 648 | __underlying_##op(p, q, __fortify_size); \ | ^~~~~~~~~~~~~ include/linux/fortify-string.h:693:26: note: in expansion of macro '__fortify_memcpy_chk' 693 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ | ^~~~~~~~~~~~~~~~~~~~ fs/bcachefs/extents.c:235:17: note: in expansion of macro 'memcpy' 235 | memcpy((void *) &k->v + bkey_val_bytes(&k->k), | ^~~~~~ fs/bcachefs/bcachefs_format.h:287:33: note: destination object 'v' of size 0 287 | struct bch_val v; | ^ Avoid making any structure changes and just replace the u64 copy into a direct assignment, side-stepping the entire problem. Cc: Kent Overstreet <[email protected]> Cc: Brian Foster <[email protected]> Cc: [email protected] Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Kent Overstreet <[email protected]>
1 parent 795413c commit 7413ab7

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

fs/bcachefs/extents.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ void bch2_bkey_extent_entry_drop(struct bkey_i *, union bch_extent_entry *);
633633

634634
static inline void bch2_bkey_append_ptr(struct bkey_i *k, struct bch_extent_ptr ptr)
635635
{
636+
struct bch_extent_ptr *dest;
637+
636638
EBUG_ON(bch2_bkey_has_device(bkey_i_to_s(k), ptr.dev));
637639

638640
switch (k->k.type) {
@@ -642,12 +644,8 @@ static inline void bch2_bkey_append_ptr(struct bkey_i *k, struct bch_extent_ptr
642644
EBUG_ON(bkey_val_u64s(&k->k) >= BKEY_EXTENT_VAL_U64s_MAX);
643645

644646
ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
645-
646-
unsafe_memcpy((void *) &k->v + bkey_val_bytes(&k->k),
647-
&ptr,
648-
sizeof(ptr),
649-
"Our memcpy target is relative to a zero size array ,"
650-
"compiler bounds checking doesn't work here");
647+
dest = (struct bch_extent_ptr *)((void *) &k->v + bkey_val_bytes(&k->k));
648+
*dest = ptr;
651649
k->k.u64s++;
652650
break;
653651
default:

0 commit comments

Comments
 (0)