From d64078eb274e952056a071e74e3afce28d09e7a8 Mon Sep 17 00:00:00 2001 From: Liu Liu Date: Thu, 25 Jun 2020 15:11:38 -0700 Subject: [PATCH] [Swift] Initialize memory when clear ByteBuffer (#5982) * Initialize memory when clear ByteBuffer It seems (based on my limited understanding) that FlatBuffers requires the writable area to be 0 initialized. However, we missed it when clear the buffer to reinitialize it. This PR fixed that by calling initialize (also fixed the typo) explicitly. * Update version to 0.5.3 --- swift/FlatBuffers.podspec | 2 +- swift/Sources/FlatBuffers/ByteBuffer.swift | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/swift/FlatBuffers.podspec b/swift/FlatBuffers.podspec index 6cf2c13f7..4cd0938ea 100644 --- a/swift/FlatBuffers.podspec +++ b/swift/FlatBuffers.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'FlatBuffers' - s.version = '0.5.2' + s.version = '0.5.3' s.summary = 'FlatBuffers: Memory Efficient Serialization Library' s.description = "FlatBuffers is a cross platform serialization library architected for diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift index 7a4f89198..af2c8ada0 100644 --- a/swift/Sources/FlatBuffers/ByteBuffer.swift +++ b/swift/Sources/FlatBuffers/ByteBuffer.swift @@ -35,7 +35,7 @@ public struct ByteBuffer { memory.copyMemory(from: ptr, byteCount: count) } - func initalize(for size: Int) { + func initialize(for size: Int) { precondition(!unowned) memset(memory, 0, size) } @@ -104,7 +104,7 @@ public struct ByteBuffer { init(initialSize size: Int) { let size = size.convertToPowerofTwo _storage = Storage(count: size, alignment: alignment) - _storage.initalize(for: size) + _storage.initialize(for: size) } /// Constructor that creates a Flatbuffer from unsafe memory region without copying @@ -264,6 +264,7 @@ public struct ByteBuffer { alignment = 1 _storage.memory.deallocate() _storage.memory = UnsafeMutableRawPointer.allocate(byteCount: _storage.capacity, alignment: alignment) + _storage.initialize(for: _storage.capacity) } /// Resizes the buffer size