From b66368b4d6e5e7eb3cc26a97574b09d6bfdbb49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristo=CC=81fer=20R?= Date: Thu, 1 May 2025 00:09:34 -0400 Subject: [PATCH] extract mac address validity check to function --- src/termio/stream_handler.zig | 57 +++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/termio/stream_handler.zig b/src/termio/stream_handler.zig index 6668b17cd..ba04ee6b1 100644 --- a/src/termio/stream_handler.zig +++ b/src/termio/stream_handler.zig @@ -1048,6 +1048,29 @@ pub const StreamHandler = struct { }; } + fn isValidMacAddress(mac_address: []const u8) bool { + // A valid mac address has 6 two-character components with 5 colons, e.g. 12:34:56:ab:cd:ef. + if (mac_address.len != 17) { + return false; + } + + for (0..mac_address.len) |i| { + const c = mac_address[i]; + + if ((i + 1) % 3 == 0) { + if (c != ':') { + return false; + } + } else { + if (!std.mem.containsAtLeastScalar(u8, "0123456789ABCDEFabcdef", 1, c)) { + return false; + } + } + } + + return true; + } + pub fn reportPwd(self: *StreamHandler, url: []const u8) !void { // Special handling for the empty URL. We treat the empty URL // as resetting the pwd as if we never saw a pwd. I can't find any @@ -1084,40 +1107,22 @@ pub const StreamHandler = struct { // Example: file://12:34:56:78:90:12/path/to/file if (e != error.InvalidPort) return; - // Insufficient length to have a mac address in the hostname. - if (url.len < 24) { - log.warn("invalid MAC address in OSC 7: insufficient length", .{}); - return; - } - - // The first '/' after the scheme marks the end of the hostname. If the hostname is - // not 17 characters, it's not a mac address. + // The first '/' after the scheme marks the end of the hostname. If the first '/' + // following the end of the `file://` scheme is not at position 24 this is not a + // valid mac address. if (std.mem.indexOfScalarPos(u8, url, 7, '/') != 24) { - log.warn("invalid MAC address in OSC 7: invalid scheme", .{}); + log.warn("invalid url in OSC 7: {}", .{e}); return; } - // At this point we have a potential mac address as the hostname. + // At this point we may have a mac address as the hostname. const mac_address = url[7..24]; - for (0..mac_address.len) |i| { - const c = mac_address[i]; - - if ((i + 1) % 3 == 0) { - if (c != ':') { - log.warn("invalid MAC address in OSC 7: missing colon", .{}); - return; - } - } else { - if (!std.mem.containsAtLeastScalar(u8, "0123456789ABCDEFabcdef", 1, mac_address[i])) { - log.warn("invalid MAC address in OSC 7: invalid character '{c}' at position '{d}'", .{ mac_address[i], i }); - return; - } - } + if (!isValidMacAddress(mac_address)) { + log.warn("ivalid url in OSC 7: {}", .{e}); + return; } - // At this point we have what looks like a valid mac address. - var uri_path_end_idx: usize = 24; while (uri_path_end_idx < url.len and !isUriPathSeparator(url[uri_path_end_idx])) { uri_path_end_idx += 1;