Author SHA1 Message Date
Manuel Gauto 43b1632877 Adjust defaulting logic for handling nil case of Credentials. Completes fix for #100. 2021-02-05 19:26:55 -05:00
Manuel Gauto e1c3e89e09 Merge remote-tracking branch https://github.com/joehinkle11/SwiftGit2.git into applesilicon-support.
Initial fix for Credentials BAD_ACCESS error.
2021-02-05 19:23:07 -05:00
Joe Hinkle 3c7c920c47 Update Credentials.swift 2020-12-17 15:40:24 -05:00
Joe Hinkle 5a56c22a97 Update Credentials.swift 2020-12-15 18:10:22 -05:00
Joe Hinkle 39f160344e Credentials fix for BAD_ACCESS
This is a fix for an old issue: https://github.com/SwiftGit2/SwiftGit2/issues/100

There's a crash caused by accessing the pointer to the creds object after libgit2 frees it. libgit2 also is calling credentialsCallback, so there is no good way to prevent the crash from happening. Interestingly, libgit2 calls the callback twice, and the result of the second one doesn't seem too important. So because we don't have control over how libgit2 chooses to free objects, I just wrote a simple change to prevent the second call of credentialsCallback from accessing the pointer to the creds object.
2020-12-03 18:42:13 -05:00
+19 -2
View File
@@ -22,8 +22,23 @@ public enum Credentials {
case plaintext(username: String, password: String)
case sshMemory(username: String, publicKey: String, privateKey: String, passphrase: String)
internal static func fromPointer(_ pointer: UnsafeMutableRawPointer) -> Credentials {
return Unmanaged<Wrapper<Credentials>>.fromOpaque(UnsafeRawPointer(pointer)).takeRetainedValue().value
private static var previouslyUsedPointer: String? = nil
internal static func fromPointer(_ pointer: UnsafeMutableRawPointer) -> Credentials? {
// check if we had just seen this pointer
if pointer.debugDescription == previouslyUsedPointer {
// we have already used this pointer, so it is likely that libgit2
// has already freed the memory and using Unmanaged<>.fromOpaque will
// result in a BAD_ACCESS crash
return nil
} else {
// mark that we have used this pointer so that
// later attempts to use it in this function will
// be blocked
previouslyUsedPointer = pointer.debugDescription
// access the pointer and convert it into a
// Credentials Swift object
return Unmanaged<Wrapper<Credentials>>.fromOpaque(UnsafeRawPointer(pointer)).takeRetainedValue().value
}
}
internal func toPointer() -> UnsafeMutableRawPointer {
@@ -55,6 +70,8 @@ internal func credentialsCallback(
result = git_cred_userpass_plaintext_new(cred, username, password)
case .sshMemory(let username, let publicKey, let privateKey, let passphrase):
result = git_cred_ssh_key_memory_new(cred, username, publicKey, privateKey, passphrase)
default:
result = git_cred_default_new(cred)
}
return (result != GIT_OK.rawValue) ? -1 : 0