diff options
| author | Aymeric Wibo <obiwac@FreeBSD.org> | 2026-02-04 20:58:13 +0000 |
|---|---|---|
| committer | Aymeric Wibo <obiwac@FreeBSD.org> | 2026-02-05 11:16:06 +0000 |
| commit | b8d55a86995b5a8db5d1651c8dc9fc5093b67d2c (patch) | |
| tree | 3b1d0ad93a64e14bd4d4150ada5ad40e866ea3e2 | |
| parent | cc702c78f70f972cf2f8ea008752d96df1989988 (diff) | |
touch: Fix setting time of created file if fstat() fails
Previously, if creating the file and fstat() fails, we would've ended up
calling utimensat() on that file anyways with whatever was in sb. Not
that this is an error likely to happen...
We don't check for the return value of close() as we aren't writing
anything to the file and the file is always created on success of
open().
Reviewed by: kevans
Approved by: kevans
Fixes: cb54c500d0e1 ("touch: don't leak descriptor if fstat(2) fails")
Sponsored by: Klara, Inc.
Differential Revision: https://reviews.freebsd.org/D55117
MFC after: 1 week
| -rw-r--r-- | usr.bin/touch/touch.c | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/usr.bin/touch/touch.c b/usr.bin/touch/touch.c index 70257e320a60..2be2e369596c 100644 --- a/usr.bin/touch/touch.c +++ b/usr.bin/touch/touch.c @@ -163,19 +163,14 @@ main(int argc, char *argv[]) /* Create the file. */ fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE); - if (fd == -1) { + if (fd < 0 || fstat(fd, &sb) < 0) { rval = 1; warn("%s", *argv); + if (fd >= 0) + (void)close(fd); continue; } - if (fstat(fd, &sb) < 0) { - warn("%s", *argv); - rval = 1; - } - if (close(fd) < 0) { - warn("%s", *argv); - rval = 1; - } + (void)close(fd); /* If using the current time, we're done. */ if (!timeset) |
