From 993bab898d4aff3a29bc041e6b45488b5a9cfa12 Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Thu, 5 Oct 2017 21:47:11 -0300 Subject: [PATCH] lib, ldpd: fix "argument cannot be negative" coverity warnings Signed-off-by: Renato Westphal --- ldpd/ldpd.c | 13 +++++++++---- lib/libfrr.c | 30 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/ldpd/ldpd.c b/ldpd/ldpd.c index a79e63229f..843d160d62 100644 --- a/ldpd/ldpd.c +++ b/ldpd/ldpd.c @@ -450,10 +450,15 @@ start_child(enum ldpd_process p, char *argv0, int fd_async, int fd_sync) } nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); - dup2(nullfd, 0); - dup2(nullfd, 1); - dup2(nullfd, 2); - close(nullfd); + if (nullfd == -1) { + zlog_err("%s: failed to open /dev/null: %s", __func__, + safe_strerror(errno)); + } else { + dup2(nullfd, 0); + dup2(nullfd, 1); + dup2(nullfd, 2); + close(nullfd); + } if (dup2(fd_async, LDPD_FD_ASYNC) == -1) fatal("cannot setup imsg async fd"); diff --git a/lib/libfrr.c b/lib/libfrr.c index 2859f062c1..6cb8711edf 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -768,6 +768,8 @@ void frr_vty_serv(void) static void frr_terminal_close(int isexit) { + int nullfd; + if (daemon_ctl_sock != -1) { close(daemon_ctl_sock); daemon_ctl_sock = -1; @@ -783,11 +785,16 @@ static void frr_terminal_close(int isexit) fflush(stdout); } - int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); - dup2(nullfd, 0); - dup2(nullfd, 1); - dup2(nullfd, 2); - close(nullfd); + nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); + if (nullfd == -1) { + zlog_err("%s: failed to open /dev/null: %s", __func__, + safe_strerror(errno)); + } else { + dup2(nullfd, 0); + dup2(nullfd, 1); + dup2(nullfd, 2); + close(nullfd); + } } static struct thread *daemon_ctl_thread = NULL; @@ -849,10 +856,15 @@ void frr_run(struct thread_master *master) } } else if (di->daemon_mode) { int nullfd = open("/dev/null", O_RDONLY | O_NOCTTY); - dup2(nullfd, 0); - dup2(nullfd, 1); - dup2(nullfd, 2); - close(nullfd); + if (nullfd == -1) { + zlog_err("%s: failed to open /dev/null: %s", __func__, + safe_strerror(errno)); + } else { + dup2(nullfd, 0); + dup2(nullfd, 1); + dup2(nullfd, 2); + close(nullfd); + } if (daemon_ctl_sock != -1) close(daemon_ctl_sock); -- 2.39.5