From 2c0bf67b7612a11f64e7407a69b51d909a5ae277 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 20 Apr 2021 01:48:58 +0000 Subject: Everyone loves systems that do not define proper limits --- src/shutdown/hpr_confirm_hostname.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/shutdown/hpr_confirm_hostname.c b/src/shutdown/hpr_confirm_hostname.c index 040c2f6..e782c80 100644 --- a/src/shutdown/hpr_confirm_hostname.c +++ b/src/shutdown/hpr_confirm_hostname.c @@ -17,15 +17,17 @@ void hpr_confirm_hostname (void) { - char name[HOST_NAME_MAX + 1] ; - char buf[HOST_NAME_MAX + 1] ; char *p ; ssize_t r ; + long hostnamemax = sysconf(_SC_HOST_NAME_MAX) ; + if (hostnamemax < 0) hostnamemax = _POSIX_HOST_NAME_MAX ; + char name[hostnamemax + 1] ; + char buf[hostnamemax + 1] ; if (!isatty(0) || !isatty(1)) strerr_diefu1x(100, "ask hostname confirmation: stdin or stdout is not a tty") ; - if (gethostname(name, HOST_NAME_MAX) < 0) + if (gethostname(name, hostnamemax) < 0) strerr_diefu1sys(111, "get host name") ; - name[HOST_NAME_MAX] = 0 ; + name[hostnamemax] = 0 ; p = strchr(name, '.') ; if (p) *p = 0 ; if (allwrite(1, PROMPT, sizeof(PROMPT)-1) < 0) @@ -34,7 +36,7 @@ void hpr_confirm_hostname (void) strerr_diefu1sys(111, "tcdrain stdout") ; if (tcflush(0, TCIFLUSH) < 0) strerr_diefu1sys(111, "empty stdin buffer") ; - r = fd_read(0, buf, HOST_NAME_MAX) ; + r = fd_read(0, buf, hostnamemax) ; if (!r) errno = EPIPE ; if (r <= 0) strerr_diefu1sys(111, "read from stdin") ; buf[byte_chr(buf, r, '\n')] = 0 ; -- cgit v1.2.3