aboutsummaryrefslogtreecommitdiffstats
path: root/fuzz/standalone_fuzz_target_runner.cc
blob: 59fb0438ab4a4726b48a547df3de7baaf77efd27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <qpdf/QUtil.hh>
#include <qpdf/PointerHolder.hh>
#include <qpdf/QIntC.hh>
#include <iostream>
#include <string>

extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size);

static void read_file_into_memory(
    char const* filename,
    PointerHolder<unsigned char>& file_buf, size_t& size)
{
    FILE* f = QUtil::safe_fopen(filename, "rb");
    fseek(f, 0, SEEK_END);
    size = QIntC::to_size(QUtil::tell(f));
    fseek(f, 0, SEEK_SET);
    file_buf = PointerHolder<unsigned char>(true, new unsigned char[size]);
    unsigned char* buf_p = file_buf.getPointer();
    size_t bytes_read = 0;
    size_t len = 0;
    while ((len = fread(buf_p + bytes_read, 1, size - bytes_read, f)) > 0)
    {
        bytes_read += len;
    }
    if (bytes_read != size)
    {
        throw std::runtime_error(
            std::string("failure reading file ") + filename +
            " into memory: read " +
            QUtil::uint_to_string(bytes_read) + "; wanted " +
            QUtil::uint_to_string(size));
    }
    fclose(f);
}

int main(int argc, char **argv)
{
    for (int i = 1; i < argc; i++)
    {
        PointerHolder<unsigned char> file_buf;
        size_t size = 0;
        read_file_into_memory(argv[i], file_buf, size);
        LLVMFuzzerTestOneInput(file_buf.getPointer(), size);
        std::cout << argv[i] << " successful" << std::endl;
    }
    return 0;
}