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

Fix memory leaks on macOS and memory exposure vulnerability #293

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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