]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ether-addr-util: make hw_addr_to_string() return valid string even if hardware addres...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 15 Sep 2021 13:59:52 +0000 (22:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 6 Oct 2021 07:39:47 +0000 (16:39 +0900)
Previously, when the length of the hardware address is zero, then the
buffer was not nul-terminated.

This also replaces sprintf() with hexchar().

src/basic/ether-addr-util.c

index e660ac2c6fd622344f99721946979f6e4af4aa21..dc5b5b833d3d1821b09d1f0f11e4b4f1f0411c54 100644 (file)
@@ -7,6 +7,7 @@
 #include <sys/types.h>
 
 #include "ether-addr-util.h"
+#include "hexdecoct.h"
 #include "macro.h"
 #include "string-util.h"
 
@@ -15,12 +16,13 @@ char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_
         assert(buffer);
         assert(addr->length <= HW_ADDR_MAX_SIZE);
 
-        for (size_t i = 0; i < addr->length; i++) {
-                sprintf(&buffer[3*i], "%02"PRIx8, addr->bytes[i]);
-                if (i < addr->length - 1)
-                        buffer[3*i + 2] = ':';
+        for (size_t i = 0, j = 0; i < addr->length; i++) {
+                buffer[j++] = hexchar(addr->bytes[i] >> 4);
+                buffer[j++] = hexchar(addr->bytes[i] & 0x0f);
+                buffer[j++] = ':';
         }
 
+        buffer[addr->length > 0 ? addr->length * 3 - 1 : 0] = '\0';
         return buffer;
 }