Skip to content

Reject copying a file onto itself before truncating the destination #552

Closed

Description

@LunaStev

fs_copy opens the source for reading, then opens the destination with fs_open_write, which uses CREAT|TRUNC. If both paths identify the same file, opening the destination truncates the source before io_copy reads it. The function can then report a successful zero-byte copy while the original contents have been destroyed. Identical path strings are the simplest case; aliases and hard links can identify the same object too.

Code evidence:

Acceptance:

  • Detect same-file identity before any destructive destination operation, including identical paths and supported aliases/hard links.
  • Preserve the original bytes and return an explicit error for self-copy.
  • Use native file identity rather than comparing only strings, lengths or timestamps; keep the check tied to the actual opened objects as far as the APIs allow.
  • Add guarded temporary-file cases for self-copy, aliases and ordinary copying, and cover handle cleanup on errors.

#527 concerns Windows open-flag translation. This is a separate cross-platform data-loss path in the high-level copy operation.

Audit status: static source inspection against canonical master ea74c2dafc31da876e5561f2d176ba719f5a0458 on 2026-09-10. No compiler build, test execution, implementation change or local reproduction was performed for this report. The examples describe the source-derived behavior to verify when implementing the fix.

Activity

  1. added
    bug
    help wanted
    on Sep 10, 2026
  2. added a commit that references this issue on Sep 16, 2026
    451cc7a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions