Repository navigation
Conversation
…andler safety ### Description This PR improves the reliability and safety of the server crash handler (`Server/core/CCrashHandler.cpp`) across both POSIX and Windows platforms: 1. **Fix stack buffer overflow in `SaveBacktraceSummary()` (Linux/macOS):** `backtrace()` expects the buffer capacity as an element count (`NUMELMS(buffer)`), not the byte size (`sizeof buffer`). On 64-bit systems, passing `sizeof buffer` passed `800` for a 100-element array, risking a stack buffer overflow inside the crash handler during deep-stack crashes (e.g., infinite recursion). 2. **Use thread-safe `localtime_r` on POSIX:** Replaced `localtime()` with `localtime_r()` to avoid mutating shared static state during crash handling (matching `localtime_s` already used in the Windows path). 3. **Avoid heap allocation in `HandleExceptionGlobal` (Windows):** Allocated `CExceptionInformation_Impl` on the stack instead of using `new (std::nothrow)` so the crash handler does not touch a potentially corrupted process heap before writing the crash log. Also removed the now-unused `<new>` header. 4. **Clean up empty/corrupt `.dmp` files on failure (Windows):** If `TryWriteDump` fails after `CreateFile` has already created the file on disk, delete the leftover 0-byte/incomplete dump file via `DeleteFileA`.
### Summary of Changes - **Fixed POSIX Stack Buffer Overflow:** Corrected `backtrace()` call in `SaveBacktraceSummary()` to pass element count (`NUMELMS(buffer)`) instead of byte size (`sizeof buffer`), preventing out-of-bounds writes on 64-bit Linux/macOS systems during deep-stack crashes. - **Improved Windows Crash Handler Safety:** Allocated `CExceptionInformation_Impl` directly on the stack instead of using heap allocation (`new`), avoiding deadlocks or faults if the process heap is corrupted at the time of the exception. - **Thread-Safe Time Formatting:** Replaced non-thread-safe `localtime()` with `localtime_r()` on POSIX platforms. - **Dump Cleanup:** Added automatic deletion (`DeleteFileA`) of incomplete/0-byte `.dmp` files on Windows if `TryWriteDump` fails. - **Code Formatting:** Adjusted spacing and alignment to fully comply with project `clang-format` standards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…andler safety
Description
This PR improves the reliability and safety of the server crash handler (
Server/core/CCrashHandler.cpp) across both POSIX and Windows platforms:SaveBacktraceSummary()(Linux/macOS):backtrace()expects the buffer capacity as an element count (NUMELMS(buffer)), not the byte size (sizeof buffer). On 64-bit systems, passingsizeof bufferpassed800for a 100-element array, risking a stack buffer overflow inside the crash handler during deep-stack crashes (e.g., infinite recursion).localtime_ron POSIX: Replacedlocaltime()withlocaltime_r()to avoid mutating shared static state during crash handling (matchinglocaltime_salready used in the Windows path).HandleExceptionGlobal(Windows): AllocatedCExceptionInformation_Implon the stack instead of usingnew (std::nothrow)so the crash handler does not touch a potentially corrupted process heap before writing the crash log. Also removed the now-unused<new>header..dmpfiles on failure (Windows): IfTryWriteDumpfails afterCreateFilehas already created the file on disk, delete the leftover 0-byte/incomplete dump file viaDeleteFileA.Summary
Motivation
Test plan
Checklist