bug: mkdir_r uses strtok() which is not thread-safe #35

Closed
opened 2026-07-20 14:58:02 +02:00 by TapTap · 0 comments
Owner

In src/shared/utils.c:29, mkdir_r() uses strtok() to split the path into components:

const char* part = strtok(path_duplicate, delimiter);

strtok() uses a hidden static buffer and is not thread-safe. If two threads call mkdir_r() simultaneously (e.g., during multithreaded file writing on the server), the paths will be interleaved and corrupted.

Additionally, strtok() modifies the input string in-place (which is why it takes char* not const char*), but the code already makes a duplicate for this purpose.

Fix: Replace strtok() with strtok_r() (the reentrant version) or manually parse the string character-by-character.

Location: src/shared/utils.c:29

Severity: high

In `src/shared/utils.c:29`, `mkdir_r()` uses `strtok()` to split the path into components: ```c const char* part = strtok(path_duplicate, delimiter); ``` `strtok()` uses a hidden static buffer and is **not thread-safe**. If two threads call `mkdir_r()` simultaneously (e.g., during multithreaded file writing on the server), the paths will be interleaved and corrupted. Additionally, `strtok()` modifies the input string in-place (which is why it takes `char*` not `const char*`), but the code already makes a duplicate for this purpose. **Fix:** Replace `strtok()` with `strtok_r()` (the reentrant version) or manually parse the string character-by-character. **Location:** `src/shared/utils.c:29` **Severity:** high
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TapTap/FastSync#35