1 /*
2 * Copyright (c) 2025 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 "addparamentry_fuzzer.h"
17 #include <string>
18 #include <memory>
19 #include "param_trie.h"
20 #include "param_manager.h"
21 #include "securec.h"
22 constexpr int DATA_SIZE = 2;
23 constexpr int INDEX_MAX = 1000;
24 constexpr int TYPE_MAX = 5;
25
26 namespace OHOS {
FuzzAddParamEntry(const uint8_t * data,size_t size)27 bool FuzzAddParamEntry(const uint8_t* data, size_t size)
28 {
29 if (size < sizeof(uint32_t)) {
30 return false;
31 }
32
33 size_t splitPos = size / DATA_SIZE;
34 if (splitPos == 0) {
35 splitPos = 1;
36 }
37
38 std::unique_ptr<char[]> name(new char[splitPos + 1]);
39 if (memcpy_s(name.get(), splitPos + 1, data, splitPos) != 0) {
40 return false;
41 }
42 name[splitPos] = '\0';
43
44 size_t valueSize = size - splitPos;
45 std::unique_ptr<char[]> value(new char[valueSize + 1]);
46 if (memcpy_s(value.get(), valueSize + 1, data + splitPos, valueSize) != 0) {
47 return false;
48 }
49 value[valueSize] = '\0';
50
51 uint32_t index = 0;
52 if (memcpy_s(&index, sizeof(uint32_t), data, sizeof(uint32_t)) != 0) {
53 index = 0;
54 }
55 index %= INDEX_MAX;
56
57 uint8_t type = 0;
58 if (size > 0) {
59 type = data[0] % TYPE_MAX;
60 }
61
62 return (AddParamEntry(index, type, name.get(), value.get()) == 0);
63 }
64 }
65
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
67 {
68 OHOS::FuzzAddParamEntry(data, size);
69 return 0;
70 }