• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
InitStreamDataTest(const uint8_t * data,size_t size)26     void InitStreamDataTest(const uint8_t* data, size_t size)
27     {
28         if ((data == nullptr) || (size < Communication::SoftBus::MAX_STREAM_LEN)) {
29             return;
30         }
31         char *buf = new char[Communication::SoftBus::MAX_STREAM_LEN + 1];
32         if (memcpy_s(buf, Communication::SoftBus::MAX_STREAM_LEN + 1,
33             data, Communication::SoftBus::MAX_STREAM_LEN) != EOK) {
34             delete []buf;
35             buf = nullptr;
36             return;
37         }
38         char *ext = new char[Communication::SoftBus::MAX_STREAM_LEN + 1];
39         if (memcpy_s(ext, Communication::SoftBus::MAX_STREAM_LEN + 1,
40             data, Communication::SoftBus::MAX_STREAM_LEN) != EOK) {
41             delete []ext;
42             ext = nullptr;
43             return;
44         }
45         std::unique_ptr<char[]> inputbuf (buf);
46         std::unique_ptr<char[]> inputext (ext);
47 
48         Communication::SoftBus::RawStreamData rawstreamdata;
49         rawstreamdata.InitStreamData(std::move(inputbuf), Communication::SoftBus::MAX_STREAM_LEN + 1,
50             std::move(inputext), Communication::SoftBus::MAX_STREAM_LEN + 1);
51     }
52 } // namespace OHOS
53 
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
56 {
57     /* Run your code on data */
58     OHOS::InitStreamDataTest(data, size);
59     return 0;
60 }
61