From: Roy Marples Date: Mon, 3 Jul 2023 12:11:05 +0000 (+0100) Subject: common: Allow hwaddr_ntoa to print an empty string X-Git-Tag: v9.5.2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4f6e7d8f2b6cbbf6f0595779c9d3ca95352b86d;p=thirdparty%2Fdhcpcd.git common: Allow hwaddr_ntoa to print an empty string This fixes #218 where we get a zero hardware address length in an ARP packet or a length that overflows the string buffer. --- diff --git a/src/common.c b/src/common.c index bb89100e..8eafe69e 100644 --- a/src/common.c +++ b/src/common.c @@ -46,10 +46,15 @@ hwaddr_ntoa(const void *hwaddr, size_t hwlen, char *buf, size_t buflen) const unsigned char *hp, *ep; char *p; - if (buf == NULL || hwlen == 0) + /* Allow a hwlen of 0 to be an empty string. */ + if (buf == NULL || buflen == 0) { + errno = ENOBUFS; return NULL; + } if (hwlen * 3 > buflen) { + /* We should still terminate the string just in case. */ + buf[0] = '\0'; errno = ENOBUFS; return NULL; } @@ -57,7 +62,6 @@ hwaddr_ntoa(const void *hwaddr, size_t hwlen, char *buf, size_t buflen) hp = hwaddr; ep = hp + hwlen; p = buf; - while (hp < ep) { if (hp != hwaddr) *p ++= ':';