summaryrefslogtreecommitdiff
path: root/tomlplusplus/fuzzing/toml_fuzzer.cpp
blob: 2b657f0fec411bb4ae5d58503ac00e089c2a19c6 (plain)
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
#include <cstdint>
#include <fuzzer/FuzzedDataProvider.h>

#include <toml++/toml.hpp>

enum class SerializationTest
{
    NONE = 0,
    JSON,
    YAML,
    TOML,
    kMaxValue = TOML
};

extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, const std::size_t size)
{
    FuzzedDataProvider fdp{data, size};
    try
    {
        const toml::table tbl = toml::parse(fdp.ConsumeRandomLengthString());

        switch (fdp.ConsumeEnum<SerializationTest>())
        {
            case SerializationTest::JSON:
                static_cast<void>(toml::json_formatter{tbl});
                break;
            case SerializationTest::YAML:
                static_cast<void>(toml::yaml_formatter{tbl});
                break;
            case SerializationTest::TOML:
                static_cast<void>(toml::toml_formatter{tbl});
            default:
                break;
        }
    }
    catch (const toml::parse_error&)
    {
        return -1;
    }
    return 0;
}