refactor: Simplify Interpret asmap function
What changed, and why it matters
This is a small code cleanup (refactor) in Bitcoin Core's asmap handling. It renames a function to use consistent capitalization and moves variable declarations closer to where they are used. There is no change to behavior, no bug fix, and no security-relevant change.
No action needed; this is a non-security refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors Interpret() in src/util/asmap.cpp by declaring variables inside the loop where they are used instead of at the top of the function, reducing scope. It also changes matchlen’s type from uint32_t to int and the loop counter accordingly. The function SanityCheckASMap is renamed to SanityCheckAsmap for consistent casing, with call sites updated in the fuzz test and CheckStandardAsmap(). The diff shows no logic changes, no new checks, no removed checks, and no changes to parsing or validation behavior.
Changed components
src/util/asmap.cppsrc/util/asmap.hsrc/test/fuzz/asmap_direct.cppInspect captured patch +14 / −16
diff --git a/src/test/fuzz/asmap_direct.cpp b/src/test/fuzz/asmap_direct.cpp
index fa775532..a338c133 100644
--- a/src/test/fuzz/asmap_direct.cpp
+++ b/src/test/fuzz/asmap_direct.cpp
@@ -51,7 +51,7 @@ FUZZ_TARGET(asmap_direct)
// Checks on asmap
auto asmap = BitsToBytes(buffer.first(sep_pos));
- if (SanityCheckASMap(asmap, ip_len)) {
+ if (SanityCheckAsmap(asmap, ip_len)) {
// Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid.
for (size_t prefix_len = sep_pos - 1; prefix_len > 0; --prefix_len) {
auto prefix = BitsToBytes(buffer.first(prefix_len));
@@ -59,7 +59,7 @@ FUZZ_TARGET(asmap_direct)
// asmap, since they will contain some zero padding bits in the last
// byte.
if (prefix.size() == asmap.size()) continue;
- assert(!SanityCheckASMap(prefix, ip_len));
+ assert(!SanityCheckAsmap(prefix, ip_len));
}
// No address input should trigger assertions in interpreter
diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp
index ed1d3b0b..4d4ed4fe 100644
--- a/src/util/asmap.cpp
+++ b/src/util/asmap.cpp
@@ -186,18 +186,16 @@ uint32_t Interpret(const std::span<const std::byte> asmap, const std::span<const
uint8_t ip_bit{0};
const uint8_t ip_bits_end = ip.size() * 8;
uint32_t default_asn = 0;
- uint32_t jump, match, matchlen;
- Instruction opcode;
while (pos < endpos) {
- opcode = DecodeType(pos, asmap);
+ Instruction opcode = DecodeType(pos, asmap);
if (opcode == Instruction::RETURN) {
// Found leaf node - return the ASN
- default_asn = DecodeASN(pos, asmap);
- if (default_asn == INVALID) break; // ASN straddles EOF
- return default_asn;
+ uint32_t asn = DecodeASN(pos, asmap);
+ if (asn == INVALID) break; // ASN straddles EOF
+ return asn;
} else if (opcode == Instruction::JUMP) {
// Binary branch: if IP bit is 1, jump forward; else continue
- jump = DecodeJump(pos, asmap);
+ uint32_t jump = DecodeJump(pos, asmap);
if (jump == INVALID) break; // Jump offset straddles EOF
if (ip_bit == ip_bits_end) break; // No input bits left
if (int64_t{jump} >= static_cast<int64_t>(endpos - pos)) break; // Jumping past EOF
@@ -210,11 +208,11 @@ uint32_t Interpret(const std::span<const std::byte> asmap, const std::span<const
// The match value encodes both length and pattern:
// - highest set bit position determines length (bit_width - 1)
// - lower bits contain the pattern to compare
- match = DecodeMatch(pos, asmap);
+ uint32_t match = DecodeMatch(pos, asmap);
if (match == INVALID) break; // Match bits straddle EOF
- matchlen = std::bit_width(match) - 1; // An n-bit value matches n-1 input bits
+ int matchlen = std::bit_width(match) - 1; // An n-bit value matches n-1 input bits
if ((ip_bits_end - ip_bit) < matchlen) break; // Not enough input bits
- for (uint32_t bit = 0; bit < matchlen; bit++) {
+ for (int bit = 0; bit < matchlen; bit++) {
if (ConsumeBitBE(ip_bit, ip) != ((match >> (matchlen - 1 - bit)) & 1)) {
return default_asn; // Pattern mismatch - use default
}
@@ -229,7 +227,7 @@ uint32_t Interpret(const std::span<const std::byte> asmap, const std::span<const
}
}
// Reached EOF without RETURN, or aborted (see any of the breaks above)
- // - should have been caught by SanityCheckASMap below
+ // - should have been caught by SanityCheckAsmap below
assert(false);
return 0; // 0 is not a valid ASN
}
@@ -238,7 +236,7 @@ uint32_t Interpret(const std::span<const std::byte> asmap, const std::span<const
* Validates ASMap structure by simulating all possible execution paths.
* Ensures well-formed bytecode, valid jumps, and proper termination.
*/
-bool SanityCheckASMap(const std::span<const std::byte> asmap, int bits)
+bool SanityCheckAsmap(const std::span<const std::byte> asmap, int bits)
{
size_t pos{0};
const size_t endpos{asmap.size() * 8};
@@ -311,7 +309,7 @@ bool SanityCheckASMap(const std::span<const std::byte> asmap, int bits)
*/
bool CheckStandardAsmap(const std::span<const std::byte> data)
{
- if (!SanityCheckASMap(data, 128)) {
+ if (!SanityCheckAsmap(data, 128)) {
LogWarning("Sanity check of asmap data failed\n");
return false;
}
diff --git a/src/util/asmap.h b/src/util/asmap.h
index 1d5f6e81..5d65e219 100644
--- a/src/util/asmap.h
+++ b/src/util/asmap.h
@@ -15,7 +15,7 @@
uint32_t Interpret(std::span<const std::byte> asmap, std::span<const std::byte> ip);
-bool SanityCheckASMap(std::span<const std::byte> asmap, int bits);
+bool SanityCheckAsmap(std::span<const std::byte> asmap, int bits);
/** Check standard asmap data (128 bits for IPv6) */
bool CheckStandardAsmap(std::span<const std::byte> data);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.