1 /* Copyright 2021 Google LLC
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5 http://www.apache.org/licenses/LICENSE-2.0
6 Unless required by applicable law or agreed to in writing, software
7 distributed under the License is distributed on an "AS IS" BASIS,
8 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 See the License for the specific language governing permissions and
10 limitations under the License.
11 */
12
13 #include <fuzzer/FuzzedDataProvider.h>
14
15 // Returns a NULL-terminated C string that should be freed by the caller.
get_modifiable_string(FuzzedDataProvider & provider)16 char *get_modifiable_string(FuzzedDataProvider &provider) {
17 std::string s1 = provider.ConsumeRandomLengthString();
18 char *tmp = (char *)malloc(s1.size() + 1);
19 memcpy(tmp, s1.c_str(), s1.size());
20 tmp[s1.size()] = '\0';
21 return tmp;
22 }
23
24 FuzzedDataProvider *prov = NULL;
25
26
fuzz_get_random_data(void * buf,size_t len)27 extern "C" ssize_t fuzz_get_random_data(void *buf, size_t len) {
28 size_t ret_val;
29 char *cbuf = (char*)buf;
30
31 if (prov->remaining_bytes() == 0) {
32 return -1;
33 }
34
35 double prob = prov->ConsumeProbability<double>();
36 if (prob < 0.05) {
37 return 0;
38 }
39
40 if (len == 1) {
41 ret_val = prov->ConsumeData(buf, 1);
42 return ret_val;
43 }
44 ret_val = prov->ConsumeData(buf, len);
45 return ret_val;
46 }
47
48