aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/thread.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2026-03-06 10:11:51 +0100
committerGitHub Enterprise <[email protected]>2026-03-06 10:11:51 +0100
commit1e731796187ad73b2dee44b48fcecdd487616394 (patch)
tree0ea37769f743ae1fb2eacc37bc8ccfa88ecc0d64 /src/zencore/thread.cpp
parentfix oidctoken exe lookup check (#811) (diff)
downloadzen-1e731796187ad73b2dee44b48fcecdd487616394.tar.xz
zen-1e731796187ad73b2dee44b48fcecdd487616394.zip
Claude config, some bug fixes (#813)
* Claude config updates * Bug fixes and hardening across `zencore` and `zenhttp`, identified via static analysis. ### zencore - **`ZEN_ASSERT` macro** -- extended to accept an optional string message literal; added `ZEN_ASSERT_MSG_` helper for message formatting. Callers needing runtime fmt-style formatting should use `ZEN_ASSERT_FORMAT`. - **`MpscQueue`** -- fixed `TypeCompatibleStorage` to use a properly-sized `char Storage[sizeof(T)]` array instead of a single `char`; corrected `Data()` to cast `&Storage` rather than `this`; switched cache-line alignment to a fixed constant to avoid GCC's `-Winterference-size` warning. Enabled previously-disabled tests. - **`StringBuilderImpl`** -- initialized `m_Base`/`m_CurPos`/`m_End` to `nullptr`. Fixed `StringCompare` return type (`bool` -> `int`). Fixed `ParseInt` to reject strings with trailing non-numeric characters. Removed deprecated `<codecvt>` include. - **`NiceNumGeneral`** -- replaced `powl()` with integer `IntPow()` to avoid floating-point precision issues. - **`RwLock::ExclusiveLockScope`** -- added move constructor/assignment; initialized `m_Lock` to `nullptr`. - **`Latch::AddCount`** -- fixed variable type (`std::atomic_ptrdiff_t` -> `std::ptrdiff_t` for the return value of `fetch_add`). - **`thread.cpp`** -- fixed Linux `pthread_setname_np` 16-byte name truncation; added null check before dereferencing in `Event::Close()`; fixed `NamedEvent::Close()` to call `close(Fd)` outside the lock region; added null guard in `NamedMutex` destructor; `Sleep()` now returns early for non-positive durations. - **`MD5Stream`** -- was entirely stubbed out (no-op); now correctly calls `MD5Init`/`MD5Update`/`MD5Final`. Fixed `ToHexString` to use the correct string length. Fixed forward declarations. Fixed tests to compare `compare() == 0`. - **`sentryintegration.cpp`** -- guard against null `filename`/`funcname` in spdlog message handler to prevent a crash in `fmt::format`. - **`jobqueue.cpp`** -- fixed lost job ID when `IdGenerator` wraps around zero; fixed raw `Job*` in `RunningJobs` map (potential use-after-free) to `RefPtr<Job>`; fixed range-loop copies; fixed format string typo. - **`trace.cpp`** -- suppress GCC false-positive warnings in third-party `trace.h` include. ### zenhttp - **WebSocket close race** (`wsasio`, `wshttpsys`, `httpwsclient`) -- `m_CloseSent` promoted from `bool` to `std::atomic<bool>`; close check changed to `exchange(true)` to eliminate the check-then-set data race. - **`wsframecodec.cpp`** -- reject WebSocket frames with payload > 256 MB to prevent OOM from malformed/malicious frames. - **`oidc.cpp`** -- URL-encode refresh token and client ID in token requests (`FormUrlEncode`); parse `end_session_endpoint` and `device_authorization_endpoint` from OIDC discovery document. - **`httpclientcommon.cpp`** -- propagate error code from `AppendData` when flushing the cache buffer. - **`httpclient.h`** -- initialize all uninitialized members (`ErrorCode`, `UploadedBytes`, `DownloadedBytes`, `ElapsedSeconds`, `MultipartBoundary` fields). - **`httpserver.h`** -- fix `operator=` return type for `HttpRpcHandler` (missing `&`). - **`packageformat.h`** -- fix `~0u` (32-bit truncation) to `~uint64_t(0)` for a `uint64_t` field. - **`httpparser`** -- initialize `m_RequestVerb` in both declaration and `ResetState()`. - **`httpplugin.cpp`** -- initialize `m_BasePort`; fix format string missing quotes around connection name. - **`httptracer.h`** -- move `#pragma once` before includes. - **`websocket.h`** -- initialize `WebSocketMessage::Opcode`. ### zenserver - **`hubservice.cpp`** -- fix two `ZEN_ASSERT` calls that incorrectly used fmt-style format args; converted to `ZEN_ASSERT_FORMAT`.
Diffstat (limited to 'src/zencore/thread.cpp')
-rw-r--r--src/zencore/thread.cpp42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp
index 9e3486e49..54459cbaa 100644
--- a/src/zencore/thread.cpp
+++ b/src/zencore/thread.cpp
@@ -133,7 +133,10 @@ SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName)
#elif ZEN_PLATFORM_MAC
pthread_setname_np(ThreadNameZ.c_str());
#else
- pthread_setname_np(pthread_self(), ThreadNameZ.c_str());
+ // Linux pthread_setname_np has a 16-byte limit (15 chars + NUL)
+ StringBuilder<16> LinuxThreadName;
+ LinuxThreadName << LimitedThreadName.substr(0, 15);
+ pthread_setname_np(pthread_self(), LinuxThreadName.c_str());
#endif
} // namespace zen
@@ -233,12 +236,15 @@ Event::Close()
#else
std::atomic_thread_fence(std::memory_order_acquire);
auto* Inner = (EventInner*)m_EventHandle.load();
+ if (Inner)
{
- std::unique_lock Lock(Inner->Mutex);
- Inner->bSet.store(true);
- m_EventHandle = nullptr;
+ {
+ std::unique_lock Lock(Inner->Mutex);
+ Inner->bSet.store(true);
+ m_EventHandle = nullptr;
+ }
+ delete Inner;
}
- delete Inner;
#endif
}
@@ -351,7 +357,7 @@ NamedEvent::NamedEvent(std::string_view EventName)
intptr_t Packed;
Packed = intptr_t(Sem) << 32;
Packed |= intptr_t(Fd) & 0xffff'ffff;
- m_EventHandle = (void*)Packed;
+ m_EventHandle = (void*)Packed;
#endif
ZEN_ASSERT(m_EventHandle != nullptr);
}
@@ -372,7 +378,9 @@ NamedEvent::Close()
#if ZEN_PLATFORM_WINDOWS
CloseHandle(m_EventHandle);
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
- int Fd = int(intptr_t(m_EventHandle.load()) & 0xffff'ffff);
+ const intptr_t Handle = intptr_t(m_EventHandle.load());
+ const int Fd = int(Handle & 0xffff'ffff);
+ const int Sem = int(Handle >> 32);
if (flock(Fd, LOCK_EX | LOCK_NB) == 0)
{
@@ -388,11 +396,10 @@ NamedEvent::Close()
}
flock(Fd, LOCK_UN | LOCK_NB);
- close(Fd);
-
- int Sem = int(intptr_t(m_EventHandle.load()) >> 32);
semctl(Sem, 0, IPC_RMID);
}
+
+ close(Fd);
#endif
m_EventHandle = nullptr;
@@ -481,9 +488,12 @@ NamedMutex::~NamedMutex()
CloseHandle(m_MutexHandle);
}
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
- int Inner = int(intptr_t(m_MutexHandle));
- flock(Inner, LOCK_UN);
- close(Inner);
+ if (m_MutexHandle)
+ {
+ int Inner = int(intptr_t(m_MutexHandle));
+ flock(Inner, LOCK_UN);
+ close(Inner);
+ }
#endif
}
@@ -516,7 +526,6 @@ NamedMutex::Create(std::string_view MutexName)
if (flock(Inner, LOCK_EX) != 0)
{
close(Inner);
- Inner = 0;
return false;
}
@@ -583,6 +592,11 @@ GetCurrentThreadId()
void
Sleep(int ms)
{
+ if (ms <= 0)
+ {
+ return;
+ }
+
#if ZEN_PLATFORM_WINDOWS
::Sleep(ms);
#else