Optimize NtGetContextThread a bit and fix use of unitinialized memory if PreviousMode == KernelMode and Thread == CurrentThread (thanks to Thomas for spotting!) Modified: trunk/reactos/ntoskrnl/ps/debug.c _____
Modified: trunk/reactos/ntoskrnl/ps/debug.c --- trunk/reactos/ntoskrnl/ps/debug.c 2005-11-03 00:17:55 UTC (rev 18966) +++ trunk/reactos/ntoskrnl/ps/debug.c 2005-11-03 00:34:09 UTC (rev 18967) @@ -86,6 +86,7 @@
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); GET_SET_CTX_CONTEXT GetSetContext; NTSTATUS Status = STATUS_SUCCESS; + PCONTEXT SafeThreadContext = NULL;
PAGED_CODE();
@@ -98,6 +99,7 @@ sizeof(CONTEXT), sizeof(ULONG)); GetSetContext.Context = *ThreadContext; + SafeThreadContext = &GetSetContext.Context;
} _SEH_HANDLE {
@@ -106,6 +108,8 @@ } _SEH_END;
if(!NT_SUCCESS(Status)) return Status; + } else { + SafeThreadContext = ThreadContext; }
/* Get the Thread Object */ @@ -121,15 +125,20 @@
/* Check if we're running in the same thread */ if(Thread == PsGetCurrentThread()) { - /* * I don't know if trying to get your own context makes much * sense but we can handle it more efficently. */ - KeTrapFrameToContext(Thread->Tcb.TrapFrame, NULL, &GetSetContext.Context); + KeTrapFrameToContext(Thread->Tcb.TrapFrame, NULL, SafeThreadContext);
} else {
+ /* Copy context into GetSetContext if not already done */ + if(PreviousMode == KernelMode) { + GetSetContext.Context = *ThreadContext; + SafeThreadContext = &GetSetContext.Context; + } + /* Use an APC... Initialize the Event */ KeInitializeEvent(&GetSetContext.Event, NotificationEvent, @@ -173,7 +182,7 @@ ObDereferenceObject(Thread);
/* Check for success and return the Context */ - if(NT_SUCCESS(Status)) { + if(NT_SUCCESS(Status) && SafeThreadContext != ThreadContext) { _SEH_TRY {
*ThreadContext = GetSetContext.Context;