File: fuzz_text_field.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (89 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (4)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/mysql/column_type.hpp>
#include <boost/mysql/field_view.hpp>
#include <boost/mysql/metadata.hpp>
#include <boost/mysql/string_view.hpp>

#include <boost/mysql/detail/coldef_view.hpp>
#include <boost/mysql/detail/flags.hpp>

#include <boost/mysql/impl/internal/protocol/deserialization.hpp>

#include <boost/endian/conversion.hpp>

#include <cstddef>
#include <cstdint>

using namespace boost::mysql::detail;
using boost::mysql::column_type;
using boost::mysql::field_view;
using boost::mysql::metadata;
using boost::mysql::string_view;

struct input
{
    metadata meta;
    string_view msg;
};

static string_view sv_from_range(const uint8_t* data, size_t size)
{
    return string_view(reinterpret_cast<const char*>(data), size);
}

static input parse_input(const uint8_t* data, size_t size)
{
    // Samples have a 2-byte header specifying metadata
    // meta[0][low 7 bits]: column_type
    // meta[0][high bit]: is unsigned flag
    // meta[1]: decimals
    if (size < 2)
        return input{metadata(), sv_from_range(data, size)};

    coldef_view coldef{};

    // Type: low 7 bits
    coldef.type = static_cast<column_type>(data[0] & (0xff >> 1));

    // Flags: we seed it with some value, and change the flag we're interested in
    coldef.flags = boost::endian::load_little_u16(size >= 4 ? data + 2 : data);
    if (data[0] & (1 << 7))
    {
        coldef.flags |= column_flags::unsigned_;
    }
    else
    {
        coldef.flags &= ~column_flags::unsigned_;
    }

    // Decimals
    coldef.decimals = data[1];

    // Done
    return {
        access::construct<metadata>(coldef, false),
        sv_from_range(data + 2, size - 2),
    };
}

static bool parse_field(const input& input) noexcept
{
    field_view fv;
    auto ec = deserialize_text_field(input.msg, input.meta, fv);
    if (ec != deserialize_errc::ok)
        return false;
    return !fv.is_null();
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
    // Note: this code should never throw exceptions, for any kind of input
    parse_field(parse_input(data, size));
    return 0;
}