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
This commit is contained in:
@@ -177,11 +177,19 @@ main(int argc, char *argv[])
|
|||||||
/* Create the file. */
|
/* Create the file. */
|
||||||
fd = open(*argv,
|
fd = open(*argv,
|
||||||
O_WRONLY | O_CREAT, DEFFILEMODE);
|
O_WRONLY | O_CREAT, DEFFILEMODE);
|
||||||
if (fd == -1 || fstat(fd, &sb) || close(fd)) {
|
if (fd == -1) {
|
||||||
rval = 1;
|
rval = 1;
|
||||||
warn("%s", *argv);
|
warn("%s", *argv);
|
||||||
continue;
|
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 using the current time, we're done. */
|
||||||
if (!timeset)
|
if (!timeset)
|
||||||
|
|||||||
Reference in New Issue
Block a user