git: 69a3d9ab8019 - stable/15 - touch: Fix setting time of created file if fstat() fails
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 17 Feb 2026 13:17:07 UTC
The branch stable/15 has been updated by obiwac:
URL: https://cgit.FreeBSD.org/src/commit/?id=69a3d9ab8019752608f2597e9f0c7a4b8c21a062
commit 69a3d9ab8019752608f2597e9f0c7a4b8c21a062
Author: Aymeric Wibo <obiwac@FreeBSD.org>
AuthorDate: 2026-02-04 20:58:13 +0000
Commit: Aymeric Wibo <obiwac@FreeBSD.org>
CommitDate: 2026-02-17 13:14:14 +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
(cherry picked from commit b8d55a86995b5a8db5d1651c8dc9fc5093b67d2c)
---
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)