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 (memcpy_s(buf, Communication::SoftBus::MAX_STREAM_LEN + 1,
35 data, Communication::SoftBus::MAX_STREAM_LEN) != EOK) {
36 delete []buf;
37 buf = nullptr;
38 return;
39 }
40 char *ext = new char[Communication::SoftBus::MAX_STREAM_LEN + 1];
41 if (memcpy_s(ext, Communication::SoftBus::MAX_STREAM_LEN + 1,
42 data, Communication::SoftBus::MAX_STREAM_LEN) != EOK) {
43 delete []ext;
44 ext = nullptr;
45 return;
46 }
47 std::unique_ptr<char[]> inputbuf (buf);
48 std::unique_ptr<char[]> inputext (ext);
49
50 Communication::SoftBus::StreamCommonData streamcommondata;
51 streamcommondata.InitStreamData(std::move(inputbuf), Communication::SoftBus::MAX_STREAM_LEN + 1,
52 std::move(inputext), Communication::SoftBus::MAX_STREAM_LEN + 1);
53 }
54 } // namespace OHOS
55
56 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59 /* Run your code on data */
60 OHOS::InitStreamDataTest(data, size);
61 return 0;
62 }
63
64