Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / base / json / string_escape_fuzzer.cc
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/json/string_escape.h"
6
7 #include <memory>
8
9 std::string escaped_string;
10
11 // Entry point for LibFuzzer.
12 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
13   if (size < 2)
14     return 0;
15
16   const bool put_in_quotes = data[size - 1];
17
18   // Create a copy of input buffer, as otherwise we don't catch
19   // overflow that touches the last byte (which is used in put_in_quotes).
20   size_t actual_size_char8 = size - 1;
21   std::unique_ptr<char[]> input(new char[actual_size_char8]);
22   memcpy(input.get(), data, actual_size_char8);
23
24   base::StringPiece input_string(input.get(), actual_size_char8);
25   base::EscapeJSONString(input_string, put_in_quotes, &escaped_string);
26
27   // Test for wide-strings if available size is even.
28   if (actual_size_char8 & 1)
29     return 0;
30
31   size_t actual_size_char16 = actual_size_char8 / 2;
32   base::StringPiece16 input_string16(
33       reinterpret_cast<base::char16*>(input.get()), actual_size_char16);
34   base::EscapeJSONString(input_string16, put_in_quotes, &escaped_string);
35
36   return 0;
37 }