1 /*
2  * Copyright (c) 2022 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 "streamcommondata_fuzzer.h"
17 #include "stream_common_data.h"
18 #include "common_inner.h"
19 #include "stream_common.h"
20 #include <securec.h>
21 #include <memory>
22 #include <cstddef>
23 #include <cstdint>
24 
25 using namespace std;
26 
27 namespace OHOS {
InitStreamDataTest(const uint8_t * data,size_t size)28     void InitStreamDataTest(const uint8_t* data, size_t size)
29     {
30         if ((data == nullptr) || (size < Communication::SoftBus::MAX_STREAM_LEN)) {
31             return;
32         }
33         char *buf = new char[Communication::SoftBus::MAX_STREAM_LEN + 1];
34         if (buf == nullptr) {
35             return;
36         }
37         std::unique_ptr<char[]> inputbuf (buf);
38         if (memcpy_s(buf, Communication::SoftBus::MAX_STREAM_LEN + 1,
39             data, Communication::SoftBus::MAX_STREAM_LEN) != EOK) {
40             delete []buf;
41             buf = nullptr;
42             return;
43         }
44         char *ext = new char[Communication::SoftBus::MAX_STREAM_LEN + 1];
45         if (ext == nullptr) {
46             delete []buf;
47             buf = nullptr;
48             return;
49         }
50         std::unique_ptr<char[]> inputext (ext);
51         if (memcpy_s(ext, Communication::SoftBus::MAX_STREAM_LEN + 1,
52             data, Communication::SoftBus::MAX_STREAM_LEN) != EOK) {
53             delete []ext;
54             delete []buf;
55             ext = nullptr;
56             buf = nullptr;
57             return;
58         }
59 
60         Communication::SoftBus::StreamCommonData streamcommondata;
61         streamcommondata.InitStreamData(std::move(inputbuf), Communication::SoftBus::MAX_STREAM_LEN + 1,
62             std::move(inputext), Communication::SoftBus::MAX_STREAM_LEN + 1);
63     }
64 } // namespace OHOS
65 
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69     /* Run your code on data */
70     OHOS::InitStreamDataTest(data, size);
71     return 0;
72 }
73