1 /******************************************************************************
2 *
3 * Copyright (C) 2022 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 */
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #include "minikin/Measurement.h"
22 using namespace minikin;
23
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)24 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
25 FuzzedDataProvider fdp(data, size);
26 float advance = fdp.ConsumeFloatingPoint<float>();
27 int sizeof_size = (int)sizeof(size_t);
28 int sizeof_uint16 = (int)sizeof(uint16_t);
29 int sizeof_float = (int)sizeof(float);
30 int remaining = fdp.remaining_bytes();
31 int limit = (int)((remaining - 3 * sizeof_size) / sizeof_uint16);
32 limit = (limit < 0) ? 0 : limit;
33 int buf_size = fdp.ConsumeIntegralInRange<int>(0, limit);
34 if (buf_size == 0) return 0;
35 uint16_t buf[buf_size];
36 for (int i = 0; i < buf_size; i++) buf[i] = fdp.ConsumeIntegral<uint16_t>();
37 size_t start = fdp.ConsumeIntegralInRange<size_t>(0, buf_size - 1);
38 size_t count = fdp.ConsumeIntegralInRange<size_t>(0, buf_size - 1 - start);
39 size_t offset = fdp.ConsumeIntegralInRange<size_t>(start, start + count);
40 remaining = fdp.remaining_bytes();
41 if (remaining / sizeof_float < count) return 0;
42 if (offset == start + count) return 0;
43 int advances_size = count;
44 float advances[advances_size];
45 for (int i = 0; i < advances_size; i++) advances[i] = fdp.ConsumeFloatingPoint<float>();
46 float advance_run = getRunAdvance(advances, buf, start, count, offset);
47 size_t advance_offset = getOffsetForAdvance(advances, buf, start, count, advance);
48 return 0;
49 }
50