Skip to content

Commit 7facf6d

Browse files
committed
eliminates possible deadlock related to memory allocation on global heap (#70)
1 parent 3326512 commit 7facf6d

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

src/detours.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,12 @@ _Benign_race_end_
16011601
s_pPendingThreads = NULL;
16021602
s_ppPendingError = NULL;
16031603

1604+
if (!HeapLock(GetProcessHeap())) {
1605+
s_nPendingError = GetLastError();
1606+
1607+
return s_nPendingError;
1608+
}
1609+
16041610
// Make sure the trampoline pages are writable.
16051611
s_nPendingError = detour_writable_trampoline_regions();
16061612

@@ -1645,10 +1651,19 @@ LONG WINAPI DetourTransactionAbort()
16451651
delete t;
16461652
t = n;
16471653
}
1654+
1655+
// s_nPendingError should not be overwritten as DetourTransactionCommitEx relies on it.
1656+
LONG nReportedError = NO_ERROR;
1657+
1658+
if (!HeapUnlock(GetProcessHeap())) {
1659+
// This may mask a preceding error but we can't do anything about it as we can report a single error.
1660+
s_nPendingError = nReportedError = GetLastError();
1661+
}
1662+
16481663
s_pPendingThreads = NULL;
16491664
s_nPendingThreadId = 0;
16501665

1651-
return NO_ERROR;
1666+
return nReportedError;
16521667
}
16531668

16541669
LONG WINAPI DetourTransactionCommit()
@@ -1935,6 +1950,11 @@ typedef ULONG_PTR DETOURS_EIP_TYPE;
19351950
delete t;
19361951
t = n;
19371952
}
1953+
1954+
if (!HeapUnlock(GetProcessHeap())) {
1955+
s_nPendingError = GetLastError();
1956+
}
1957+
19381958
s_pPendingThreads = NULL;
19391959
s_nPendingThreadId = 0;
19401960

0 commit comments

Comments
 (0)