Coverage Report

Created: 2026-01-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/uriparser/fuzz/ParseFuzzer.cpp
Line
Count
Source
1
// Copyright 2020 Google LLC
2
// Copyright 2025 Mikhail Khachaiants <mkhachaiants@gmail.com>
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//      http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
16
#include "uriparser/Uri.h"
17
#include "uriparser/UriIp4.h"
18
#include "FuzzingUtils.h"
19
#include <cstddef>
20
#include <cstring>
21
#include <vector>
22
23
class UriHolder {
24
public:
25
20.2k
    UriHolder() {
26
20.2k
        memset((void *)&uri_, 0, sizeof(uri_));
27
20.2k
    }
28
29
20.2k
    ~UriHolder() {
30
20.2k
        URI_FUNC(FreeUriMembers)(&uri_);
31
20.2k
    }
32
33
39.2k
    URI_TYPE(Uri) * get() {
34
39.2k
        return &uri_;
35
39.2k
    }
36
37
private:
38
    URI_TYPE(Uri) uri_;
39
};
40
41
20.8k
void Escapes(const UriString & uri) {
42
20.8k
    const URI_CHAR * first = uri.c_str();
43
    // Up to 6 bytes per character with normalizeBreaks enabled (\n -> %0D%0A)
44
20.8k
    std::vector<URI_CHAR> buf1(uri.size() * 6 + 1);
45
    // and up to 3 bytes per character otherwise
46
20.8k
    std::vector<URI_CHAR> buf2(uri.size() * 3 + 1);
47
48
20.8k
    URI_FUNC(Escape)(first, &buf1[0], URI_TRUE, URI_TRUE);
49
20.8k
    URI_FUNC(Escape)(first, &buf1[0], URI_FALSE, URI_TRUE);
50
20.8k
    if (buf1.data()) {
51
20.8k
        URI_FUNC(UnescapeInPlace)(&buf1[0]);
52
20.8k
    }
53
54
20.8k
    URI_FUNC(Escape)(first, &buf2[0], URI_TRUE, URI_FALSE);
55
20.8k
    URI_FUNC(Escape)(first, &buf2[0], URI_FALSE, URI_FALSE);
56
20.8k
    if (buf2.data()) {
57
20.8k
        URI_FUNC(UnescapeInPlace)(&buf2[0]);
58
20.8k
    }
59
20.8k
}
60
61
20.8k
void FileNames(const UriString & uri) {
62
20.8k
    const size_t size = 8 + 3 * uri.size() + 1;
63
20.8k
    std::vector<URI_CHAR> buf(size);
64
65
20.8k
    URI_FUNC(UnixFilenameToUriString)(uri.c_str(), &buf[0]);
66
20.8k
    URI_FUNC(WindowsFilenameToUriString)(uri.c_str(), &buf[0]);
67
20.8k
    URI_FUNC(UriStringToUnixFilename)(uri.c_str(), &buf[0]);
68
20.8k
    URI_FUNC(UriStringToWindowsFilename)(uri.c_str(), &buf[0]);
69
20.8k
}
70
71
20.8k
void Ipv4(const UriString & s) {
72
20.8k
    const URI_CHAR * cstr = s.c_str();
73
20.8k
    unsigned char result[4] = {};
74
20.8k
    URI_FUNC(ParseIpFourAddress)(result, cstr, &cstr[s.size()]);
75
20.8k
}
76
77
10.4k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) {
78
10.4k
    FuzzedDataProvider stream(data, size);
79
10.4k
    bool domainRelative = stream.ConsumeBool();
80
81
10.4k
    const UriString uri1 = consumeRandomLengthString(stream);
82
10.4k
    const UriString uri2 = consumeRemainingBytesAsString(stream);
83
84
10.4k
    Escapes(uri1);
85
10.4k
    Escapes(uri2);
86
87
10.4k
    FileNames(uri1);
88
10.4k
    FileNames(uri2);
89
90
10.4k
    Ipv4(uri1);
91
10.4k
    Ipv4(uri2);
92
93
10.4k
    UriHolder uriHolder1;
94
10.4k
    URI_TYPE(ParserState) state1;
95
10.4k
    state1.uri = uriHolder1.get();
96
10.4k
    if (URI_FUNC(ParseUri)(&state1, uri1.c_str()) != URI_SUCCESS) {
97
635
        return 0;
98
635
    }
99
100
9.79k
    URI_CHAR buf[1024 * 8] = {0};
101
9.79k
    int written = 0;
102
9.79k
    URI_FUNC(ToString)(buf, state1.uri, sizeof(buf) / sizeof(buf[0]), &written);
103
104
9.79k
    UriHolder uriHolder2;
105
9.79k
    if (URI_FUNC(ParseSingleUri)(uriHolder2.get(), uri2.c_str(), nullptr)
106
9.79k
        != URI_SUCCESS) {
107
3.46k
        return 0;
108
3.46k
    }
109
110
6.33k
    URI_FUNC(EqualsUri)(state1.uri, uriHolder2.get());
111
112
6.33k
    unsigned int mask = 0;
113
6.33k
    URI_FUNC(NormalizeSyntaxMaskRequiredEx)(state1.uri, &mask);
114
6.33k
    URI_FUNC(NormalizeSyntax)(state1.uri);
115
116
6.33k
    URI_TYPE(Uri) absUri;
117
6.33k
    URI_FUNC(AddBaseUri)(&absUri, state1.uri, uriHolder2.get());
118
6.33k
    URI_FUNC(FreeUriMembers)(&absUri);
119
120
6.33k
    URI_TYPE(Uri) relUri;
121
6.33k
    URI_FUNC(RemoveBaseUri)(&relUri, state1.uri, uriHolder2.get(), domainRelative);
122
6.33k
    URI_FUNC(FreeUriMembers)(&relUri);
123
124
6.33k
    return 0;
125
9.79k
}