• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google Inc.
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 ////////////////////////////////////////////////////////////////////////////////
16 
17 #include <string>
18 
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include "absl/strings/str_format.h"
22 #include "absl/strings/str_join.h"
23 #include "absl/strings/str_split.h"
24 #include "absl/strings/numbers.h"
25 #include "absl/strings/str_cat.h"
26 
27 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)28 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
29 	FuzzedDataProvider fuzzed_data(data, size);
30 	float float_value = fuzzed_data.ConsumeFloatingPoint<float>();
31 	double double_value = fuzzed_data.ConsumeFloatingPoint<double>();
32 	int int_value = fuzzed_data.ConsumeIntegral<int>();
33 	bool bool_value = fuzzed_data.ConsumeBool();
34 	std::string str1 = fuzzed_data.ConsumeRandomLengthString();
35 	std::string str2 = fuzzed_data.ConsumeRemainingBytesAsString();
36 
37 	std::string float_str = absl::StrFormat("%g", float_value);
38 	std::string double_str = absl::StrFormat("%g", double_value);
39 	std::string int_str = absl::StrFormat("%d", int_value);
40 	std::string bool_str = absl::StrFormat("%d", bool_value);
41 
42 	if (!absl::SimpleAtof(float_str, &float_value))
43 		return 0;
44 	if (!absl::SimpleAtod(double_str, &double_value))
45 		return 0;
46 	if (!absl::SimpleAtoi(int_str, &int_value))
47 		return 0;
48 	if (!absl::SimpleAtob(bool_str, &bool_value))
49 		return 0;
50 
51 	absl::StrAppend(&str1, str2);
52 	std::string str_result = absl::StrCat(str1, float_value, double_value, int_value, bool_value);
53 	std::vector<std::string> v = absl::StrSplit(str_result, ".");
54 	absl::StrJoin(v, ".");
55 	return 0;
56 }