Skip to content

Commit

Permalink
Fixed deallocating issue in read()
Browse files Browse the repository at this point in the history
  • Loading branch information
amosavian committed Jul 17, 2018
1 parent db87cfb commit eaa1a5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
5 changes: 3 additions & 2 deletions AMSMB2/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -287,8 +288,8 @@ extension SMB2Context {

static let async_handler: @convention(c) (_ smb2: UnsafeMutablePointer<smb2_context>?, _ 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
Expand Down
25 changes: 14 additions & 11 deletions AMSMB2/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt8>.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<UInt8>.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 {
Expand Down

0 comments on commit eaa1a5d

Please sign in to comment.