Coverage Report

Created: 2025-06-13 06:09

/src/uWebSockets/fuzzing/MultipartParser.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This is a fuzz test of the multipart parser */
2
3
#define WIN32_EXPORT
4
5
#include <cstdio>
6
#include <string>
7
#include <cstdlib>
8
9
#include "../src/Multipart.h"
10
11
1.42k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
12
13
1.42k
    if (!size) {
14
0
        return 0;
15
0
    }
16
17
1.42k
    char *mutableMemory = (char *) malloc(size);
18
1.42k
    memcpy(mutableMemory, data, size);
19
20
    /* First byte determines how long contentType is */
21
1.42k
    unsigned char contentTypeLength = data[0];
22
1.42k
    size--;
23
24
1.42k
    std::string_view contentType((char *) mutableMemory + 1, std::min<size_t>(contentTypeLength, size));
25
1.42k
    size -= contentType.length();
26
27
1.42k
    std::string_view body((char *) mutableMemory + 1 + contentType.length(), size);
28
29
1.42k
    uWS::MultipartParser mp(contentType);
30
1.42k
    if (mp.isValid()) {
31
1.27k
        mp.setBody(body);
32
33
1.27k
        std::pair<std::string_view, std::string_view> headers[10];
34
35
4.20k
        while (true) {
36
4.20k
            std::optional<std::string_view> optionalPart = mp.getNextPart(headers);
37
4.20k
            if (!optionalPart.has_value()) {
38
1.27k
                break;
39
1.27k
            }
40
41
2.93k
            std::string_view part = optionalPart.value();
42
43
9.91k
            for (int i = 0; headers[i].first.length(); i++) {
44
                /* We care about content-type and content-disposition */
45
6.98k
                if (headers[i].first == "content-disposition") {
46
                    /* Parse the parameters */
47
5.16k
                    uWS::ParameterParser pp(headers[i].second);
48
170k
                    while (true) {
49
170k
                        auto [key, value] = pp.getKeyValue();
50
170k
                        if (!key.length()) {
51
5.16k
                            break;
52
5.16k
                        }
53
170k
                    }
54
5.16k
                }
55
6.98k
            }
56
2.93k
        }
57
1.27k
    }
58
59
1.42k
    free(mutableMemory);
60
1.42k
    return 0;
61
1.42k
}
62