diff --git a/Sources/NIO/ByteBuffer-aux.swift b/Sources/NIO/ByteBuffer-aux.swift index 197710282..26b84ab04 100644 --- a/Sources/NIO/ByteBuffer-aux.swift +++ b/Sources/NIO/ByteBuffer-aux.swift @@ -22,7 +22,7 @@ extension ByteBuffer { // MARK: Bytes ([UInt8]) APIs public func bytes(at index: Int, length: Int) -> [UInt8]? { - guard index + length <= self.capacity else { + guard index >= 0 && length >= 0 && index <= self.capacity - length else { return nil } @@ -33,7 +33,7 @@ extension ByteBuffer { } public mutating func readBytes(length: Int) -> [UInt8]? { - guard self.readableBytes >= length else { + guard length >= 0 && self.readableBytes >= length else { return nil } defer { @@ -74,7 +74,7 @@ extension ByteBuffer { public func string(at index: Int, length: Int) -> String? { return withVeryUnsafeBytes { pointer in - if index + length > pointer.count { + guard index >= 0 && length >= 0 && index <= pointer.count - length else { return nil } return String(decoding: UnsafeBufferPointer(start: pointer.baseAddress?.assumingMemoryBound(to: UInt8.self).advanced(by: index), count: length), @@ -134,7 +134,7 @@ extension ByteBuffer { } public mutating func readSlice(length: Int) -> ByteBuffer? { - guard self.readableBytes >= length else { + guard length >= 0 && self.readableBytes >= length else { return nil } diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index de7b02e72..16b8d1dc6 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -249,11 +249,11 @@ public struct ByteBuffer { } public func slice(at index: Int, length: Int) -> ByteBuffer? { - let index = toIndex(index) - let length = toCapacity(length) - guard index + length <= self._slice.count else { + guard index >= 0 && length >= 0 && index <= self.capacity - length else { return nil } + let index = toIndex(index) + let length = toCapacity(length) var new = self new._slice = self._slice.lowerBound + index ..< self._slice.lowerBound + index+length new.moveReaderIndex(to: 0) diff --git a/Sources/NIO/ByteBuffer-foundation.swift b/Sources/NIO/ByteBuffer-foundation.swift index f2ece9579..ac97bbb6c 100644 --- a/Sources/NIO/ByteBuffer-foundation.swift +++ b/Sources/NIO/ByteBuffer-foundation.swift @@ -18,7 +18,7 @@ extension ByteBuffer { // MARK: Data APIs public mutating func readData(length: Int) -> Data? { - guard self.readableBytes >= length else { + guard length >= 0 && self.readableBytes >= length else { return nil } let data = self.data(at: self.readerIndex, length: length)! /* must work, enough readable bytes */ @@ -41,7 +41,7 @@ extension ByteBuffer { } public func data(at index: Int, length: Int) -> Data? { - guard index + length <= self.capacity else { + guard index >= 0 && length >= 0 && index <= self.capacity - length else { return nil } return self.withVeryUnsafeBytesWithStorageManagement { ptr, storageRef in diff --git a/Sources/NIO/ByteBuffer-int.swift b/Sources/NIO/ByteBuffer-int.swift index ae18ce883..e7305a523 100644 --- a/Sources/NIO/ByteBuffer-int.swift +++ b/Sources/NIO/ByteBuffer-int.swift @@ -34,7 +34,7 @@ extension ByteBuffer { public func integer(at index: Int, endianness: Endianness = Endianness.big) -> T? { return self.withVeryUnsafeBytes { ptr in - if index + MemoryLayout.size > ptr.count { + guard index <= ptr.count - MemoryLayout.size && index >= 0 else { return nil } var value: T = 0 diff --git a/Tests/NIOTests/ByteBufferTest+XCTest.swift b/Tests/NIOTests/ByteBufferTest+XCTest.swift index 6705ca7b0..245f24c81 100644 --- a/Tests/NIOTests/ByteBufferTest+XCTest.swift +++ b/Tests/NIOTests/ByteBufferTest+XCTest.swift @@ -92,6 +92,7 @@ extension ByteBufferTest { ("testReadTooLong", testReadTooLong), ("testReadWithUnsafeReadableBytesVariantsNothingToRead", testReadWithUnsafeReadableBytesVariantsNothingToRead), ("testReadWithUnsafeReadableBytesVariantsSomethingToRead", testReadWithUnsafeReadableBytesVariantsSomethingToRead), + ("testSomePotentialIntegerUnderOrOverflows", testSomePotentialIntegerUnderOrOverflows), ] } } diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index 450837aa6..887aab386 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -868,4 +868,43 @@ class ByteBufferTest: XCTestCase { XCTAssertEqual(6, buf.readerIndex) XCTAssertEqual(8, buf.writerIndex) } + + func testSomePotentialIntegerUnderOrOverflows() throws { + buf.changeCapacity(to: 1024) + buf.write(staticString: "hello world, just some trap bytes here") + + func testIndexAndLengthFunc(_ fn: (Int, Int) -> T?, file: StaticString = #file, line: UInt = #line) { + XCTAssertNil(fn(0, -1), file: file, line: line) + XCTAssertNil(fn(-1, 1), file: file, line: line) + + XCTAssertNil(fn(0, Int.min), file: file, line: line) + XCTAssertNil(fn(Int.min, 0), file: file, line: line) + + XCTAssertNil(fn(Int.min, 1), file: file, line: line) + XCTAssertNil(fn(Int.min, Int.min), file: file, line: line) + XCTAssertNil(fn(Int.min, Int.max), file: file, line: line) + XCTAssertNil(fn(Int.max, Int.min), file: file, line: line) + + XCTAssertNil(fn(Int.max, 1), file: file, line: line) + XCTAssertNil(fn(Int.max - 1, 2), file: file, line: line) + XCTAssertNil(fn(1, Int.max), file: file, line: line) + XCTAssertNil(fn(2, Int.max - 1), file: file, line: line) + } + + func testIndexOrLengthFunc(_ fn: (Int) -> T?, file: StaticString = #file, line: UInt = #line) { + XCTAssertNil(fn(Int.max)) + XCTAssertNil(fn(Int.max - 1)) + XCTAssertNil(fn(Int.min)) + } + + testIndexOrLengthFunc({ x in buf.integer(at: x) as UInt16? }) + testIndexOrLengthFunc({ buf.readSlice(length: $0) }) + testIndexOrLengthFunc({ buf.readBytes(length: $0) }) + testIndexOrLengthFunc({ buf.readData(length: $0) }) + + testIndexAndLengthFunc(buf.bytes) + testIndexAndLengthFunc(buf.data) + testIndexAndLengthFunc(buf.slice) + testIndexAndLengthFunc(buf.string) + } }