tint: Fix utils::Vector<T, 0>::Push segfaulting
Grow multiplies capacity by 2, but for a 0-sized Vector (i.e. no small
array), capacity is 0, so Grow wouldn't grow.
Bug: tint:1613
Change-Id: I6f2954cbfdb0c638e02b2f441e17a016c0198ad7
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/98540
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/utils/vector.h b/src/tint/utils/vector.h
index ee305e2..518a6938 100644
--- a/src/tint/utils/vector.h
+++ b/src/tint/utils/vector.h
@@ -17,6 +17,7 @@
#include <stddef.h>
#include <stdint.h>
+#include <algorithm>
#include <array>
#include <iterator>
#include <utility>
@@ -461,7 +462,7 @@
}
/// Expands the capacity of the vector
- void Grow() { Reserve(impl_.slice.cap * 2); }
+ void Grow() { Reserve(std::max(impl_.slice.cap, static_cast<size_t>(1)) * 2); }
/// Moves 'other' to this vector, if possible, otherwise performs a copy.
void MoveOrCopy(VectorRef<T>&& other) {
diff --git a/src/tint/utils/vector_test.cc b/src/tint/utils/vector_test.cc
index 24cfcbe..b4e9fb5 100644
--- a/src/tint/utils/vector_test.cc
+++ b/src/tint/utils/vector_test.cc
@@ -151,6 +151,17 @@
EXPECT_TRUE(AllExternallyHeld(vec));
}
+TEST(TintVectorTest, Push_NoSmallArray) {
+ Vector<std::string, 0> vec;
+ vec.Push("one");
+ vec.Push("two");
+ EXPECT_EQ(vec.Length(), 2u);
+ EXPECT_EQ(vec.Capacity(), 2u);
+ EXPECT_EQ(vec[0], "one");
+ EXPECT_EQ(vec[1], "two");
+ EXPECT_TRUE(AllExternallyHeld(vec));
+}
+
TEST(TintVectorTest, InferTN_1CString) {
auto vec = Vector{"one"};
static_assert(std::is_same_v<decltype(vec)::value_type, const char*>);