From a6ee40c1315ff1683a9435e3efa8c3114f7c35ad Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 12 Oct 2020 14:30:35 -0700 Subject: [PATCH] Fix memory leaks on macOS (#293) These leaks popped up in the Xcode memory leak tool. --- src/keytar_mac.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/keytar_mac.cc b/src/keytar_mac.cc index 457fa4c3..93741bd6 100644 --- a/src/keytar_mac.cc +++ b/src/keytar_mac.cc @@ -213,9 +213,12 @@ Credentials getCredentialsForItem(CFDictionaryRef item) { CFDictionaryAddValue(query, kSecReturnData, kCFBooleanTrue); CFDictionaryAddValue(query, kSecAttrAccount, account); - CFTypeRef result; + Credentials cred; + CFTypeRef result = NULL; OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, &result); + CFRelease(query); + if (status == errSecSuccess) { CFDataRef passwordData = (CFDataRef) CFDictionaryGetValue( (CFDictionaryRef) result, @@ -225,15 +228,18 @@ Credentials getCredentialsForItem(CFDictionaryRef item) { passwordData, kCFStringEncodingUTF8); - Credentials cred = Credentials( + cred = Credentials( CFStringToStdString(account), CFStringToStdString(password)); + CFRelease(password); + } - return cred; + if (result != NULL) { + CFRelease(result); } - return Credentials(); + return cred; } KEYTAR_OP_RESULT FindCredentials(const std::string& service, @@ -255,9 +261,12 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service, CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue); CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue); - CFTypeRef result; + CFTypeRef result = NULL; OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, &result); + CFRelease(serviceStr); + CFRelease(query); + if (status == errSecSuccess) { CFArrayRef resultArray = (CFArrayRef) result; int resultCount = CFArrayGetCount(resultArray); @@ -277,13 +286,10 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service, return FAIL_ERROR; } - if (result != NULL) { CFRelease(result); } - CFRelease(query); - return SUCCESS; }