fixed integer overflow in envelope.go #893
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix makeEnvelopePrefix type safety for 32-bit builds
The original implementation of makeEnvelopePrefix accepted size int and compared it against math.MaxUint32.
This caused a build failure on 32-bit architectures (e.g. GOARCH=arm GOARM=7) because int is 32-bit on those platforms, and the constant math.MaxUint32 (4294967295) cannot fit in an int without overflowing.
What changed
Updated the function signature to accept size uint32 instead of int.
Removed the redundant bounds check (size < 0 and > math.MaxUint32), since uint32 already encodes the valid domain for the 4-byte envelope length field.
Ensured that the type matches the protocol specification: the envelope length prefix is always a 4-byte unsigned integer in big-endian order.
On both 32-bit and 64-bit builds, the function now works without type conversion or overflow issues.
Callers passing len(payload) must explicitly cast to uint32 and can guard against oversize payloads (len(payload) > math.MaxUint32), which matches the maximum representable size of the envelope field.
This aligns the code with the protocol definition and avoids platform-dependent build errors.
Related Issue
#886