]> 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>
Wed, 19 Jul 2023 13:29:50 +0000 (14:29 +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 bb89100ef2cf932dc5553dd240c47df635d265b1..8eafe69e2a68345e63e77e7a977c4f7b6a5ef617 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 ++= ':';