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

Commit 0f7c9b7

Browse files
appdenshiftkey
authored andcommitted
Fix memory leaks on macOS
These leaks popped up in the Xcode memory leak tool.
1 parent 13633ed commit 0f7c9b7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/keytar_mac.cc

+14-8
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,12 @@ Credentials getCredentialsForItem(CFDictionaryRef item) {
213213
CFDictionaryAddValue(query, kSecReturnData, kCFBooleanTrue);
214214
CFDictionaryAddValue(query, kSecAttrAccount, account);
215215

216-
CFTypeRef result;
216+
Credentials cred;
217+
CFTypeRef result = NULL;
217218
OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, &result);
218219

220+
CFRelease(query);
221+
219222
if (status == errSecSuccess) {
220223
CFDataRef passwordData = (CFDataRef) CFDictionaryGetValue(
221224
(CFDictionaryRef) result,
@@ -225,15 +228,18 @@ Credentials getCredentialsForItem(CFDictionaryRef item) {
225228
passwordData,
226229
kCFStringEncodingUTF8);
227230

228-
Credentials cred = Credentials(
231+
cred = Credentials(
229232
CFStringToStdString(account),
230233
CFStringToStdString(password));
234+
231235
CFRelease(password);
236+
}
232237

233-
return cred;
238+
if (result != NULL) {
239+
CFRelease(result);
234240
}
235241

236-
return Credentials();
242+
return cred;
237243
}
238244

239245
KEYTAR_OP_RESULT FindCredentials(const std::string& service,
@@ -255,9 +261,12 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service,
255261
CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue);
256262
CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue);
257263

258-
CFTypeRef result;
264+
CFTypeRef result = NULL;
259265
OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, &result);
260266

267+
CFRelease(serviceStr);
268+
CFRelease(query);
269+
261270
if (status == errSecSuccess) {
262271
CFArrayRef resultArray = (CFArrayRef) result;
263272
int resultCount = CFArrayGetCount(resultArray);
@@ -277,13 +286,10 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service,
277286
return FAIL_ERROR;
278287
}
279288

280-
281289
if (result != NULL) {
282290
CFRelease(result);
283291
}
284292

285-
CFRelease(query);
286-
287293
return SUCCESS;
288294
}
289295

0 commit comments

Comments
 (0)