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.
This commit is contained in:
Kristófer R
2024-11-07 18:17:51 -05:00
parent 7517d0a86b
commit 84707932d2

View File

@ -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");