From 5939d09f16547ab935d8e3699688ed1817471da5 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 2 Aug 2016 16:45:07 +0200 Subject: llmnrd: Use MAXHOSTNAMELEN for hostname buffer and zero-terminate string Use MAXHOSTNAMELEN defined in sys/param.h to determine the size of the hostname buffer to allocate. Also make sure the string is properly zero-terminated in all cases, as in some cases it might be undefined whether a truncated string is properly terminated. Note: Also remove a TODO comment, as watching for hostname changes at runtime is outside the scope of llmnrd (also, avahi behaves the same). Signed-off-by: Tobias Klauser --- llmnrd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/llmnrd.c b/llmnrd.c index 663d524..48e4030 100644 --- a/llmnrd.c +++ b/llmnrd.c @@ -29,6 +29,7 @@ #include #include +#include #include "compiler.h" #include "log.h" @@ -148,12 +149,12 @@ int main(int argc, char **argv) register_signal(SIGHUP, signal_handler); if (!hostname) { - /* TODO: Consider hostname changing at runtime */ - hostname = xmalloc(255); - if (gethostname(hostname, 255) != 0) { + hostname = xzalloc(MAXHOSTNAMELEN); + if (gethostname(hostname, MAXHOSTNAMELEN) != 0) { log_err("Failed to get hostname"); return EXIT_FAILURE; } + hostname[MAXHOSTNAMELEN - 1] = '\0'; } if (daemonize) { -- cgit v1.2.3-54-g00ecf