Skip to content

Commit 9ba47f2

Browse files
Fix bad handle usage and leaking
1 parent 86012cf commit 9ba47f2

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

ffrunner.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ fetch_icon(char *iconUrl, char *outPath)
622622
{
623623
HANDLE iconFile;
624624
HANDLE doneSignal;
625+
HANDLE workerSignal;
625626
DWORD waitResult;
626627

627628
iconFile = gen_temp_file(outPath);
@@ -632,7 +633,18 @@ fetch_icon(char *iconUrl, char *outPath)
632633

633634
logmsg("Downloading icon to %s\n", outPath);
634635
doneSignal = CreateEventA(NULL, TRUE, FALSE, NULL);
635-
register_temp_request(iconUrl, iconFile, doneSignal);
636+
637+
/*
638+
* Duplicate the handle so the worker has independent ownership.
639+
* If we time out and close our handle, the worker can still safely
640+
* SetEvent on its own copy without races.
641+
*/
642+
643+
DuplicateHandle(GetCurrentProcess(), doneSignal,
644+
GetCurrentProcess(), &workerSignal,
645+
0, FALSE, DUPLICATE_SAME_ACCESS);
646+
647+
register_temp_request(iconUrl, iconFile, workerSignal);
636648

637649
/* 3-second timeout for icon download so we don't hang if something goes wrong */
638650
waitResult = WaitForSingleObject(doneSignal, 3000);

requests.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,12 @@ handle_request(PTP_CALLBACK_INSTANCE inst, void *reqArg, PTP_WORK work)
606606
}
607607
}
608608

609+
/* Close handles here, because req has been freed at this point */
609610
if (doneEvent != INVALID_HANDLE_VALUE) {
610611
SetEvent(doneEvent);
612+
CloseHandle(doneEvent);
611613
}
614+
CloseHandle(readyEvent);
612615
}
613616

614617
static void

0 commit comments

Comments
 (0)