Skip to content

[BUG] stream_cleanup_all() maps slot index to wrong fd → stdout never restored #61

Description

@lucocozz

Summary

stream_cleanup_all() maps the loop slot index to the wrong file descriptor, so an active redirection on stdout is never restored and its backup fd leaks.

Location

source/utils/stream.c:188stream_cleanup_all().

void stream_cleanup_all(void)
{
    for (int i = 0; i < MAX_STREAMS; i++) {
        stream_info_t *info = &streams[i];
        if (info->is_active) {
            int fd = (i == 0) ? STDOUT_FILENO : STDERR_FILENO;  // <-- wrong mapping
            stream_restore(fd);
        }
    }
}

Mechanism

The streams[] table is indexed by fdis_valid_fd() accepts 0/1/2 and stream_redirect() uses &streams[fd]. So slot i is fd i. But cleanup remaps i == 0 → STDOUT_FILENO (1), else → STDERR_FILENO (2):

  • stdout redirected → active slot is i == 1 → calls stream_restore(STDERR_FILENO=2) → inspects streams[2] (inactive) → returns without doing anything. stdout is never restored; backup_fd leaks.
  • i == 0 (stdin slot) would restore STDOUT — also wrong.

Suggested fix

The index is already the fd:

if (info->is_active)
    stream_restore(i);

Severity

Medium/Low — confined to the stdout/stderr capture utility (redirect_stdout() / redirect_stderr()), but leaves descriptors redirected and leaked on the cleanup path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions