Reject out-of-range and empty integer field values#765
Open
raphaelroshan wants to merge 1 commit into
Open
Conversation
atoi/parseUInt silently overflowed on integer field values that exceed the int range: e.g. "9223372036854775808" wrapped to a negative number and "18446744073709551617" to 1, all returned with a nil error. Because atoi backs FIXInt.Read (MsgSeqNum, BodyLength, resend ranges, etc.), a malformed or oversized field silently corrupted the parsed value instead of being rejected. parseUInt now detects overflow before it happens and returns an error. atoi also length-checks its input before indexing d[0], which previously panicked on an empty integer field. Adds tests for the overflow and empty-input cases.
4b0d094 to
4daa4b5
Compare
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.
Problem
atoi/parseUIntinfix_int.gobackFIXInt.Read, which parses integer FIX fields includingMsgSeqNum (34),BodyLength (9), and resend-range fields. Two issues:Silent overflow.
parseUIntaccumulatesn = n*10 + digitwith no range check, so an out-of-range value wraps silently and is returned with anilerror:"9223372036854775808"(MaxInt64+1) →-9223372036854775808"18446744073709551617"(MaxUint64+1) →1"99999999999999999999"→ wrapped garbageA malformed or oversized field (e.g. tag 34) is therefore accepted with a corrupted value instead of being rejected, which can feed wrong values into sequence-number handling.
Empty-input panic.
atoireadsd[0]before checking the length, so an empty integer field value panics withindex out of range [0] with length 0.Fix
parseUIntdetects overflow before it happens (n > (math.MaxInt-digit)/10) and returns an error.atoilength-checks its input before indexingd[0].Both keep the allocation-free hot path. Added tests for the overflow and empty-input cases; existing tests and the full package suite pass.