Skip to content

Commit 812f931

Browse files
committed
fix: Make sure there's enough space for CONSUME1 in fuzzers.
1 parent 50f1b30 commit 812f931

7 files changed

Lines changed: 26 additions & 26 deletions

File tree

testing/fuzzing/bootstrap_harness.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void TestBootstrap(Fuzz_Data &input)
126126
}
127127
});
128128

129-
CONSUME1_OR_RETURN(const uint8_t proxy_type, input);
129+
CONSUME1_OR_RETURN(const uint8_t, proxy_type, input);
130130
if (proxy_type == 0) {
131131
tox_options_set_proxy_type(opts.get(), TOX_PROXY_TYPE_NONE);
132132
} else if (proxy_type == 1) {
@@ -139,7 +139,7 @@ void TestBootstrap(Fuzz_Data &input)
139139
tox_options_set_proxy_port(opts.get(), 8080);
140140
}
141141

142-
CONSUME1_OR_RETURN(const uint8_t tcp_relay_enabled, input);
142+
CONSUME1_OR_RETURN(const uint8_t, tcp_relay_enabled, input);
143143
if (tcp_relay_enabled >= (UINT8_MAX / 2)) {
144144
tox_options_set_tcp_port(opts.get(), 33445);
145145
}

testing/fuzzing/fuzz_support.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int recv_common(Fuzz_Data &input, uint8_t *buf, size_t buf_len)
7777
template <typename F>
7878
static void *alloc_common(Fuzz_Data &data, F func)
7979
{
80-
CONSUME1_OR_RETURN_VAL(const uint8_t want_alloc, data, func());
80+
CONSUME1_OR_RETURN_VAL(const uint8_t, want_alloc, data, func());
8181
if (!want_alloc) {
8282
return nullptr;
8383
}

testing/fuzzing/fuzz_support.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ struct Fuzz_Data {
6060
*
6161
* @example
6262
* @code
63-
* CONSUME1_OR_RETURN(const uint8_t one_byte, input);
63+
* CONSUME1_OR_RETURN(const uint8_t, one_byte, input);
6464
* @endcode
6565
*/
66-
#define CONSUME1_OR_RETURN(DECL, INPUT) \
67-
if (INPUT.size < 1) { \
68-
return; \
69-
} \
70-
DECL = INPUT.consume1()
66+
#define CONSUME1_OR_RETURN(TYPE, NAME, INPUT) \
67+
if (INPUT.size < sizeof(TYPE)) { \
68+
return; \
69+
} \
70+
TYPE NAME = INPUT.consume1()
7171

7272
/** @brief Consumes 1 byte of the fuzzer input or returns a value if no data
7373
* available.
@@ -80,11 +80,11 @@ struct Fuzz_Data {
8080
* CONSUME1_OR_RETURN_VAL(const uint8_t one_byte, input, nullptr);
8181
* @endcode
8282
*/
83-
#define CONSUME1_OR_RETURN_VAL(DECL, INPUT, VAL) \
84-
if (INPUT.size < 1) { \
85-
return VAL; \
86-
} \
87-
DECL = INPUT.consume1()
83+
#define CONSUME1_OR_RETURN_VAL(TYPE, NAME, INPUT, VAL) \
84+
if (INPUT.size < sizeof(TYPE)) { \
85+
return VAL; \
86+
} \
87+
TYPE NAME = INPUT.consume1()
8888

8989
/** @brief Consumes SIZE bytes of the fuzzer input or returns if not enough data available.
9090
*
@@ -129,7 +129,7 @@ void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&...args)
129129
{
130130
Fuzz_Data input{data, size};
131131

132-
CONSUME1_OR_RETURN(uint8_t selector, input);
132+
CONSUME1_OR_RETURN(const uint8_t, selector, input);
133133
return fuzz_select_target(selector, input, std::forward<Args>(args)...);
134134
}
135135

toxcore/DHT_fuzz_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void TestHandleRequest(Fuzz_Data &input)
2323

2424
void TestUnpackNodes(Fuzz_Data &input)
2525
{
26-
CONSUME1_OR_RETURN(const bool tcp_enabled, input);
26+
CONSUME1_OR_RETURN(const bool, tcp_enabled, input);
2727

2828
const uint16_t node_count = 5;
2929
Node_format nodes[node_count];

toxcore/forwarding_fuzz_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ std::optional<std::tuple<IP_Port, IP_Port, const uint8_t *, size_t>> prepare(Fuz
3333

3434
void TestSendForwardRequest(Fuzz_Data &input)
3535
{
36-
CONSUME1_OR_RETURN(const uint16_t chain_length, input);
36+
CONSUME1_OR_RETURN(const uint16_t, chain_length, input);
3737
const uint16_t chain_keys_size = chain_length * CRYPTO_PUBLIC_KEY_SIZE;
3838
CONSUME_OR_RETURN(const uint8_t *chain_keys, input, chain_keys_size);
3939

@@ -60,7 +60,7 @@ void TestSendForwardRequest(Fuzz_Data &input)
6060

6161
void TestForwardReply(Fuzz_Data &input)
6262
{
63-
CONSUME1_OR_RETURN(const uint16_t sendback_length, input);
63+
CONSUME1_OR_RETURN(const uint16_t, sendback_length, input);
6464
CONSUME_OR_RETURN(const uint8_t *sendback, input, sendback_length);
6565

6666
auto prep = prepare(input);

toxcore/group_announce_fuzz_test.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ namespace {
1111

1212
void TestUnpackAnnouncesList(Fuzz_Data &input)
1313
{
14-
CONSUME1_OR_RETURN(const uint8_t max_count, input);
14+
CONSUME1_OR_RETURN(const uint8_t, max_count, input);
1515
// Always allocate at least something to avoid passing nullptr to functions below.
1616
std::vector<GC_Announce> announces(max_count + 1);
1717

1818
// TODO(iphydf): How do we know the packed size?
19-
CONSUME1_OR_RETURN(const uint16_t packed_size, input);
19+
CONSUME1_OR_RETURN(const uint16_t, packed_size, input);
2020

2121
Logger *logger = logger_new();
2222
if (gca_unpack_announces_list(logger, input.data, input.size, announces.data(), max_count)
@@ -35,7 +35,7 @@ void TestUnpackPublicAnnounce(Fuzz_Data &input)
3535
GC_Public_Announce public_announce;
3636

3737
// TODO(iphydf): How do we know the packed size?
38-
CONSUME1_OR_RETURN(const uint16_t packed_size, input);
38+
CONSUME1_OR_RETURN(const uint16_t, packed_size, input);
3939

4040
Logger *logger = logger_new();
4141
if (gca_unpack_public_announce(logger, input.data, input.size, &public_announce) != -1) {
@@ -61,11 +61,11 @@ void TestDoGca(Fuzz_Data &input)
6161
assert(gca != nullptr);
6262

6363
while (input.size > 0) {
64-
CONSUME1_OR_RETURN(const uint8_t choice, input);
64+
CONSUME1_OR_RETURN(const uint8_t, choice, input);
6565
switch (choice) {
6666
case 0: {
6767
// Add an announce.
68-
CONSUME1_OR_RETURN(const uint16_t length, input);
68+
CONSUME1_OR_RETURN(const uint16_t, length, input);
6969
CONSUME_OR_RETURN(const uint8_t *data, input, length);
7070
GC_Public_Announce public_announce;
7171
if (gca_unpack_public_announce(logger.get(), data, length, &public_announce) != -1) {
@@ -75,15 +75,15 @@ void TestDoGca(Fuzz_Data &input)
7575
}
7676
case 1: {
7777
// Advance the time by a number of tox_iteration_intervals.
78-
CONSUME1_OR_RETURN(const uint8_t iterations, input);
78+
CONSUME1_OR_RETURN(const uint8_t, iterations, input);
7979
clock += iterations * 20;
8080
// Do an iteration.
8181
do_gca(mono_time.get(), gca.get());
8282
break;
8383
}
8484
case 2: {
8585
// Get announces.
86-
CONSUME1_OR_RETURN(const uint8_t max_nodes, input);
86+
CONSUME1_OR_RETURN(const uint8_t, max_nodes, input);
8787
std::vector<GC_Announce> gc_announces(max_nodes);
8888
CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE);
8989
CONSUME_OR_RETURN(const uint8_t *except_public_key, input, ENC_PUBLIC_KEY_SIZE);

toxcore/group_moderation_fuzz_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace {
66

77
void TestModListUnpack(Fuzz_Data &input)
88
{
9-
CONSUME1_OR_RETURN(const uint16_t num_mods, input);
9+
CONSUME1_OR_RETURN(const uint16_t, num_mods, input);
1010
Moderation mods{system_memory()};
1111
mod_list_unpack(&mods, input.data, input.size, num_mods);
1212
mod_list_cleanup(&mods);

0 commit comments

Comments
 (0)