1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef LIBOSI_FUZZ_HELPERS_H_
18 #define LIBOSI_FUZZ_HELPERS_H_
19
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <vector>
22
generateBuffer(FuzzedDataProvider * dataProvider,size_t max_buffer_size,bool null_terminate)23 char* generateBuffer(FuzzedDataProvider* dataProvider, size_t max_buffer_size,
24 bool null_terminate) {
25 // Get our buffer size
26 size_t buf_size =
27 dataProvider->ConsumeIntegralInRange<size_t>(0, max_buffer_size);
28 if (buf_size == 0) {
29 return nullptr;
30 }
31
32 // Allocate and copy in data
33 char* buf = reinterpret_cast<char*>(malloc(buf_size));
34 std::vector<char> bytes = dataProvider->ConsumeBytes<char>(buf_size);
35 memcpy(buf, bytes.data(), bytes.size());
36
37 if (null_terminate) {
38 // Force a null-termination
39 buf[buf_size - 1] = 0x00;
40 }
41
42 return buf;
43 }
44
45 #endif // LIBOSI_FUZZ_HELPERS_H_
46