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