1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <fuzzer/FuzzedDataProvider.h>
16
17 #include <cstdint>
18 #include <cstdlib>
19
20 #include "tensorflow/core/platform/stringprintf.h"
21
22 // This is a fuzzer for tensorflow::strings::Printf
23
24 namespace {
25
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)26 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
27 FuzzedDataProvider fuzzed_data(data, size);
28
29 const char split = fuzzed_data.ConsumeIntegral<char>();
30 const char split_a = split & 0x07;
31 const char split_b = (split >> 3) & 0x07;
32
33 const std::string ss[3] = {
34 fuzzed_data.ConsumeBytesAsString(split_a),
35 fuzzed_data.ConsumeBytesAsString(split_b),
36 fuzzed_data.ConsumeRemainingBytesAsString(),
37 };
38 const std::string all = ss[0] + ss[1] + ss[2];
39
40 int n[4] = {-1, -1, -1, -1};
41 const std::string ret =
42 tensorflow::strings::Printf("%n%s%n%s%n%s%n", &n[0], ss[0].c_str(), &n[1],
43 ss[1].c_str(), &n[2], ss[2].c_str(), &n[3]);
44
45 int size_so_far = 0;
46 for (int i = 0; i < 3; i++) {
47 assert(n[i] >= 0);
48 assert(n[i] <= size_so_far);
49 size_so_far += ss[i].size();
50 }
51
52 assert(n[3] >= 0);
53 assert(n[3] <= size_so_far);
54 assert(n[3] <= all.size());
55 assert(n[3] <= size - 1);
56 assert(ret.size() == n[3]);
57
58 return 0;
59 }
60
61 } // namespace
62