Skip to content

Conversation

@davidmrdavid
Copy link

@davidmrdavid davidmrdavid commented Jun 18, 2025

By running msvc's ASan on this project, we detected a buffer-overflow error in RaiseException call in CoreThreadWin's SetThreadName.

See the relevant bit below

void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
  THREADNAME_INFO info;
  info.dwType = 0x1000;
  info.szName = szThreadName;
  info.dwThreadID = dwThreadID;
  info.dwFlags = 0;

  __try
  {
    // Magic exception which informs the OS of this thread's name
    RaiseException(0x406D1388, 0, sizeof(info)/sizeof(DWORD), (ULONG_PTR*)&info);
  }
  __except(EXCEPTION_CONTINUE_EXECUTION)
  {
  }
}

This method follows the pattern outlined in these docs with one difference, it is dividing sizeof(info) by sizeof(DWORD) instead of sizeof(ULONG_PTR).

This difference is fine for 32-bit architectures (1 pointer = 1 DWORD, or 4 bytes), but not for 64 bit, where 1 pointer is 2 DWORDS (8 bytes).

This PR aims to correct this minor arithmetic error, so it works across all architectures. Thanks!

@davidmrdavid davidmrdavid changed the title [Bug] [Bug] Fix buffer-overflow in SetThreadName Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant