Coverage Report

Created: 2023-06-06 06:17

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