ByteBuffer: avoid potential overflows for reading functions

This commit is contained in:
Johannes Weiss committed 2017-10-24 13:24:47 +01:00
1 parent 25d57e8fab
commit e9c76088ac
6 files changed
+50 -10

No files matched your search

+4 -4
View File
@@ -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
}
+3 -3
View File
@@ -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)
+2 -2
View File
@@ -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
+1 -1
View File
@@ -34,7 +34,7 @@ extension ByteBuffer {
public func integer<T: EndiannessInteger>(at index: Int, endianness: Endianness = Endianness.big) -> T? {
return self.withVeryUnsafeBytes { ptr in
if index + MemoryLayout<T>.size > ptr.count {
guard index <= ptr.count - MemoryLayout<T>.size && index >= 0 else {
return nil
}
var value: T = 0
@@ -92,6 +92,7 @@ extension ByteBufferTest {
("testReadTooLong", testReadTooLong),
("testReadWithUnsafeReadableBytesVariantsNothingToRead", testReadWithUnsafeReadableBytesVariantsNothingToRead),
("testReadWithUnsafeReadableBytesVariantsSomethingToRead", testReadWithUnsafeReadableBytesVariantsSomethingToRead),
("testSomePotentialIntegerUnderOrOverflows", testSomePotentialIntegerUnderOrOverflows),
]
}
}
+39
View File
@@ -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<T>(_ 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<T>(_ 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)
}
}