From c0d25bf9b8a2766e789b78a95f09af487957b327 Mon Sep 17 00:00:00 2001 From: Mike Aizatsky Date: Fri, 4 Mar 2016 23:18:01 +0000 Subject: [PATCH] [libfuzzer] adding std:string to allowed adaptable argument. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@262757 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Fuzzer/FuzzerFnAdapter.h | 11 +++++++++++ lib/Fuzzer/test/FuzzerFnAdapterUnittest.cpp | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Fuzzer/FuzzerFnAdapter.h b/lib/Fuzzer/FuzzerFnAdapter.h index eec378e7e3b..ae121d238dc 100644 --- a/lib/Fuzzer/FuzzerFnAdapter.h +++ b/lib/Fuzzer/FuzzerFnAdapter.h @@ -92,6 +92,17 @@ size_t UnpackSingle>(const uint8_t *Data, size_t Size, return Size - Len - 1; } +template <> +size_t UnpackSingle(const uint8_t *Data, size_t Size, + std::string *Value) { + if (Size < 1) + return Size; + size_t Len = std::min(static_cast(*Data), Size - 1); + std::string S(Data + 1, Data + 1 + Len); + Value->swap(S); + return Size - Len - 1; +} + // Unpacking into arbitrary tuple. // Recursion guard. diff --git a/lib/Fuzzer/test/FuzzerFnAdapterUnittest.cpp b/lib/Fuzzer/test/FuzzerFnAdapterUnittest.cpp index a2083d8a9f1..a8b5a149cff 100644 --- a/lib/Fuzzer/test/FuzzerFnAdapterUnittest.cpp +++ b/lib/Fuzzer/test/FuzzerFnAdapterUnittest.cpp @@ -70,6 +70,15 @@ TEST(Unpack, Vector) { EXPECT_EQ(8, V[1]); } +TEST(Unpack, String) { + std::tuple T; + const auto &S = std::get<0>(T); + + EXPECT_TRUE(Unpack(&T, {2, 3})); + EXPECT_EQ(1ul, S.size()); + EXPECT_EQ(3, S[0]); +} + template bool UnpackAndApply(Fn F, std::initializer_list Data) { std::vector V(Data);