ByteBuffer: fix a UInt underflow

This commit is contained in:
Johannes Weiß committed 2017-11-30 16:57:26 +00:00
1 parent 3f38979347
commit a22fc02c51
3 files changed
+45 -1

No files matched your search

+1 -1
View File
@@ -141,7 +141,7 @@ public struct ByteBuffer {
// double the capacity until the requested capacity can be full-filled
repeat {
newCapacity = newCapacity << 1
} while newCapacity - index < capacity
} while newCapacity < index || newCapacity - index < capacity
self._storage.reallocStorage(capacity: newCapacity)
self._slice = _slice.lowerBound..<_slice.lowerBound + newCapacity
@@ -97,6 +97,10 @@ extension ByteBufferTest {
("testWriteForNonContiguousCollections", testWriteForNonContiguousCollections),
("testReadStringOkay", testReadStringOkay),
("testReadStringTooMuch", testReadStringTooMuch),
("testSetIntegerBeyondCapacity", testSetIntegerBeyondCapacity),
("testGetIntegerBeyondCapacity", testGetIntegerBeyondCapacity),
("testSetStringBeyondCapacity", testSetStringBeyondCapacity),
("testGetStringBeyondCapacity", testGetStringBeyondCapacity),
]
}
}
+40
View File
@@ -950,4 +950,44 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual("a", buf.readString(length: 1))
}
func testSetIntegerBeyondCapacity() throws {
buf.clear()
buf.changeCapacity(to: 32)
XCTAssertLessThan(buf.capacity, 200)
buf.set(integer: 17, at: 201)
let i: Int = buf.integer(at: 201)!
XCTAssertEqual(17, i)
XCTAssertGreaterThanOrEqual(buf.capacity, 200 + MemoryLayout.size(ofValue: i))
}
func testGetIntegerBeyondCapacity() throws {
buf.clear()
buf.changeCapacity(to: 32)
XCTAssertLessThan(buf.capacity, 200)
let i: Int? = buf.integer(at: 201)
XCTAssertNil(i)
}
func testSetStringBeyondCapacity() throws {
buf.clear()
buf.changeCapacity(to: 32)
XCTAssertLessThan(buf.capacity, 200)
buf.set(string: "HW", at: 201)
let s = buf.string(at: 201, length: 2)!
XCTAssertEqual("HW", s)
XCTAssertGreaterThanOrEqual(buf.capacity, 202)
}
func testGetStringBeyondCapacity() throws {
buf.clear()
buf.changeCapacity(to: 32)
XCTAssertLessThan(buf.capacity, 200)
let i: String? = buf.string(at: 201, length: 1)
XCTAssertNil(i)
}
}