1
0
mirror of https://github.com/openbsd/src.git synced 2024-12-21 23:18:00 -08:00

touch(1): don't leak file descriptor if futimens(2) fails

This conditional chain short-circuits if futimens(2) fails, leaving the
file descriptor open.  We need to evaluate each system call in the chain
separately to ensure we attempt to close(2) the descriptor.

With input from guenther@ and millert@.

Thread: https://marc.info/?l=openbsd-tech&m=164332809900558&w=2

ok millert@, probably ok guenther@
This commit is contained in:
cheloha 2022-01-29 00:06:26 +00:00
parent 2987d5a7a5
commit b873219f06

View File

@ -1,4 +1,4 @@
/* $OpenBSD: touch.c,v 1.26 2019/03/10 15:11:52 schwarze Exp $ */
/* $OpenBSD: touch.c,v 1.27 2022/01/29 00:06:26 cheloha Exp $ */
/* $NetBSD: touch.c,v 1.11 1995/08/31 22:10:06 jtc Exp $ */
/*
@ -137,9 +137,18 @@ main(int argc, char *argv[])
/* Create the file. */
fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE);
if (fd == -1 || futimens(fd, ts) || close(fd)) {
if (fd == -1) {
rval = 1;
warn("%s", *argv);
continue;
}
if (futimens(fd, ts) == -1) {
warn("%s", *argv);
rval = 1;
}
if (close(fd) == -1) {
warn("%s", *argv);
rval = 1;
}
}
return rval;