• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include <fuzzer/FuzzedDataProvider.h>
17 
18 #include <cppbor.h>
19 #include <cppbor_parse.h>
20 
FuzzStringParsing(const uint8_t * data,size_t size)21 void FuzzStringParsing(const uint8_t* data, size_t size) {
22     FuzzedDataProvider dataProvider(data, size);
23     std::string contents = dataProvider.ConsumeRemainingBytesAsString();
24     cppbor::Tstr val(contents);
25     cppbor::parse(val.encode());
26 }
27 
FuzzVectorParsing(const uint8_t * data,size_t size)28 void FuzzVectorParsing(const uint8_t* data, size_t size) {
29     FuzzedDataProvider dataProvider(data, size);
30     size_t keySize = dataProvider.remaining_bytes() / 4;
31     std::string key1 = dataProvider.ConsumeBytesAsString(keySize);
32     std::string key2 = dataProvider.ConsumeBytesAsString(keySize);
33     std::vector<uint8_t> contentsBytes = dataProvider.ConsumeRemainingBytes<uint8_t>();
34     cppbor::Map map;
35     map.add(key1, cppbor::Array().add(cppbor::Map().add(key2, contentsBytes)));
36     cppbor::parse(map.encode());
37 }
38 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)39 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
40     FuzzStringParsing(data, size);
41     FuzzVectorParsing(data, size);
42     return 0;
43 }
44