Subzero: fix Ice::BitVector::grow not copying old to new data
UBSAN pointed out that memcpy cannot have nullptr dest or source,
which I've also fixed; however, this also made me realize a latent bug:
the dest and source args needed to be swapped. It seems this bug hasn't
bit us yet because the normal use-case is to resize the bitvector, and
then call 'set' to set all bits to 0 or 1. In any case, still worth
fixing.
Bug: b/171581990
Change-Id: Iaaa7e019b53ae661f49c97195f2d144b5715f164
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/49528
Presubmit-Ready: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/third_party/subzero/src/IceBitVector.h b/third_party/subzero/src/IceBitVector.h
index f81810f..742d2b4 100644
--- a/third_party/subzero/src/IceBitVector.h
+++ b/third_party/subzero/src/IceBitVector.h
@@ -768,8 +768,10 @@
Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
assert(Capacity > 0 && "realloc-ing zero space");
auto *NewBits = Alloc.allocate(Capacity);
- std::memcpy(Bits, NewBits, OldCapacity * sizeof(BitWord));
- Alloc.deallocate(Bits, OldCapacity);
+ if (Bits) {
+ std::memcpy(NewBits, Bits, OldCapacity * sizeof(BitWord));
+ Alloc.deallocate(Bits, OldCapacity);
+ }
Bits = NewBits;
clear_unused_bits();