gthread-win32: No need to raise thread name exception if the debugger is not present

As outlined in [1], raising the exception without a debugger attached
isn't of any use, as the thread name won't be stored anywhere.
The system doesn't trap such exception, only debuggers.

This also fixes a TOCTTOU issue between the IsDebuggerPresent() check
and the RaiseException() call.

References:
 [1] - https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2022#set-a-thread-name-by-throwing-an-exception
This commit is contained in:
Luca Bacci 2023-09-04 16:14:30 +02:00
parent fb3f1733c3
commit f4b53cacbf

View File

@ -605,10 +605,7 @@ SetThreadName (DWORD dwThreadID,
{
}
#else
/* Without a debugger we *must* have an exception handler,
* otherwise raising an exception will crash the process.
*/
if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle == NULL))
if ((!IsDebuggerPresent ()) || (SetThreadName_VEH_handle == NULL))
return;
RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (const ULONG_PTR *) &info);