Skip to content

fix: defensive checks for cwd restore, malloc, and log writes#23

Open
rmorgans wants to merge 3 commits intomobydeck:mainfrom
rmorgans:fix/defensive-checks
Open

fix: defensive checks for cwd restore, malloc, and log writes#23
rmorgans wants to merge 3 commits intomobydeck:mainfrom
rmorgans:fix/defensive-checks

Conversation

@rmorgans
Copy link

Summary

Three small defensive fixes:

  1. socket_with_chdir: always restore cwd after socket operation, even on failure. Preserves errno so the caller sees the original error.
  2. expand_sockname: check malloc() return before use. Exits with error instead of passing NULL to snprintf (undefined behavior).
  3. rotate_log / pty_activity: once logging is disabled (log_fd = -1), no code path touches the fd again. Early guard in rotate_log(), split guard blocks in pty_activity(), and correct return value from open_log().

What bugs these fix

  • cwd: after a failed socket operation with a long path, the process could remain in the wrong working directory.
  • malloc: on OOM, expand_sockname would call snprintf(NULL, ...) — undefined behavior.
  • log: a write failure would close the log fd but subsequent code still incremented log_written, called rotate_log(), and lseek() on the invalid fd.

Test results

202/203 pass (1 pre-existing: test 93). ASan+UBSan clean.

rmorgans and others added 2 commits March 14, 2026 22:49
- socket_with_chdir: always restore cwd, even on socket failure
- expand_sockname: check malloc return before use
- rotate_log/pty_activity: disable logging on write failure instead
  of silently losing data

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Once logging is disabled (log_fd = -1), no code path should
increment log_written, call rotate_log, or lseek/write the fd.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
rmorgans added a commit to rmorgans/atch that referenced this pull request Mar 15, 2026
Verifies that a failed socket operation via socket_with_chdir does
not leave the process in the wrong working directory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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