1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-07-31 21:09:01 +02:00

truncate: don't leak a file descriptor with --ref=PIPE

* src/truncate.c (main): For a user who makes the mistake of
using a non-seekable file as a reference for the desired length,
truncate would open that file, attempt to seek to its end, but
upon seek failure would neglect to close the file descriptor.
Close the file descriptor even when lseek fails.
In addition, ignore failure to close that reference FD, since as
long as the lseek succeeds, a close failure doesn't matter.
Coverity spotted the potential FD leak.

Improved-by: Pádraig Brady.
This commit is contained in:
Jim Meyering
2012-08-04 11:02:40 +02:00
parent 4bee223d96
commit cbd1cffa3e
+8 -1
View File
@@ -370,8 +370,15 @@ main (int argc, char **argv)
if (0 <= ref_fd)
{
off_t file_end = lseek (ref_fd, 0, SEEK_END);
if (0 <= file_end && close (ref_fd) == 0)
int saved_errno = errno;
close (ref_fd); /* ignore failure */
if (0 <= file_end)
file_size = file_end;
else
{
/* restore, in case close clobbered it. */
errno = saved_errno;
}
}
}
if (file_size < 0)