From 58232e3ffb319d27b41cfe040cfea8e43a3fb7e9 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Sun, 24 Jan 2021 18:45:44 +0000 Subject: fuzz: Avoid -fsanitize=integer warnings in fuzzing harnesses --- src/test/fuzz/crypto_chacha20_poly1305_aead.cpp | 10 ++++++++-- src/test/fuzz/pow.cpp | 5 ++++- src/test/fuzz/script.cpp | 4 ++-- src/test/fuzz/util.h | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/test/fuzz/crypto_chacha20_poly1305_aead.cpp b/src/test/fuzz/crypto_chacha20_poly1305_aead.cpp index 1f122082b2..0e1c44cded 100644 --- a/src/test/fuzz/crypto_chacha20_poly1305_aead.cpp +++ b/src/test/fuzz/crypto_chacha20_poly1305_aead.cpp @@ -45,18 +45,24 @@ FUZZ_TARGET(crypto_chacha20_poly1305_aead) assert(ok); }, [&] { + if (AdditionOverflow(seqnr_payload, static_cast(1))) { + return; + } seqnr_payload += 1; aad_pos += CHACHA20_POLY1305_AEAD_AAD_LEN; if (aad_pos + CHACHA20_POLY1305_AEAD_AAD_LEN > CHACHA20_ROUND_OUTPUT) { aad_pos = 0; + if (AdditionOverflow(seqnr_aad, static_cast(1))) { + return; + } seqnr_aad += 1; } }, [&] { - seqnr_payload = fuzzed_data_provider.ConsumeIntegral(); + seqnr_payload = fuzzed_data_provider.ConsumeIntegral(); }, [&] { - seqnr_aad = fuzzed_data_provider.ConsumeIntegral(); + seqnr_aad = fuzzed_data_provider.ConsumeIntegral(); }, [&] { is_encrypt = fuzzed_data_provider.ConsumeBool(); diff --git a/src/test/fuzz/pow.cpp b/src/test/fuzz/pow.cpp index 02beb6eb37..c4348495bf 100644 --- a/src/test/fuzz/pow.cpp +++ b/src/test/fuzz/pow.cpp @@ -43,7 +43,10 @@ FUZZ_TARGET_INIT(pow, initialize_pow) current_block.nHeight = current_height; } if (fuzzed_data_provider.ConsumeBool()) { - current_block.nTime = fixed_time + current_height * consensus_params.nPowTargetSpacing; + const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing; + if (!AdditionOverflow(fixed_time, seconds)) { + current_block.nTime = fixed_time + seconds; + } } if (fuzzed_data_provider.ConsumeBool()) { current_block.nBits = fixed_bits; diff --git a/src/test/fuzz/script.cpp b/src/test/fuzz/script.cpp index d883426c81..7fadf36f98 100644 --- a/src/test/fuzz/script.cpp +++ b/src/test/fuzz/script.cpp @@ -154,13 +154,13 @@ FUZZ_TARGET_INIT(script, initialize_script) { WitnessUnknown witness_unknown_1{}; - witness_unknown_1.version = fuzzed_data_provider.ConsumeIntegral(); + witness_unknown_1.version = fuzzed_data_provider.ConsumeIntegral(); const std::vector witness_unknown_program_1 = fuzzed_data_provider.ConsumeBytes(40); witness_unknown_1.length = witness_unknown_program_1.size(); std::copy(witness_unknown_program_1.begin(), witness_unknown_program_1.end(), witness_unknown_1.program); WitnessUnknown witness_unknown_2{}; - witness_unknown_2.version = fuzzed_data_provider.ConsumeIntegral(); + witness_unknown_2.version = fuzzed_data_provider.ConsumeIntegral(); const std::vector witness_unknown_program_2 = fuzzed_data_provider.ConsumeBytes(40); witness_unknown_2.length = witness_unknown_program_2.size(); std::copy(witness_unknown_program_2.begin(), witness_unknown_program_2.end(), witness_unknown_2.program); diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index 7796f77cc6..5b8eacf051 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -196,7 +196,7 @@ template }, [&] { WitnessUnknown witness_unknown{}; - witness_unknown.version = fuzzed_data_provider.ConsumeIntegral(); + witness_unknown.version = fuzzed_data_provider.ConsumeIntegral(); const std::vector witness_unknown_program_1 = fuzzed_data_provider.ConsumeBytes(40); witness_unknown.length = witness_unknown_program_1.size(); std::copy(witness_unknown_program_1.begin(), witness_unknown_program_1.end(), witness_unknown.program); -- cgit v1.2.3