From: Harlan Stenn Date: Sun, 7 May 2023 08:46:52 +0000 (-0500) Subject: Make sure the value returned by refid_str() prints cleanly X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1b95067e6ca3ed35700cf7371fa41ecad4b87ed;p=thirdparty%2Fntp.git Make sure the value returned by refid_str() prints cleanly bk: 6457657cbmOY_RIfIqHA4VWtmCb0iw --- diff --git a/ChangeLog b/ChangeLog index 667c836ec..d58088e17 100644 --- a/ChangeLog +++ b/ChangeLog @@ -89,6 +89,7 @@ * Only define tv_fmt_libbuf() if we will use it. * Use recv_buffer instead of the longer recv_space.X_recv_buffer. hart/stenn * Disable "embedded NUL in string" warnings in libopts, when we can. +* Make sure the value returned by refid_str() prints cleanly. * If DEBUG is enabled, the startup banner now says that debug assertions are in force and that ntpd will abort if any are violated. * syslog valid incoming KoDs. diff --git a/libntp/numtoa.c b/libntp/numtoa.c index 5efbe942e..51645de01 100644 --- a/libntp/numtoa.c +++ b/libntp/numtoa.c @@ -9,6 +9,7 @@ #endif #include +#include #include "ntp_fp.h" #include "lib_strbuf.h" @@ -42,18 +43,33 @@ refid_str( { char * text; size_t tlen; + char * cp; if (stratum > 1) return numtoa(refid); LIB_GETBUF(text); text[0] = '.'; + /* What if any non-NUL char is not printable? */ memcpy(&text[1], &refid, sizeof(refid)); text[1 + sizeof(refid)] = '\0'; tlen = strlen(text); text[tlen] = '.'; text[tlen + 1] = '\0'; + /* + * Now make sure the contents are 'graphic'. + * + * This refid is expected to be up to 4 ascii graphics. + * If any character is not a graphic, replace it with a space. + * This will at least alert the viewer of a problem. + */ + for (cp = text + 1; *cp; ++cp) { + if (!isgraph((int)*cp)) { + *cp = ' '; + } + } + return text; }