Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Fix memory leaks on macOS (#293)
Browse files Browse the repository at this point in the history
These leaks popped up in the Xcode memory leak tool.
  • Loading branch information
appden authored Oct 12, 2020
1 parent 13633ed commit a6ee40c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/keytar_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -277,13 +286,10 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service,
return FAIL_ERROR;
}


if (result != NULL) {
CFRelease(result);
}

CFRelease(query);

return SUCCESS;
}

Expand Down

0 comments on commit a6ee40c

Please sign in to comment.