Skip to content

Commit

Permalink
compact_array: Fix ubsan error (#284)
Browse files Browse the repository at this point in the history
Do not `memcpy` or `deallocate` if `old_capacity == 0`. `Array() == nullptr` in this case, so `memcpy` is UB.

Fixes #283 .
  • Loading branch information
MBkkt authored Nov 28, 2022
1 parent 254c136 commit e15d00b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/s2/util/gtl/compact_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit e15d00b

Please sign in to comment.