From 95cb4a2f3b91e1e26a4f74ba950015833e59deaa Mon Sep 17 00:00:00 2001 From: krw Date: Sun, 29 Dec 2013 14:26:22 +0000 Subject: [PATCH] Fix fd leaks when fd < 0 or flock() fails. Original diff from NetBSD via Loganaden Velvindron out of cppcheck. --- usr.sbin/lpr/lpc/cmds.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/usr.sbin/lpr/lpc/cmds.c b/usr.sbin/lpr/lpc/cmds.c index 3b4a59b8e33..bd73855b824 100644 --- a/usr.sbin/lpr/lpc/cmds.c +++ b/usr.sbin/lpr/lpc/cmds.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmds.c,v 1.25 2013/11/24 21:32:32 deraadt Exp $ */ +/* $OpenBSD: cmds.c,v 1.26 2013/12/29 14:26:22 krw Exp $ */ /* $NetBSD: cmds.c,v 1.12 1997/10/05 15:12:06 mrg Exp $ */ /* @@ -197,6 +197,8 @@ upstat(char *msg) fd = safe_open(statfile, O_WRONLY|O_CREAT|O_NOFOLLOW, 0660); if (fd < 0 || flock(fd, LOCK_EX) < 0) { printf("\tcannot create status file\n"); + if (fd >= 0) + (void)close(fd); /* unlocks as well */ return; } (void)fchown(fd, DEFUID, -1); @@ -593,6 +595,8 @@ putmsg(int argc, char **argv) fd = safe_open(line, O_WRONLY|O_CREAT|O_NOFOLLOW, 0660); if (fd < 0 || flock(fd, LOCK_EX) < 0) { printf("\tcannot create status file\n"); + if (fd >= 0) + (void)close(fd); /* unlocks as well */ PRIV_END; return; } @@ -838,8 +842,9 @@ prstat(void) fd = safe_open(line, O_RDONLY|O_NOFOLLOW, 0); PRIV_END; if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) { - (void)close(fd); /* unlocks as well */ printf("\tprinter idle\n"); + if (fd >= 0) + (void)close(fd); /* unlocks as well */ return; } (void)close(fd); -- 2.20.1