From 2486558ae1d15e878e2df4d067858a710f168580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gardou?= Date: Fri, 29 Jan 2021 18:15:17 +0100 Subject: [PATCH] [RTL] Do not mess with critical section lock when there is no reason to. - When process is shutting down. - When the caller is so drunk that they leave twice the pub altough they entered it only once. --- sdk/lib/rtl/critical.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sdk/lib/rtl/critical.c b/sdk/lib/rtl/critical.c index c9b9c68ff6d..0911a8ce76f 100644 --- a/sdk/lib/rtl/critical.c +++ b/sdk/lib/rtl/critical.c @@ -114,12 +114,6 @@ RtlpWaitForCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) EXCEPTION_RECORD ExceptionRecord; BOOLEAN LastChance = FALSE; - /* Do we have an Event yet? */ - if (!CriticalSection->LockSemaphore) - { - RtlpCreateCriticalSectionSem(CriticalSection); - } - /* Increase the Debug Entry count */ DPRINT("Waiting on Critical Section Event: %p %p\n", CriticalSection, @@ -136,10 +130,15 @@ RtlpWaitForCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) LdrpShutdownThreadId == NtCurrentTeb()->RealClientId.UniqueThread) { DPRINT("Forcing ownership of critical section %p\n", CriticalSection); - CriticalSection->LockCount = 0; return STATUS_SUCCESS; } + /* Do we have an Event yet? */ + if (!CriticalSection->LockSemaphore) + { + RtlpCreateCriticalSectionSem(CriticalSection); + } + for (;;) { /* Increase the number of times we've had contention */ @@ -715,9 +714,13 @@ RtlLeaveCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) */ if (--CriticalSection->RecursionCount) { + if (CriticalSection->RecursionCount < 0) + { + DPRINT1("CRITICAL SECTION MESS: Section %p is not acquired!\n", CriticalSection); + return STATUS_UNSUCCESSFUL; + } /* Someone still owns us, but we are free. This needs to be done atomically. */ InterlockedDecrement(&CriticalSection->LockCount); - } else {