Coverage Report

Created: 2025-07-04 06:41

/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.46k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
12
13
1.46k
    if (!size) {
14
0
        return 0;
15
0
    }
16
17
1.46k
    char *mutableMemory = (char *) malloc(size);
18
1.46k
    memcpy(mutableMemory, data, size);
19
20
    /* First byte determines how long contentType is */
21
1.46k
    unsigned char contentTypeLength = data[0];
22
1.46k
    size--;
23
24
1.46k
    std::string_view contentType((char *) mutableMemory + 1, std::min<size_t>(contentTypeLength, size));
25
1.46k
    size -= contentType.length();
26
27
1.46k
    std::string_view body((char *) mutableMemory + 1 + contentType.length(), size);
28
29
1.46k
    uWS::MultipartParser mp(contentType);
30
1.46k
    if (mp.isValid()) {
31
1.29k
        mp.setBody(body);
32
33
1.29k
        std::pair<std::string_view, std::string_view> headers[10];
34
35
4.60k
        while (true) {
36
4.60k
            std::optional<std::string_view> optionalPart = mp.getNextPart(headers);
37
4.60k
            if (!optionalPart.has_value()) {
38
1.29k
                break;
39
1.29k
            }
40
41
3.30k
            std::string_view part = optionalPart.value();
42
43
9.15k
            for (int i = 0; headers[i].first.length(); i++) {
44
                /* We care about content-type and content-disposition */
45
5.84k
                if (headers[i].first == "content-disposition") {
46
                    /* Parse the parameters */
47
4.51k
                    uWS::ParameterParser pp(headers[i].second);
48
127k
                    while (true) {
49
127k
                        auto [key, value] = pp.getKeyValue();
50
127k
                        if (!key.length()) {
51
4.51k
                            break;
52
4.51k
                        }
53
127k
                    }
54
4.51k
                }
55
5.84k
            }
56
3.30k
        }
57
1.29k
    }
58
59
1.46k
    free(mutableMemory);
60
1.46k
    return 0;
61
1.46k
}
62