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 <netaddress.h>6#include <util/asmap.h>7#include <test/fuzz/fuzz.h>9#include <cstdint>10#include <optional>11#include <vector>13#include <cassert>15std::vector<std::byte> BitsToBytes(std::span<const uint8_t> bits) noexcept16{17std::vector<std::byte> ret;18uint8_t next_byte{0};19int next_byte_bits{0};20for (uint8_t val : bits) {21next_byte |= (val & 1) << (next_byte_bits++);22if (next_byte_bits == 8) {23ret.push_back(std::byte(next_byte));24next_byte = 0;25next_byte_bits = 0;26}27}28if (next_byte_bits) ret.push_back(std::byte(next_byte));30return ret;31}33FUZZ_TARGET(asmap_direct)34{35// Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]36std::optional<size_t> sep_pos_opt;37for (size_t pos = 0; pos < buffer.size(); ++pos) {38uint8_t x = buffer[pos];39if ((x & 0xFE) == 0) continue;40if (x == 0xFF) {41if (sep_pos_opt) return;42sep_pos_opt = pos;43} else {44return;45}46}47if (!sep_pos_opt) return; // Needs exactly 1 separator48const size_t sep_pos{sep_pos_opt.value()};49const size_t ip_len{buffer.size() - sep_pos - 1};50if (ip_len > 128) return; // At most 128 bits in IP address52// Checks on asmap53auto asmap = BitsToBytes(buffer.first(sep_pos));54if (SanityCheckAsmap(asmap, ip_len)) {55// Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid.56for (size_t prefix_len = sep_pos - 1; prefix_len > 0; --prefix_len) {57auto prefix = BitsToBytes(buffer.first(prefix_len));58// We have to skip the prefixes of the same length as the original59// asmap, since they will contain some zero padding bits in the last60// byte.61if (prefix.size() == asmap.size()) continue;62assert(!SanityCheckAsmap(prefix, ip_len));63}65// No address input should trigger assertions in interpreter66auto addr = BitsToBytes(buffer.subspan(sep_pos + 1));67(void)Interpret(asmap, addr);68}69}
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.