From e529f7bcf338cf5a98c3d79e0c1ae3bf33811891 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 11:52:14 +0300 Subject: [PATCH] Fix zero-length memcpy from NULL found by UBSan memcpy() with a NULL pointer is undefined even when the length is 0, and UBSan reports it as "null pointer passed as argument 2". Skip the copy when there is nothing to copy: - Store v2 flusher: a zero-length small write has no checksums, so get_checksums() returns NULL when compaction merges them - Store v2 list_objects(): the unstable or the tombstone list is NULL when only the other one is non-empty - OSD sync with immediate_commit != all: a dirty PG without copies to delete after sync has an empty vector whose data() is NULL, while another PG has some - disk mock: an empty iovec of a write may have a NULL iov_base Found by test_pg_chaos --seed 18, test_blockstore and test_heap built with WITH_UBSAN=yes. --- src/blockstore/blockstore_flush.cpp | 5 +++-- src/blockstore/blockstore_heap.cpp | 6 ++++-- src/osd/osd_primary_sync.cpp | 2 +- src/test/ringloop_mock.cpp | 3 ++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/blockstore/blockstore_flush.cpp b/src/blockstore/blockstore_flush.cpp index 837ceafa..330caaa5 100644 --- a/src/blockstore/blockstore_flush.cpp +++ b/src/blockstore/blockstore_flush.cpp @@ -376,8 +376,9 @@ resume_28: for (size_t i = csum_copy.size(); i > 0; i--) { auto wr = csum_copy[i-1]; - memcpy(new_csums + wr->small().offset/bs->dsk.csum_block_size*(bs->dsk.data_csum_type & 0xFF), - wr->get_checksums(bs->heap), wr->small().len/bs->dsk.csum_block_size*(bs->dsk.data_csum_type & 0xFF)); + if (wr->small().len > 0) + memcpy(new_csums + wr->small().offset/bs->dsk.csum_block_size*(bs->dsk.data_csum_type & 0xFF), + wr->get_checksums(bs->heap), wr->small().len/bs->dsk.csum_block_size*(bs->dsk.data_csum_type & 0xFF)); } csum_copy.clear(); } diff --git a/src/blockstore/blockstore_heap.cpp b/src/blockstore/blockstore_heap.cpp index b93a8e32..e2615e32 100644 --- a/src/blockstore/blockstore_heap.cpp +++ b/src/blockstore/blockstore_heap.cpp @@ -2674,8 +2674,10 @@ int blockstore_heap_t::list_objects(uint32_t pg_num, object_id min_oid, object_i res_alloc = res_size+unstable_size+tombstone_size; res = (obj_ver_id*)realloc_or_die(res, sizeof(obj_ver_id) * res_alloc); } - memcpy(res + res_size, unstable, sizeof(obj_ver_id) * unstable_size); - memcpy(res + res_size+unstable_size, tombstone, sizeof(obj_ver_id) * tombstone_size); + if (unstable_size > 0) + memcpy(res + res_size, unstable, sizeof(obj_ver_id) * unstable_size); + if (tombstone_size > 0) + memcpy(res + res_size+unstable_size, tombstone, sizeof(obj_ver_id) * tombstone_size); free(unstable); free(tombstone); } diff --git a/src/osd/osd_primary_sync.cpp b/src/osd/osd_primary_sync.cpp index e78c17c1..4a3ae72c 100644 --- a/src/osd/osd_primary_sync.cpp +++ b/src/osd/osd_primary_sync.cpp @@ -110,7 +110,7 @@ resume_2: pg.inflight++; op_data->dirty_pgs[dpg] = dirty_pg_num; pg_del_counts[dpg] = pg.copies_to_delete_after_sync.size(); - if (this->copies_to_delete_after_sync_count) + if (this->copies_to_delete_after_sync_count && pg.copies_to_delete_after_sync.size() > 0) { assert(pg.copies_to_delete_after_sync.size() <= this->copies_to_delete_after_sync_count - op_data->copies_to_delete_count); memcpy( diff --git a/src/test/ringloop_mock.cpp b/src/test/ringloop_mock.cpp index 44db1f41..9af36d87 100644 --- a/src/test/ringloop_mock.cpp +++ b/src/test/ringloop_mock.cpp @@ -356,7 +356,8 @@ ssize_t disk_mock_t::copy_from_sqe(io_uring_sqe *sqe, uint8_t *to, uint64_t base cur = limit-done; if (trace) printf("%s: write %zu+%zu from %jx\n", name.c_str(), off, cur, (uint64_t)v[i].iov_base); - memcpy(to + off - base_offset, v[i].iov_base, cur); + if (cur > 0) + memcpy(to + off - base_offset, v[i].iov_base, cur); off += v[i].iov_len; done += cur; } -- 2.47.3