Skip to content

Commit b41e3a6

Browse files
authored
Merge pull request #2 from batonPiotr/bugfix/memory-leak-in-error-case-hashPasswordBytes
Moved pointer release to a `defer` clause to handle all exit cases
2 parents 233fb65 + 8e3cc29 commit b41e3a6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Sources/Swift/Argon2Swift.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public class Argon2Swift {
6868
// Allocate a pointer for the hash and the encoded hash
6969
let hash = setPtr(length: length)
7070
let encoded = setPtr(length: encodedLen)
71+
72+
// Free the previously created pointers when the program is out of scope
73+
defer {
74+
freePtr(pointer: hash, length: length)
75+
freePtr(pointer: encoded, length: encodedLen)
76+
}
7177

7278
// Perform the actual hash operation
7379
let errorCode = argon2_hash(UInt32(iterations), UInt32(memory), UInt32(parallelism), [UInt8](password), password.count, [UInt8](salt.bytes), salt.bytes.count, hash, length, encoded, encodedLen, getArgon2Type(type: type), UInt32(version.rawValue))
@@ -85,10 +91,6 @@ public class Argon2Swift {
8591
// Create an instance of Argon2SwiftResult with the arrays
8692
let result = Argon2SwiftResult(hashBytes: hashArray, encodedBytes: encodedArray)
8793

88-
// Free the previously created pointers
89-
freePtr(pointer: hash, length: length)
90-
freePtr(pointer: encoded, length: encodedLen)
91-
9294
// Return the result from above
9395
return result
9496
}

0 commit comments

Comments
 (0)