Skip to content

programs: don't write the output through a symbolic link - #4906

Merged
Cyan4973 merged 1 commit into
devfrom
cli-dst-no-follow
Nov 9, 2026
Merged

Cyan4973 merged 1 commit into
devfrom
cli-dst-no-follow

Conversation

@Cyan4973

@Cyan4973 Cyan4973 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

zstd opened its output with O_CREAT|O_TRUNC, which follows a symbolic link. stat() fails on a dangling link, so a dangling link at the output path was not seen as an existing file: zstd created its target and wrote the output there, even without -f.

New output files are now created with O_EXCL, as gzip, bzip2 and xz do. O_EXCL fails on any symbolic link, dangling or not, including a link that appears after the existence check or after -f removed an existing file. A dangling link now counts as an existing file: it is not overwritten without -f, and -f replaces the link, as it already did for a link to an existing file.

Outputs that exist and are not regular files (/dev/null, /dev/stdout, /dev/fd/N, pipes) are opened as before, but without O_TRUNC, and they are refused if they turn out to be regular files once opened. A regular file that could not be removed is still truncated in place. Windows is unchanged.

This supersedes #4640, thanks @metsw24-max. Its approach breaks some legitimate outputs: O_NOFOLLOW rejects -o /dev/stdout and -o /dev/fd/N, O_EXCL without -f rejects -o /dev/null and pipes, and truncating in place instead of removing the file writes through hard links.

Tests: a new case in playTests.sh, which fails without the fix. The file-stat traces are updated for the new UTIL_stat() and UTIL_isLink() calls.

The output file was opened with O_CREAT|O_TRUNC, which follows a
symbolic link. stat() fails on a dangling link, so it was not seen as
an existing file: zstd created its target and wrote the output there,
even without -f.

A new output file is now created with O_EXCL, which fails on any
symbolic link, even a dangling one, including a link that appears
after the existence check or after an existing file was removed.
A dangling link now counts as an existing file: it is not overwritten
without -f, and -f replaces the link, as it already did for a link to
an existing file.

Existing files that are not regular files, such as /dev/null,
/dev/stdout or pipes, are still opened as they are. They are no longer
truncated, and they are refused if they turn out to be regular files
once opened. A regular file that could not be removed is still
truncated in place.
@meta-cla meta-cla Bot added the CLA Signed label Nov 9, 2026
@Cyan4973 Cyan4973 self-assigned this Nov 9, 2026
@Cyan4973
Cyan4973 merged commit cc53693 into dev Nov 9, 2026
110 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant