From 84707932d27df65198ede1ba4b0b963f17c0f120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristo=CC=81fer=20R?= Date: Thu, 7 Nov 2024 18:17:51 -0500 Subject: [PATCH] os/hostname: fix mac address handling when last section starts with '0' I hit an edge case when using Private Wi-Fi addressing on macOS where the last section of the randomized mac address starts with a '0'. The hostname parsing for the shell integration didn't handle this case, so issue #2512 reappeared. This fixes that by explicitly handling port numbers < 10. --- src/os/hostname.zig | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/os/hostname.zig b/src/os/hostname.zig index 6956ed71f..22f29ceff 100644 --- a/src/os/hostname.zig +++ b/src/os/hostname.zig @@ -36,15 +36,16 @@ pub fn bufPrintHostnameFromFileUri( // it's not a partial MAC-address. const port = uri.port orelse return host; - // If the port is not a 2-digit number we're not looking at a partial + // If the port is not a 1 or 2-digit number we're not looking at a partial // MAC-address, and instead just a regular port so we return the plain // URI hostname. - if (port < 10 or port > 99) return host; + if (port > 99) return host; var fbs = std.io.fixedBufferStream(buf); try std.fmt.format( fbs.writer(), - "{s}:{d}", + // Make sure "port" is always 2 digits, prefixed with a 0 when "port" is a 1-digit number. + "{s}:{d:0>2}", .{ host, port }, ); @@ -85,6 +86,14 @@ test "bufPrintHostnameFromFileUri succeeds with hostname as mac address" { try std.testing.expectEqualStrings("12:34:56:78:90:12", actual); } +test "bufPrintHostnameFromFileUri succeeds with hostname as a mac address and the last section is < 10" { + const uri = try std.Uri.parse("file://12:34:56:78:90:05"); + + var buf: [posix.HOST_NAME_MAX]u8 = undefined; + const actual = try bufPrintHostnameFromFileUri(&buf, uri); + try std.testing.expectEqualStrings("12:34:56:78:90:05", actual); +} + test "bufPrintHostnameFromFileUri returns only hostname when there is a port component in the URI" { // First: try with a non-2-digit port, to test general port handling. const four_port_uri = try std.Uri.parse("file://has-a-port:1234");