From 7cdfc8475e3e9d8d1f3330c86a1a15eaf3149d57 Mon Sep 17 00:00:00 2001 From: vkill Date: Sat, 25 Jan 2020 01:45:19 +0800 Subject: [PATCH] [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 --- swift/Sources/FlatBuffers/FlatBufferBuilder.swift | 2 +- .../FlatBuffersVectorsTests.swift | 8 ++++++++ .../FlatBuffers.Test.SwiftTests/XCTestManifests.swift | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index 84fba1be9..023f37641 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -219,7 +219,7 @@ extension FlatBufferBuilder { /// - bufSize: Current size of the buffer + the offset of the object to be written /// - elementSize: Element size 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 diff --git a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersVectorsTests.swift b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersVectorsTests.swift index 5e6301ffe..0064c06fd 100644 --- a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersVectorsTests.swift +++ b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersVectorsTests.swift @@ -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]) } + 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() { let strs = ["Denmark", "Norway"] let b = FlatBufferBuilder(initialSize: 20) diff --git a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/XCTestManifests.swift b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/XCTestManifests.swift index d53a7a6c8..18ad6a062 100644 --- a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/XCTestManifests.swift +++ b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/XCTestManifests.swift @@ -72,6 +72,7 @@ extension FlatBuffersVectors { // `swift test --generate-linuxmain` // to regenerate. static let __allTests__FlatBuffersVectors = [ + ("testCreateEmptyIntArray", testCreateEmptyIntArray), ("testCreateIntArray", testCreateIntArray), ("testCreateSharedStringVector", testCreateSharedStringVector), ("testCreateVectorOfStrings", testCreateVectorOfStrings),