Changeset View
Changeset View
Standalone View
Standalone View
src/test/fuzz/strprintf.cpp
// Copyright (c) 2020 The Bitcoin Core developers | // Copyright (c) 2020 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/translation.h> | |||||
#include <test/fuzz/FuzzedDataProvider.h> | #include <test/fuzz/FuzzedDataProvider.h> | ||||
#include <test/fuzz/fuzz.h> | #include <test/fuzz/fuzz.h> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
void test_one_input(const std::vector<uint8_t> &buffer) { | void test_one_input(const std::vector<uint8_t> &buffer) { | ||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||||
const std::string format_string = | const std::string format_string = | ||||
fuzzed_data_provider.ConsumeRandomLengthString(64); | fuzzed_data_provider.ConsumeRandomLengthString(64); | ||||
const bilingual_str bilingual_string{format_string, format_string}; | |||||
const int digits_in_format_specifier = | const int digits_in_format_specifier = | ||||
std::count_if(format_string.begin(), format_string.end(), IsDigit); | std::count_if(format_string.begin(), format_string.end(), IsDigit); | ||||
// Avoid triggering the following crash bug: | // Avoid triggering the following crash bug: | ||||
// * strprintf("%987654321000000:", 1); | // * strprintf("%987654321000000:", 1); | ||||
// | // | ||||
// Avoid triggering the following OOM bug: | // Avoid triggering the following OOM bug: | ||||
Show All 20 Lines | void test_one_input(const std::vector<uint8_t> &buffer) { | ||||
// * strprintf("%.1s", (char*)nullptr); | // * strprintf("%.1s", (char*)nullptr); | ||||
// | // | ||||
// ()strprintf(format_string, (char*)nullptr); | // ()strprintf(format_string, (char*)nullptr); | ||||
// | // | ||||
// Upstream bug report: https://github.com/c42f/tinyformat/issues/70 | // Upstream bug report: https://github.com/c42f/tinyformat/issues/70 | ||||
try { | try { | ||||
(void)strprintf(format_string, (signed char *)nullptr); | (void)strprintf(format_string, (signed char *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (signed char *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (uint8_t *)nullptr); | (void)strprintf(format_string, (uint8_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (uint8_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (void *)nullptr); | (void)strprintf(format_string, (void *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (void *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (bool *)nullptr); | (void)strprintf(format_string, (bool *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (bool *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (float *)nullptr); | (void)strprintf(format_string, (float *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (float *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (double *)nullptr); | (void)strprintf(format_string, (double *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (double *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (int16_t *)nullptr); | (void)strprintf(format_string, (int16_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (int16_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (uint16_t *)nullptr); | (void)strprintf(format_string, (uint16_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (uint16_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (int32_t *)nullptr); | (void)strprintf(format_string, (int32_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (int32_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (uint32_t *)nullptr); | (void)strprintf(format_string, (uint32_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (uint32_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (int64_t *)nullptr); | (void)strprintf(format_string, (int64_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (int64_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
(void)strprintf(format_string, (uint64_t *)nullptr); | (void)strprintf(format_string, (uint64_t *)nullptr); | ||||
(void)tinyformat::format(bilingual_string, (uint64_t *)nullptr); | |||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
try { | try { | ||||
switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 5)) { | switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 5)) { | ||||
case 0: | case 0: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeRandomLengthString(32)); | fuzzed_data_provider.ConsumeRandomLengthString(32)); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeRandomLengthString(32)); | |||||
break; | break; | ||||
case 1: | case 1: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeRandomLengthString(32).c_str()); | fuzzed_data_provider.ConsumeRandomLengthString(32).c_str()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeRandomLengthString(32).c_str()); | |||||
break; | break; | ||||
case 2: | case 2: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<signed char>()); | fuzzed_data_provider.ConsumeIntegral<signed char>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<signed char>()); | |||||
break; | break; | ||||
case 3: | case 3: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<uint8_t>()); | fuzzed_data_provider.ConsumeIntegral<uint8_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<uint8_t>()); | |||||
break; | break; | ||||
case 4: | case 4: | ||||
(void)strprintf(format_string, | (void)strprintf(format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<char>()); | fuzzed_data_provider.ConsumeIntegral<char>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<char>()); | |||||
break; | break; | ||||
case 5: | case 5: | ||||
(void)strprintf(format_string, | (void)strprintf(format_string, | ||||
fuzzed_data_provider.ConsumeBool()); | fuzzed_data_provider.ConsumeBool()); | ||||
(void)tinyformat::format(bilingual_string, | |||||
fuzzed_data_provider.ConsumeBool()); | |||||
break; | break; | ||||
} | } | ||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
if (format_string.find('%') != std::string::npos && | if (format_string.find('%') != std::string::npos && | ||||
format_string.find('c') != std::string::npos) { | format_string.find('c') != std::string::npos) { | ||||
// Avoid triggering the following: | // Avoid triggering the following: | ||||
Show All 17 Lines | void test_one_input(const std::vector<uint8_t> &buffer) { | ||||
} | } | ||||
try { | try { | ||||
switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 7)) { | switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 7)) { | ||||
case 0: | case 0: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeFloatingPoint<float>()); | fuzzed_data_provider.ConsumeFloatingPoint<float>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeFloatingPoint<float>()); | |||||
break; | break; | ||||
case 1: | case 1: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeFloatingPoint<double>()); | fuzzed_data_provider.ConsumeFloatingPoint<double>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeFloatingPoint<double>()); | |||||
break; | break; | ||||
case 2: | case 2: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<int16_t>()); | fuzzed_data_provider.ConsumeIntegral<int16_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<int16_t>()); | |||||
break; | break; | ||||
case 3: | case 3: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<uint16_t>()); | fuzzed_data_provider.ConsumeIntegral<uint16_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<uint16_t>()); | |||||
break; | break; | ||||
case 4: | case 4: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<int32_t>()); | fuzzed_data_provider.ConsumeIntegral<int32_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<int32_t>()); | |||||
break; | break; | ||||
case 5: | case 5: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<uint32_t>()); | fuzzed_data_provider.ConsumeIntegral<uint32_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<uint32_t>()); | |||||
break; | break; | ||||
case 6: | case 6: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<int64_t>()); | fuzzed_data_provider.ConsumeIntegral<int64_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<int64_t>()); | |||||
break; | break; | ||||
case 7: | case 7: | ||||
(void)strprintf( | (void)strprintf( | ||||
format_string, | format_string, | ||||
fuzzed_data_provider.ConsumeIntegral<uint64_t>()); | fuzzed_data_provider.ConsumeIntegral<uint64_t>()); | ||||
(void)tinyformat::format( | |||||
bilingual_string, | |||||
fuzzed_data_provider.ConsumeIntegral<uint64_t>()); | |||||
break; | break; | ||||
} | } | ||||
} catch (const tinyformat::format_error &) { | } catch (const tinyformat::format_error &) { | ||||
} | } | ||||
} | } |