1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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 #include "cfcreate_fuzzer.h"
17
18 #include <securec.h>
19
20 #include "cf_api.h"
21 #include "cf_memory.h"
22 #include "cf_result.h"
23
24 namespace OHOS {
CfCreateFuzzTest(const uint8_t * data,size_t size)25 bool CfCreateFuzzTest(const uint8_t* data, size_t size)
26 {
27 if (size < sizeof(CfObjectType) + sizeof(CfEncodingBlob)) {
28 return false;
29 }
30
31 uint8_t *tmpData = const_cast<uint8_t *>(data);
32 size_t usedSize = 0;
33 CfObjectType objType = *(reinterpret_cast<CfObjectType *>(tmpData));
34 usedSize += sizeof(CfObjectType);
35
36 CfEncodingBlob inStream = { 0 };
37 inStream.encodingFormat = *(reinterpret_cast<enum CfEncodingFormat *>(tmpData + usedSize));
38 usedSize += sizeof(enum CfEncodingFormat);
39 inStream.len = *(reinterpret_cast<size_t *>(tmpData + usedSize));
40 usedSize += sizeof(size_t);
41 if (inStream.len > size - usedSize) {
42 return false;
43 }
44 inStream.data = static_cast<uint8_t *>(CfMalloc(inStream.len));
45 if (inStream.data == nullptr) {
46 return false;
47 }
48 (void)memcpy_s(inStream.data, inStream.len, tmpData + usedSize, inStream.len);
49
50 CfObject *object = nullptr;
51 (void)CfCreate(objType, &inStream, &object);
52 CfFree(inStream.data);
53 if (object != nullptr) {
54 object->destroy(&object);
55 }
56
57 return true;
58 }
59 }
60
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64 /* Run your code on data */
65 OHOS::CfCreateFuzzTest(data, size);
66 return 0;
67 }
68