Skip to content

Fix BIP276 decoding, AuthFetch data race, and add NUM2BIN tests#288

Merged
shruggr merged 9 commits intomasterfrom
fix/issues-286-262
Feb 12, 2026
Merged

Fix BIP276 decoding, AuthFetch data race, and add NUM2BIN tests#288
shruggr merged 9 commits intomasterfrom
fix/issues-286-262

Conversation

@shruggr
Copy link
Collaborator

@shruggr shruggr commented Jan 29, 2026

Summary

This PR addresses three issues:

Fix BIP276 decoding bugs (#286)

  • Changed regex from \d{2} to [0-9A-Fa-f]{2} to support hex digits A-F in network/version fields
  • Changed + to * in data pattern to allow empty data
  • Changed strconv.Atoi to strconv.ParseInt(..., 16, 64) for proper hex parsing
  • Fixed network/version field order (were swapped during decode)

Fix data race in AuthFetch (#262)

  • Added sync.RWMutex to protect concurrent access to peers, callbacks, and certificatesReceived maps/slices
  • Added thread-safe accessor methods: GetPeer, SetPeer, GetCallback, SetCallback, DeleteCallback, AppendCertificatesReceived, etc.
  • Updated Fetch() and SendCertificateRequest() to use thread-safe methods

Add test for large stack data NUM2BIN operations (#261)

Test plan

  • All existing tests pass
  • New BIP276 tests pass (including hex digits A-F, empty data, field order)
  • AuthFetch race detection test passes (go test -race)
  • NUM2BIN large data tests pass (100KB and 10MB with AfterGenesis)

🤖 Generated with Claude Code

shruggr and others added 3 commits January 29, 2026 12:52
- Fix regex to accept hex digits A-F in network/version fields
  (changed \d{2} to [0-9A-Fa-f]{2})
- Fix hex parsing using strconv.ParseInt with base 16 instead of Atoi
- Fix network/version field order (were swapped during decode)
- Allow empty data in BIP276 strings (changed + to * in regex)
- Add comprehensive tests for roundtrip encode/decode

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add mutex protection to AuthFetch struct for concurrent map/slice access:

- Add sync.RWMutex to protect callbacks, peers, and certificatesReceived
- Add thread-safe accessor methods: GetPeer, SetPeer, DeletePeer,
  GetCallback, SetCallback, DeleteCallback, InvokeCallback,
  AppendCertificatesReceived, GetOrCreatePeer, UpdatePeerIdentity, etc.
- Update Fetch and SendCertificateRequest to use thread-safe accessors
- Add TestAuthFetchConcurrentMapAccess race detection test

This mirrors the mutex pattern already used in auth/peer.go (callbacksMu).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add regression test confirming that large data items (up to 10MB) can be
created on the stack using NUM2BIN when WithAfterGenesis() is enabled.

The test uses the exact script template from issue #261 and verifies:
- 100KB and 10MB operations succeed with AfterGenesis
- Operations correctly fail with pre-genesis 520-byte limit without AfterGenesis
- Documents when "cannot fit it into n sized array" error occurs (target size
  smaller than the minimal encoding of the input data)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@shruggr shruggr requested a review from rohenaz as a code owner January 29, 2026 18:30
@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 10.76923% with 58 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
auth/clients/authhttp/authhttp.go 3.33% 58 Missing ⚠️

📢 Thoughts on this report? Let us know!

shruggr and others added 6 commits January 29, 2026 13:44
Use ParseUint with 8-bit size constraint instead of ParseInt with
64-bit to properly bound network and version values (0-255 range).
This addresses the CodeQL alerts about potential integer overflow
when converting int64 to int on 32-bit systems.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace manual map + mutex pattern with sync.Map for peers and callbacks.
This simplifies the code by removing helper methods and inlining thread-safe
operations. The certificatesReceived slice still uses a dedicated mutex.

Benefits:
- Reduced code complexity (removed ~100 lines of helper methods)
- Better test coverage since operations are inlined
- sync.Map is optimized for concurrent read-heavy workloads

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace require.Empty(authFetch.peers) with Range-based check
since sync.Map contains noCopy and cannot be passed by value.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
BIP276 spec defines format as: <prefix>:<version><network><data><checksum>
The previous implementation had version and network swapped in both
encode and decode functions, breaking interoperability with other
BIP276 implementations.

Changes:
- Encode: put version before network (was network before version)
- Decode: read version from res[2], network from res[3] (was swapped)
- Update tests with correct expected values

This is a breaking change for any existing BIP276 strings encoded
with this library, but is necessary for spec compliance.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sonarqubecloud
Copy link

@shruggr shruggr merged commit 725db51 into master Feb 12, 2026
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants