git: b8d55a86995b - main - touch: Fix setting time of created file if fstat() fails
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 05 Feb 2026 11:17:12 UTC
The branch main has been updated by obiwac:
URL: https://cgit.FreeBSD.org/src/commit/?id=b8d55a86995b5a8db5d1651c8dc9fc5093b67d2c
commit b8d55a86995b5a8db5d1651c8dc9fc5093b67d2c
Author: Aymeric Wibo <obiwac@FreeBSD.org>
AuthorDate: 2026-02-04 20:58:13 +0000
Commit: Aymeric Wibo <obiwac@FreeBSD.org>
CommitDate: 2026-02-05 11:16:06 +0000
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
---
usr.bin/touch/touch.c | 13 ++++---------
1 file 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)