[Swift] Fix padding function overflow when bufSize is 0 (#5721)

* [Swift] Fix padding function overflow when bufSize is 0

[Swift] Generate linuxmain

* [Swift] Using the overflow addition operator to resolve integer overflow
This commit is contained in:
vkill 2020-01-25 01:45:19 +08:00 committed by Wouter van Oortmerssen
parent bab2b0db48
commit 7cdfc8475e
3 changed files with 10 additions and 1 deletions

View File

@ -219,7 +219,7 @@ extension FlatBufferBuilder {
/// - bufSize: Current size of the buffer + the offset of the object to be written /// - bufSize: Current size of the buffer + the offset of the object to be written
/// - elementSize: Element size /// - elementSize: Element size
fileprivate func padding(bufSize: UInt32, elementSize: UInt32) -> UInt32 { fileprivate func padding(bufSize: UInt32, elementSize: UInt32) -> UInt32 {
((~bufSize) + 1) & (elementSize - 1) ((~bufSize) &+ 1) & (elementSize - 1)
} }
/// Prealigns the buffer before writting a new object into the buffer /// Prealigns the buffer before writting a new object into the buffer

View File

@ -25,6 +25,14 @@ final class FlatBuffersVectors: XCTestCase {
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]) XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0])
} }
func testCreateEmptyIntArray() {
let numbers: [Int32] = []
let b = FlatBufferBuilder(initialSize: 20)
let o = b.createVector(numbers, size: numbers.count)
b.finish(offset: o)
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 0, 0, 0, 0])
}
func testCreateVectorOfStrings() { func testCreateVectorOfStrings() {
let strs = ["Denmark", "Norway"] let strs = ["Denmark", "Norway"]
let b = FlatBufferBuilder(initialSize: 20) let b = FlatBufferBuilder(initialSize: 20)

View File

@ -72,6 +72,7 @@ extension FlatBuffersVectors {
// `swift test --generate-linuxmain` // `swift test --generate-linuxmain`
// to regenerate. // to regenerate.
static let __allTests__FlatBuffersVectors = [ static let __allTests__FlatBuffersVectors = [
("testCreateEmptyIntArray", testCreateEmptyIntArray),
("testCreateIntArray", testCreateIntArray), ("testCreateIntArray", testCreateIntArray),
("testCreateSharedStringVector", testCreateSharedStringVector), ("testCreateSharedStringVector", testCreateSharedStringVector),
("testCreateVectorOfStrings", testCreateVectorOfStrings), ("testCreateVectorOfStrings", testCreateVectorOfStrings),