authorgravatar for thekirjastonhoitaja@protonmail.comKirjastonhoitaja <thekirjastonhoitaja@protonmail.com> 2021-09-15 09:51:43+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-09-16 11:35:12+02:00
loge1bf350b4d66a682c8fc5f151563dd1725e8eaf1
tree3f5862ce05476513a87c24708bf08631d5587d8d
parentd5c1d24964b0ee8ad37beb8e2a907e8caa645e07

net.Address: Fix writing 0-bytes when formatting Unix addresses

The entire 'path' array would get written to the formatting function, when it should instead be treated as a regular zero-terminated string. Note that this doesn't handle abstract paths on Linux, those paths *start* with a \0 byte and are hence treated as empty strings instead. But fixing that would require more adjustments than just formatting, in particular to getOsSockLen().

2 files changed, 14 insertions(+), 1 deletions(-)

lib/std/net.zig+1-1
...@@ -157,7 +157,7 @@ pub const Address = extern union {...@@ -157,7 +157,7 @@ pub const Address = extern union {
157 unreachable;157 unreachable;
158 }158 }
159159
160 try std.fmt.format(out_stream, "{s}", .{&self.un.path});160 try std.fmt.format(out_stream, "{s}", .{std.mem.sliceTo(&self.un.path, 0)});
161 },161 },
162 else => unreachable,162 else => unreachable,
163 }163 }
lib/std/net/test.zig+13
...@@ -90,6 +90,19 @@ test "parse and render IPv4 addresses" {...@@ -90,6 +90,19 @@ test "parse and render IPv4 addresses" {
90 try testing.expectError(error.NonCanonical, net.Address.parseIp4("127.01.0.1", 0));90 try testing.expectError(error.NonCanonical, net.Address.parseIp4("127.01.0.1", 0));
91}91}
9292
93test "parse and render UNIX addresses" {
94 if (builtin.os.tag == .wasi) return error.SkipZigTest;
95 if (!net.has_unix_sockets) return error.SkipZigTest;
96
97 var buffer: [14]u8 = undefined;
98 const addr = net.Address.initUnix("/tmp/testpath") catch unreachable;
99 const fmt_addr = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
100 try std.testing.expectEqualSlices(u8, "/tmp/testpath", fmt_addr);
101
102 const too_long = [_]u8{'a'} ** (addr.un.path.len + 1);
103 try testing.expectError(error.NameTooLong, net.Address.initUnix(too_long[0..]));
104}
105
93test "resolve DNS" {106test "resolve DNS" {
94 if (builtin.os.tag == .wasi) return error.SkipZigTest;107 if (builtin.os.tag == .wasi) return error.SkipZigTest;
95108