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>
This commit is contained in:
Antonio Maiorano 2022-08-09 02:57:00 +00:00 committed by Dawn LUCI CQ
parent 953d610aa2
commit 1884c964d7
2 changed files with 13 additions and 1 deletions

View File

@ -17,6 +17,7 @@
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <array>
#include <iterator>
#include <utility>
@ -461,7 +462,7 @@ class Vector {
}
/// 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) {

View File

@ -151,6 +151,17 @@ TEST(TintVectorTest, InitializerList_NoSmallArray) {
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*>);