From eddb17b89c5d4cfc49b22a202a1a7966eab146d8 Mon Sep 17 00:00:00 2001 From: Mike Gilbert Date: Tue, 21 May 2024 14:33:45 -0400 Subject: [PATCH] openrc-shutdown: broadcast: avoid double close Initialize fd and tp to known invalid values, and reset them after calling close/fclose. Bug: https://bugs.gentoo.org/923326 Signed-off-by: Mike Gilbert --- src/openrc-shutdown/broadcast.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/openrc-shutdown/broadcast.c b/src/openrc-shutdown/broadcast.c index f21ee95c4..47af9a8d1 100644 --- a/src/openrc-shutdown/broadcast.c +++ b/src/openrc-shutdown/broadcast.c @@ -131,8 +131,8 @@ void broadcast(char *text) * These are set across the sigsetjmp call, so they can't be stored on * the stack, otherwise they might be clobbered. */ - static int fd; - static FILE *tp; + static int fd = -1; + static FILE *tp = NULL; getuidtty(&user, &tty); @@ -193,10 +193,15 @@ void broadcast(char *text) } } alarm(0); - if (fd >= 0) - close(fd); - if (tp != NULL) + if (tp != NULL) { fclose(tp); + tp = NULL; + fd = -1; /* fclose will call close for us */ + } + if (fd >= 0) { + close(fd); + fd = -1; + } free(term); } endutxent();