Index: compiler-rt/include/fuzzer/FuzzedDataProvider.h =================================================================== --- compiler-rt/include/fuzzer/FuzzedDataProvider.h +++ compiler-rt/include/fuzzer/FuzzedDataProvider.h @@ -14,6 +14,7 @@ #define LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_ #include +#include #include #include #include @@ -71,6 +72,7 @@ // Returns a value from the given array. template T PickValueInArray(const T (&array)[size]); + template T PickValueInArray(const std::array& array); template T PickValueInArray(std::initializer_list list); // Writes data to the given destination and returns number of bytes written. @@ -301,6 +303,12 @@ return array[ConsumeIntegralInRange(0, size - 1)]; } +template +T FuzzedDataProvider::PickValueInArray(const std::array& array) { + static_assert(size > 0, "The array must be non empty."); + return array[ConsumeIntegralInRange(0, size - 1)]; +} + template T FuzzedDataProvider::PickValueInArray(std::initializer_list list) { // TODO(Dor1s): switch to static_assert once C++14 is allowed. Index: compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp =================================================================== --- compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp +++ compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp @@ -286,6 +286,7 @@ TEST(FuzzedDataProvider, PickValueInArray) { FuzzedDataProvider DataProv(Data, sizeof(Data)); const int Array[] = {1, 2, 3, 4, 5}; + const std::array SameArray = {1, 2, 3, 4, 5}; EXPECT_EQ(5, DataProv.PickValueInArray(Array)); EXPECT_EQ(2, DataProv.PickValueInArray(Array)); EXPECT_EQ(2, DataProv.PickValueInArray(Array)); @@ -296,6 +297,16 @@ EXPECT_EQ(3, DataProv.PickValueInArray(Array)); EXPECT_EQ(2, DataProv.PickValueInArray(Array)); + EXPECT_EQ(5, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(2, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(2, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(3, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(3, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(3, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(1, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(3, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(2, DataProv.PickValueInArray(SameArray)); + EXPECT_EQ(uint8_t(0x9D), DataProv.PickValueInArray(Data)); EXPECT_EQ(uint8_t(0xBA), DataProv.PickValueInArray(Data)); EXPECT_EQ(uint8_t(0x69), DataProv.PickValueInArray(Data));