]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
common: Allow hwaddr_ntoa to print an empty string
authorRoy Marples <roy@marples.name>
Mon, 3 Jul 2023 12:11:05 +0000 (13:11 +0100)
committerRoy Marples <roy@marples.name>
Tue, 4 Jul 2023 07:58:13 +0000 (08:58 +0100)
This fixes #218 where we get a zero hardware address length in
an ARP packet or a length that overflows the string buffer.

src/common.c

index 553825ef7f51ed27078beeca77393e4b5d6ff4e7..34746d2496253e1297e5bc2f6f099a2f7906650b 100644 (file)
@@ -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 ++= ':';