Skip to content

fix macOS crashes in TorManager (invalid SYS_gettid syscall) and bytearray (memcpy block on null pointers)#273

Open
jolavillette wants to merge 3 commits intoRetroShare:masterfrom
jolavillette:FixTorProfileGenerationOnMacOS
Open

fix macOS crashes in TorManager (invalid SYS_gettid syscall) and bytearray (memcpy block on null pointers)#273
jolavillette wants to merge 3 commits intoRetroShare:masterfrom
jolavillette:FixTorProfileGenerationOnMacOS

Conversation

@jolavillette
Copy link
Contributor

Description

This PR fixes two critical, distinct bugs that cause RetroShare to crash on macOS during the Tor hidden service generation and initialization processes. Both issues stem from macOS having stricter system-level constraints and memory handling than Linux.

1. Fix SIGABRT in [TorManager.cpp] (Invalid sys-call)

  • The Issue: RsTor::instance() used syscall(SYS_gettid) to verify it was running on the main thread. However, SYS_gettid is a Linux-only system call that does not exist on macOS (Darwin/XNU). This caused the assert() to fail (getpid() == -1), resulting in a mandatory SIGABRT crash.
  • The Fix: Replaced the OS-dependent system call with a cross-platform check using std::this_thread::get_id(). The thread ID is now securely cached upon the first invocation and correctly asserts against subsequent calls on all platforms.

2. Fix SIGSEGV / Undefined Behavior in [bytearray.h] (Null pointer exceptions)

  • The Issue: [ByteArray] operations (memcpy, memcmp, strncmp) were being called on empty strings (size() == 0), passing nullptr to these low-level C functions. While Linux glibc silently ignores memcpy(nullptr, nullptr, 0), macOS clang/libc++ aggressively flags this as Undefined Behavior and triggers a hard crash.
  • The Fix: Added strict size > 0 and !empty() guard clauses before all memory operations within [bytearray.h]. Additionally, patched an off-by-one truncation bug in ByteArray::replace that inadvertently stripped trailing characters.

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