Skip to content

Fix the gitx stdin check leaking the stream it opens - #593

Merged
hannesa2 merged 1 commit into
gitx:masterfrom
goneng:fix-stdin-stream-leak
Aug 24, 2026
Merged

hannesa2 merged 1 commit into
gitx:masterfrom
goneng:fix-stdin-stream-leak

Conversation

@goneng

@goneng goneng commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator

The command line tool decides whether to read a piped diff by opening a stream
on stdin and throwing the result away, so every run that is not attached to a terminal
leaks a FILE for the life of the process.
This is the unix.Stream analyzer finding in gitx.m.

  • Test that stdin is open and readable through its descriptor flags
    instead, which answers the same question without allocating anything
  • Keep the guard in place, since handleSTDINDiff reads stdin through
    NSFileHandle and raises when the descriptor is closed

The stream cannot simply be closed: fclose would take stdin with it,
and handleSTDINDiff needs that same descriptor. The guard cannot be
dropped either, because isatty returns 0 for a closed stdin, so it is
the guard that keeps readDataToEndOfFile off a bad descriptor.
The new check matches fdopen(fd, "r") exactly, which fails both on an invalid
descriptor and on a write-only one.

Test plan

  • xcodebuild analyze no longer reports the unix.Stream warning at gitx.m:370,
    with gitx.m confirmed re-analyzed in that run
  • Run from a non-repository directory, gitx gets past the guard and
    prints its usual message under an empty pipe, a closed stdin,
    a /dev/null stdin and a write-only descriptor
  • A pipe held open for three seconds still blocks in handleSTDINDiff
    for the full three, while a closed stdin returns at once, so the
    readable path is still entered rather than skipped

The command line tool decides whether to read a piped diff by opening a
stream on stdin and throwing the result away, so every run that is not
attached to a terminal leaks a FILE for the life of the process.

- Test that stdin is open and readable through its descriptor flags
  instead, which answers the same question without allocating anything
- Keep the guard in place, since handleSTDINDiff reads stdin through
  NSFileHandle and raises when the descriptor is closed
@goneng
goneng marked this pull request as ready for review August 24, 2026 13:30
@hannesa2
hannesa2 merged commit 7d679c9 into gitx:master Aug 24, 2026
3 checks passed
@goneng
goneng deleted the fix-stdin-stream-leak branch August 24, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants