Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <test/fuzz/FuzzedDataProvider.h>6#include <test/fuzz/fuzz.h>7#include <test/fuzz/util.h>8#include <tinyformat.h>9#include <util/strencodings.h>10#include <util/translation.h>12#include <algorithm>13#include <cstdint>14#include <string>15#include <vector>17template <typename... Args>18void fuzz_fmt(const std::string& fmt, const Args&... args)19{20(void)tfm::format(tfm::RuntimeFormat{fmt}, args...);21}23FUZZ_TARGET(str_printf)24{25FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());26const std::string format_string = fuzzed_data_provider.ConsumeRandomLengthString(64);28const int digits_in_format_specifier = std::count_if(format_string.begin(), format_string.end(), IsDigit);30// Avoid triggering the following crash bug:31// * strprintf("%987654321000000:", 1);32//33// Avoid triggering the following OOM bug:34// * strprintf("%.222222200000000$", 1.1);35//36// Upstream bug report: https://github.com/c42f/tinyformat/issues/7037if (format_string.find('%') != std::string::npos && digits_in_format_specifier >= 7) {38return;39}41// Avoid triggering the following crash bug:42// * strprintf("%1$*1$*", -11111111);43//44// Upstream bug report: https://github.com/c42f/tinyformat/issues/7045if (format_string.find('%') != std::string::npos && format_string.find('$') != std::string::npos && format_string.find('*') != std::string::npos && digits_in_format_specifier > 0) {46return;47}49// Avoid triggering the following crash bug:50// * strprintf("%.1s", (char*)nullptr);51//52// (void)strprintf(format_string, (char*)nullptr);53//54// Upstream bug report: https://github.com/c42f/tinyformat/issues/7056try {57CallOneOf(58fuzzed_data_provider,59[&] {60fuzz_fmt(format_string, fuzzed_data_provider.ConsumeRandomLengthString(32));61},62[&] {63fuzz_fmt(format_string, fuzzed_data_provider.ConsumeRandomLengthString(32).c_str());64},65[&] {66fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<signed char>());67},68[&] {69fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<unsigned char>());70},71[&] {72fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<char>());73},74[&] {75fuzz_fmt(format_string, fuzzed_data_provider.ConsumeBool());76});77} catch (const tinyformat::format_error&) {78}80if (format_string.find('%') != std::string::npos && format_string.find('c') != std::string::npos) {81// Avoid triggering the following:82// * strprintf("%c", 1.31783e+38);83// tinyformat.h:244:36: runtime error: 1.31783e+38 is outside the range of representable values of type 'char'84return;85}87if (format_string.find('%') != std::string::npos && format_string.find('*') != std::string::npos) {88// Avoid triggering the following:89// * strprintf("%*", -2.33527e+38);90// tinyformat.h:283:65: runtime error: -2.33527e+38 is outside the range of representable values of type 'int'91// * strprintf("%*", -2147483648);92// tinyformat.h:763:25: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself93return;94}96try {97CallOneOf(98fuzzed_data_provider,99[&] {100fuzz_fmt(format_string, fuzzed_data_provider.ConsumeFloatingPoint<float>());101},102[&] {103fuzz_fmt(format_string, fuzzed_data_provider.ConsumeFloatingPoint<double>());104},105[&] {106fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<int16_t>());107},108[&] {109fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<uint16_t>());110},111[&] {112fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<int32_t>());113},114[&] {115fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<uint32_t>());116},117[&] {118fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<int64_t>());119},120[&] {121fuzz_fmt(format_string, fuzzed_data_provider.ConsumeIntegral<uint64_t>());122});123} catch (const tinyformat::format_error&) {124}125}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.