• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "valueobject_fuzzer.h"
17 
18 #include <fuzzer/FuzzedDataProvider.h>
19 
20 #include "grd_api_manager.h"
21 #include "oh_value_object.h"
22 #include "rdb_errno.h"
23 #include "relational_store.h"
24 #include "relational_store_error_code.h"
25 #include "relational_store_impl.h"
26 
27 
28 using namespace OHOS::NativeRdb;
29 using namespace OHOS::RdbNdk;
30 
ValueObjectFuzzTest(FuzzedDataProvider & provider)31 void ValueObjectFuzzTest(FuzzedDataProvider &provider)
32 {
33     OH_VObject *valueObject = OH_Rdb_CreateValueObject();
34     if (valueObject == nullptr) {
35         return;
36     }
37 
38     // Test putText
39     {
40         std::string value = provider.ConsumeRandomLengthString();
41         valueObject->putText(valueObject, value.c_str());
42     }
43 
44     // Test putInt64
45     {
46         const int minArraySize = 1;
47         const int maxArraySize = 50;
48         size_t arraySize = provider.ConsumeIntegralInRange<size_t>(minArraySize, maxArraySize);
49         std::vector<int64_t> array(arraySize);
50         for (size_t i = 0; i < arraySize; i++) {
51             array[i] = provider.ConsumeIntegral<int64_t>();
52         }
53         valueObject->putInt64(valueObject, array.data(), array.size());
54     }
55 
56     // Test putDouble
57     {
58         const int minArraySize = 1;
59         const int maxArraySize = 50;
60         size_t arraySize = provider.ConsumeIntegralInRange<size_t>(minArraySize, maxArraySize);
61         std::vector<double> array(arraySize);
62         for (size_t i = 0; i < arraySize; i++) {
63             array[i] = static_cast<double>(provider.ConsumeFloatingPoint<float>());
64         }
65         valueObject->putDouble(valueObject, array.data(), array.size());
66     }
67 
68     // Test putTexts
69     {
70         const int minArraySize = 1;
71         const int maxArraySize = 50;
72         size_t arraySize = provider.ConsumeIntegralInRange<size_t>(minArraySize, maxArraySize);
73 
74         // Use std::vector to manage memory
75         std::vector<std::unique_ptr<char[]>> stringStorage(arraySize);
76         const char **array = new const char *[arraySize];
77 
78         for (size_t i = 0; i < arraySize; i++) {
79             std::string value = provider.ConsumeRandomLengthString();
80 
81             // Allocate memory for each string
82             stringStorage[i] = std::make_unique<char[]>(value.size() + 1);
83             std::copy(value.begin(), value.end(), stringStorage[i].get());
84             stringStorage[i][value.size()] = '\0'; // Ensure the string is null-terminated
85 
86             // Assign the pointer to array
87             array[i] = stringStorage[i].get();
88         }
89 
90         // Use array
91         valueObject->putTexts(valueObject, array, arraySize);
92 
93         // Free the array pointer
94         delete[] array;
95     }
96 
97     // Destroy valueObject
98     valueObject->destroy(valueObject);
99 }
100 
101 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
103 {
104     // Run your code on data
105     FuzzedDataProvider provider(data, size);
106     ValueObjectFuzzTest(provider);
107     return 0;
108 }
109