asn1: decode Counter64 as unsigned so values >= 2^63 are not rejected (#36) - #37
Open
youdie006 wants to merge 1 commit into
Open
asn1: decode Counter64 as unsigned so values >= 2^63 are not rejected (#36)#37youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
Counter64 is an unsigned INTEGER (0..2^64-1). A value with bit 63 set is BER-encoded with a leading 0x00 sign octet and therefore occupies 9 octets. read_snmp_counter64 decoded it through the signed decode_i64, which rejects anything longer than 8 octets with AsnIntOverflow. Because Varbinds treats a decode error as the end of the sequence, the offending varbind and every varbind after it were silently dropped from the response. Decode Counter64 through a dedicated unsigned path that accepts up to 8 octets, or 9 octets when the extra leading octet is the 0x00 sign pad. Add regression tests for 2^63 and u64::MAX. Fixes roboplc#36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #36.
Problem
Counter64is an unsignedINTEGER (0..2^64-1)(RFC 2578). A value with bit 63 set is BER-encoded with a leading0x00sign octet, so it occupies 9 octets.read_snmp_counter64decoded it through the signedread_i64_type/decode_i64, which rejects anything longer than 8 octets withAsnIntOverflow.Worse,
Varbinds::nexttreats any decode failure as the end of the sequence, so the failingCounter64varbind and every varbind after it are silently dropped. A response whose only highCounter64is at the end returns zero varbinds; au64::MAXcounter in the middle of a 3-varbind response yields just one.Fix
Decode
Counter64through a dedicated unsigned path (decode_u64) that accepts up to 8 octets, or 9 octets when the extra leading octet is the0x00sign pad. This is scoped to the value-decode bug; the broaderVarbinds::next"treat error as end-of-sequence" behavior is left as a separate concern (as suggested in the issue).Test
Added
read_counter64_large_valuescovering a small value,2^63, andu64::MAX. Red-green verified withcargo test: before the change the2^63/u64::MAXcases fail withAsnIntOverflow; after, they decode correctly. The full test suite passes (11 lib + 6 integration), andcargo fmt --check/cargo clippyare clean.Disclosure: I used AI assistance (Claude) while preparing this change. I verified the encoding against RFC 2578, ran the tests (red-green) and the full suite, and take responsibility for the contribution.