Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sources/BinaryParsing/Parsers/Integer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions Tests/BinaryParsingTests/IntegerParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Loading