mkdir_r() has potential buffer overflow in path construction loop #50
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
In
src/shared/utils.clines 11-49, themkdir_r()function allocates a buffer ofstrlen(path) + 2bytes for building directory paths, but then usesstrcpy()to append path components without bounds checking:Each
strcpywrites without checking if the remaining buffer is large enough. The allocationstrlen(path) + 2barely fits the original path plus a null terminator, but each intermediate directory component is built incrementally by appending to the buffer, potentially overflowing if the path has directory separators in unexpected positions.Impact
While the specific use case in this codebase is likely safe (paths come from the system and
strtoksegments the path), this is a latent memory safety bug. An attacker-controlled or unusually structured path could trigger a buffer overflow.Location
src/shared/utils.c:16-48Suggested Fix
Replace the manual buffer arithmetic with
snprintf()or useasprintf()for safe path construction. Alternatively, allocate usingstrlen(path) + strlen(path) + 2(double the path length) for safety margin.Better: Use a simpler recursive approach that creates directories one at a time, or use
snprintf:Severity
Medium
Category
Bug
This issue was automatically generated by the issue-creator agent.