Fix memory leaks on error paths

This commit is contained in:
Frank Denis 2017-09-23 20:30:28 +02:00
parent f977141b51
commit e31629b8d3
2 changed files with 21 additions and 3 deletions

View File

@ -142,17 +142,23 @@ public class GenericHash {
result = crypto_generichash_init(state, nil, 0, outputLength)
}
if result != 0 {
free()
return nil
}
self.outputLength = outputLength
}
deinit {
private func free() {
guard let state = state else {
return
}
let rawState = UnsafeMutableRawPointer(state).bindMemory(to: UInt8.self, capacity: crypto_generichash_statebytes())
rawState.deallocate(capacity: 1)
self.state = nil
}
deinit {
free()
}
/**

View File

@ -80,6 +80,7 @@ public class SecretStream {
}
}
if result != 0 {
free()
return nil
}
}
@ -126,12 +127,17 @@ public class SecretStream {
crypto_secretstream_xchacha20poly1305_rekey(state)
}
deinit {
private func free() {
guard let state = state else {
return
}
let rawState = UnsafeMutableRawPointer(state).bindMemory(to: UInt8.self, capacity: crypto_secretstream_xchacha20poly1305_statebytes())
rawState.deallocate(capacity: 1)
self.state = nil
}
deinit {
free()
}
}
@ -153,6 +159,7 @@ public class SecretStream {
}
}
if result != 0 {
free()
return nil
}
}
@ -195,12 +202,17 @@ public class SecretStream {
crypto_secretstream_xchacha20poly1305_rekey(state)
}
deinit {
private func free() {
guard let state = state else {
return
}
let rawState = UnsafeMutableRawPointer(state).bindMemory(to: UInt8.self, capacity: crypto_secretstream_xchacha20poly1305_statebytes())
rawState.deallocate(capacity: 1)
self.state = nil
}
deinit {
free()
}
}
}