Page MenuHomeFreeBSD

cp: Avoid closing an invalid file descriptor.
ClosedPublic

Authored by des on May 17 2025, 7:57 AM.
Tags
None
Referenced Files
F142077408: D50391.id155606.diff
Thu, Jan 15, 7:57 PM
Unknown Object (File)
Sun, Jan 4, 1:06 PM
Unknown Object (File)
Dec 11 2025, 5:08 AM
Unknown Object (File)
Nov 30 2025, 6:40 AM
Unknown Object (File)
Nov 25 2025, 7:20 PM
Unknown Object (File)
Nov 15 2025, 9:39 PM
Unknown Object (File)
Nov 3 2025, 10:25 AM
Unknown Object (File)
Oct 29 2025, 5:41 AM
Subscribers

Details

Summary
  • At the end of copy(), we always close to.dir, even though it can be AT_FDCWD (in the file-to-file case) or even -1 (if we failed to open or create the destination directory). While closing an invalid file descriptor is harmless, it's still bad form.
  • In the DIR_TO_DNE case, initialize to.dir to -1 to guard against the case where mkdir() fails so we never assign anything to to.dir and end up (harmlessly, luckily) closing stdin on our way to the exit.

Coverity ID: 1609954
Fixes: 82fc0d09e862
Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

des requested review of this revision.May 17 2025, 7:57 AM
markj added a subscriber: markj.
markj added inline comments.
bin/cp/cp.c
642

I might also explicitly check to.dir != AT_FDCWD rather than assuming that its value is negative instead of, say, INT_MAX.

This revision is now accepted and ready to land.May 17 2025, 1:35 PM
This revision was automatically updated to reflect the committed changes.