From eaa1a5d2a3fda285fba39024bf224d6d7ce00439 Mon Sep 17 00:00:00 2001 From: Amir Abbas Date: Wed, 18 Jul 2018 03:17:58 +0430 Subject: [PATCH] Fixed deallocating issue in read() --- AMSMB2/Context.swift | 5 +++-- AMSMB2/FileHandle.swift | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/AMSMB2/Context.swift b/AMSMB2/Context.swift index 07dcf60..b4318a2 100644 --- a/AMSMB2/Context.swift +++ b/AMSMB2/Context.swift @@ -270,7 +270,8 @@ extension SMB2Context { pfd.events = Int16(whichEvents()) if poll(&pfd, 1, 1000) < 0, errno != EAGAIN { - try POSIXError.throwIfError(errno, description: error, default: .EINVAL) + let code = POSIXErrorCode(rawValue: errno) ?? .EINVAL + throw POSIXError(code, description: error) } if pfd.revents == 0 { @@ -287,8 +288,8 @@ extension SMB2Context { static let async_handler: @convention(c) (_ smb2: UnsafeMutablePointer?, _ status: Int32, _ command_data: UnsafeMutableRawPointer?, _ cbdata: UnsafeMutableRawPointer?) -> Void = { smb2, status, command_data, cbdata in cbdata?.bindMemory(to: CBData.self, capacity: 1).pointee.result = status - cbdata?.bindMemory(to: CBData.self, capacity: 1).pointee.isFinished = true cbdata?.bindMemory(to: CBData.self, capacity: 1).pointee.commandData = command_data + cbdata?.bindMemory(to: CBData.self, capacity: 1).pointee.isFinished = true } @discardableResult diff --git a/AMSMB2/FileHandle.swift b/AMSMB2/FileHandle.swift index 2ccbcad..f61601f 100644 --- a/AMSMB2/FileHandle.swift +++ b/AMSMB2/FileHandle.swift @@ -110,26 +110,29 @@ final class SMB2FileHandle { func read(length: Int = 0) throws -> Data { precondition(length <= UInt32.max, "Length bigger than UInt32.max can't be handled by libsmb2.") - let bufSize = length > 0 ? length : optimizedReadSize - let buffer = UnsafeMutablePointer.allocate(capacity: bufSize) - buffer.initialize(repeating: 0, count: bufSize) + let count = length > 0 ? length : optimizedReadSize + var data = Data(count: count) let (result, _) = try context.async_await(defaultError: .EIO) { (context, cbPtr) -> Int32 in - smb2_read_async(context, handle, buffer, UInt32(bufSize), SMB2Context.async_handler, cbPtr) + data.withUnsafeMutableBytes { buffer in + smb2_read_async(context, handle, buffer, UInt32(count), SMB2Context.async_handler, cbPtr) + } } - return Data(bytesNoCopy: buffer, count: Int(result), deallocator: .free) + data.count = Int(result) + return data } func pread(offset: UInt64, length: Int = 0) throws -> Data { precondition(length <= UInt32.max, "Length bigger than UInt32.max can't be handled by libsmb2.") - let bufSize = length > 0 ? length : optimizedReadSize - let buffer = UnsafeMutablePointer.allocate(capacity: bufSize) - buffer.initialize(repeating: 0, count: bufSize) - + let count = length > 0 ? length : optimizedReadSize + var data = Data(count: count) let (result, _) = try context.async_await(defaultError: .EIO) { (context, cbPtr) -> Int32 in - smb2_pread_async(context, handle, buffer, UInt32(bufSize), offset, SMB2Context.async_handler, cbPtr) + data.withUnsafeMutableBytes { buffer in + smb2_pread_async(context, handle, buffer, UInt32(count), offset, SMB2Context.async_handler, cbPtr) + } } - return Data(bytesNoCopy: buffer, count: Int(result), deallocator: .free) + data.count = Int(result) + return data } var maxWriteSize: Int {