From 270886c826be37bdb629d55e88729278b424e936 Mon Sep 17 00:00:00 2001 From: pfg Date: Mon, 25 Jul 2016 15:05:01 +0000 Subject: [PATCH] MFC r302911: mail(1): Avoid closing negative file descriptors. CID: 1008105, 1008106 git-svn-id: svn://svn.freebsd.org/base/stable/9@303305 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- usr.bin/mail/quit.c | 3 ++- usr.bin/mail/v7.local.c | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/usr.bin/mail/quit.c b/usr.bin/mail/quit.c index a9607550c..4b9bcda3a 100644 --- a/usr.bin/mail/quit.c +++ b/usr.bin/mail/quit.c @@ -228,7 +228,8 @@ quit(void) return; } (void)Fclose(obuf); - (void)close(open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)); + if ((fd = open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)) >= 0) + (void)close(fd); if ((obuf = Fopen(mbox, "r+")) == NULL) { warn("%s", mbox); (void)Fclose(ibuf); diff --git a/usr.bin/mail/v7.local.c b/usr.bin/mail/v7.local.c index 8fc2f64cc..0da7b3b30 100644 --- a/usr.bin/mail/v7.local.c +++ b/usr.bin/mail/v7.local.c @@ -68,9 +68,12 @@ findmail(char *user, char *buf, int buflen) void demail(void) { + int fd; if (value("keep") != NULL || rm(mailname) < 0) - (void)close(open(mailname, O_CREAT | O_TRUNC | O_WRONLY, 0600)); + if ((fd = open(mailname, O_CREAT | O_TRUNC | O_WRONLY, 0600)) >= + 0) + (void)close(fd); } /* -- 2.45.0