From e15d00b21e9bbc252e436c243710cb9712465814 Mon Sep 17 00:00:00 2001 From: Valery Mironov <32071355+MBkkt@users.noreply.github.com> Date: Mon, 28 Nov 2022 08:38:19 +0100 Subject: [PATCH] compact_array: Fix ubsan error (#284) Do not `memcpy` or `deallocate` if `old_capacity == 0`. `Array() == nullptr` in this case, so `memcpy` is UB. Fixes #283 . --- src/s2/util/gtl/compact_array.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/s2/util/gtl/compact_array.h b/src/s2/util/gtl/compact_array.h index cbc4fe9a..033db144 100644 --- a/src/s2/util/gtl/compact_array.h +++ b/src/s2/util/gtl/compact_array.h @@ -413,8 +413,10 @@ class compact_array_base { value_allocator_type allocator; T* new_ptr = allocator.allocate(capacity()); - memcpy(new_ptr, Array(), old_capacity * sizeof(T)); - allocator.deallocate(Array(), old_capacity); + if (old_capacity != 0) { + memcpy(new_ptr, Array(), old_capacity * sizeof(T)); + allocator.deallocate(Array(), old_capacity); + } SetArray(new_ptr); }