Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 19 Mar 2022 13:17:34 GMT
From:      Mariusz Zaborski <oshogbo@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: e7023af434e2 - stable/13 - touch: don't leak descriptor if fstat(2) fails
Message-ID:  <202203191317.22JDHYFB028153@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by oshogbo:

URL: https://cgit.FreeBSD.org/src/commit/?id=e7023af434e2035d6359ca0b5108a6f3e5caf9c4

commit e7023af434e2035d6359ca0b5108a6f3e5caf9c4
Author:     Mariusz Zaborski <oshogbo@FreeBSD.org>
AuthorDate: 2022-03-12 10:38:12 +0000
Commit:     Mariusz Zaborski <oshogbo@FreeBSD.org>
CommitDate: 2022-03-19 13:16:29 +0000

    touch: don't leak descriptor if fstat(2) fails
    
    If fstat(2) fails the close(2) won't be called, which will leak the
    file descriptor.
    
    The idea was borrowed from OpenBSD, where similar patch
    was applied for futimens(2).
    
    MFC after:      1 week
    
    (cherry picked from commit cb54c500d0e1a2c52270b15c6db6a88ca3feb86a)
---
 usr.bin/touch/touch.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/usr.bin/touch/touch.c b/usr.bin/touch/touch.c
index 62f77d46e429..1594eacb67d0 100644
--- a/usr.bin/touch/touch.c
+++ b/usr.bin/touch/touch.c
@@ -177,11 +177,19 @@ main(int argc, char *argv[])
 				/* Create the file. */
 				fd = open(*argv,
 				    O_WRONLY | O_CREAT, DEFFILEMODE);
-				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
+				if (fd == -1) {
 					rval = 1;
 					warn("%s", *argv);
 					continue;
 				}
+				if (fstat(fd, &sb) < 0) {
+					warn("%s", *argv);
+					rval = 1;
+				}
+				if (close(fd) < 0) {
+					warn("%s", *argv);
+					rval = 1;
+				}
 
 				/* If using the current time, we're done. */
 				if (!timeset)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202203191317.22JDHYFB028153>