Coverage Report

Created: 2025-11-15 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/uriparser/fuzz/FuzzingUtils.h
Line
Count
Source
1
// Copyright 2025 Mikhail Khachaiants <mkhachaiants@gmail.com>
2
//
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
#ifndef URI_FUZZING_UTILS_H
16
#  define URI_FUZZING_UTILS_H 1
17
18
#  include "uriparser/Uri.h"
19
#  include <fuzzer/FuzzedDataProvider.h>
20
#  include <string>
21
22
using UriString = std::basic_string<URI_CHAR>;
23
24
7.70k
inline UriString tryConsumeBytesAsString(FuzzedDataProvider & fdp, size_t chars) {
25
7.70k
    UriString str(chars, 0);
26
7.70k
    const size_t bytes = fdp.ConsumeData(str.data(), chars * sizeof(URI_CHAR));
27
    // FuzzedDataProvider may provide less data than requested if the input is
28
    // insufficiently long. We need to adjust the string length accordingly.
29
7.70k
    str.resize(bytes / sizeof(URI_CHAR));
30
7.70k
    return str;
31
7.70k
}
32
33
0
inline UriString consumeRandomLengthString(FuzzedDataProvider & fdp) {
34
0
    const size_t max_chars = fdp.remaining_bytes() / sizeof(URI_CHAR);
35
0
    const size_t chars = fdp.ConsumeIntegralInRange<size_t>(0, max_chars);
36
0
    return tryConsumeBytesAsString(fdp, chars);
37
0
}
38
39
7.70k
inline UriString consumeRemainingBytesAsString(FuzzedDataProvider & fdp) {
40
7.70k
    const size_t chars = fdp.remaining_bytes() / sizeof(URI_CHAR);
41
7.70k
    return tryConsumeBytesAsString(fdp, chars);
42
7.70k
}
43
44
#endif /* URI_FUZZING_UTILS_H */