Skip to content

Commit 9b348ff

Browse files
committed
Fix memory leaks in qt/guiutil.cpp
on macOS: listSnapshot was leaking in findStartupItemInList() bitcoinAppUrl was leaking in [Get|Set]StartOnSystemStartup()
1 parent 07c92b9 commit 9b348ff

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/qt/guiutil.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -780,47 +780,64 @@ bool SetStartOnSystemStartup(bool fAutoStart)
780780
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl);
781781
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl)
782782
{
783-
// loop through the list of startup items and try to find the bitcoin app
784783
CFArrayRef listSnapshot = LSSharedFileListCopySnapshot(list, nullptr);
784+
if (listSnapshot == nullptr) {
785+
return nullptr;
786+
}
787+
788+
// loop through the list of startup items and try to find the bitcoin app
785789
for(int i = 0; i < CFArrayGetCount(listSnapshot); i++) {
786790
LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(listSnapshot, i);
787791
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
788792
CFURLRef currentItemURL = nullptr;
789793

790794
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED >= 10100
791-
if(&LSSharedFileListItemCopyResolvedURL)
792-
currentItemURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, nullptr);
795+
if(&LSSharedFileListItemCopyResolvedURL)
796+
currentItemURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, nullptr);
793797
#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && MAC_OS_X_VERSION_MIN_REQUIRED < 10100
794-
else
795-
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
798+
else
799+
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
796800
#endif
797801
#else
798-
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
802+
LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, nullptr);
799803
#endif
800804

801-
if(currentItemURL && CFEqual(currentItemURL, findUrl)) {
802-
// found
803-
CFRelease(currentItemURL);
804-
return item;
805-
}
806805
if(currentItemURL) {
806+
if (CFEqual(currentItemURL, findUrl)) {
807+
// found
808+
CFRelease(listSnapshot);
809+
CFRelease(currentItemURL);
810+
return item;
811+
}
807812
CFRelease(currentItemURL);
808813
}
809814
}
815+
816+
CFRelease(listSnapshot);
810817
return nullptr;
811818
}
812819

813820
bool GetStartOnSystemStartup()
814821
{
815822
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
823+
if (bitcoinAppUrl == nullptr) {
824+
return false;
825+
}
826+
816827
LSSharedFileListRef loginItems = LSSharedFileListCreate(nullptr, kLSSharedFileListSessionLoginItems, nullptr);
817828
LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, bitcoinAppUrl);
829+
830+
CFRelease(bitcoinAppUrl);
818831
return !!foundItem; // return boolified object
819832
}
820833

821834
bool SetStartOnSystemStartup(bool fAutoStart)
822835
{
823836
CFURLRef bitcoinAppUrl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
837+
if (bitcoinAppUrl == nullptr) {
838+
return false;
839+
}
840+
824841
LSSharedFileListRef loginItems = LSSharedFileListCreate(nullptr, kLSSharedFileListSessionLoginItems, nullptr);
825842
LSSharedFileListItemRef foundItem = findStartupItemInList(loginItems, bitcoinAppUrl);
826843

@@ -832,6 +849,8 @@ bool SetStartOnSystemStartup(bool fAutoStart)
832849
// remove item
833850
LSSharedFileListItemRemove(loginItems, foundItem);
834851
}
852+
853+
CFRelease(bitcoinAppUrl);
835854
return true;
836855
}
837856
#pragma GCC diagnostic pop

0 commit comments

Comments
 (0)