diff --git a/Sources/BinaryParsing/Parsers/Integer.swift b/Sources/BinaryParsing/Parsers/Integer.swift index 2366ffd..42cbd76 100644 --- a/Sources/BinaryParsing/Parsers/Integer.swift +++ b/Sources/BinaryParsing/Parsers/Integer.swift @@ -12,8 +12,7 @@ extension ParserSpan { @inlinable public func _checkCount(minimum: Int) throws(ParsingError) { - let requiredUpper = _lowerBound &+ minimum - guard requiredUpper <= _upperBound else { + guard count >= minimum else { throw ParsingError( status: .insufficientData, location: startPosition) diff --git a/Tests/BinaryParsingTests/IntegerParsingTests.swift b/Tests/BinaryParsingTests/IntegerParsingTests.swift index 96652a9..b683d8d 100644 --- a/Tests/BinaryParsingTests/IntegerParsingTests.swift +++ b/Tests/BinaryParsingTests/IntegerParsingTests.swift @@ -655,4 +655,15 @@ struct IntegerParsingTests { let result = try lebEncoded.withParserSpan { try Int(parsingLEB128: &$0) } #expect(result == expected) } + + @Test + func checkCountDoesNotOverflow() throws { + try bigEndianOnes.withParserSpanIfAvailable { span in + // Consume some bytes so that `startPosition` is nonzero. + _ = try UInt32(parsingBigEndian: &span) + #expect(throws: ParsingError.self) { + try span._checkCount(minimum: .max) + } + } + } }