From e0d71ef6398d571d170eee18e1daad2ac81040d8 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 14:18:41 +0200 Subject: [PATCH 01/17] removed obsolete ArrayList usage --- lib/std/net.zig | 77 +++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 37fe2734d52d..7054602d06fe 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1591,16 +1591,14 @@ const ResolvConf = struct { ndots: u32, timeout: u32, search: ArrayList(u8), - /// TODO there are actually only allowed to be maximum 3 nameservers, no need - /// for an array list. - ns: ArrayList(LookupAddr), + ns: [3]?LookupAddr, /// Returns `error.StreamTooLong` if a line is longer than 512 bytes. /// TODO: https://github.com/ziglang/zig/issues/2765 and https://github.com/ziglang/zig/issues/2761 fn init(rc: *ResolvConf, gpa: Allocator) !void { rc.* = .{ .gpa = gpa, - .ns = .empty, + .ns = .{ null, null, null }, .search = .empty, .ndots = 1, .timeout = 5, @@ -1612,7 +1610,7 @@ const ResolvConf = struct { error.FileNotFound, error.NotDir, error.AccessDenied, - => return linuxLookupNameFromNumericUnspec(gpa, &rc.ns, "127.0.0.1", 53), + => return linuxLookupNameFromNumericUnspec(&rc.ns, "127.0.0.1", 53), else => |e| return e, }; defer file.close(); @@ -1655,7 +1653,7 @@ const ResolvConf = struct { }, .nameserver => { const ip_txt = line_it.next() orelse continue; - try linuxLookupNameFromNumericUnspec(gpa, &rc.ns, ip_txt, 53); + try linuxLookupNameFromNumericUnspec(&rc.ns, ip_txt, 53); }, .domain, .search => { rc.search.items.len = 0; @@ -1667,8 +1665,8 @@ const ResolvConf = struct { else => |e| return e, } - if (rc.ns.items.len == 0) { - return linuxLookupNameFromNumericUnspec(gpa, &rc.ns, "127.0.0.1", 53); + if (&rc.ns == &[3]?LookupAddr{ null, null, null }) { + return linuxLookupNameFromNumericUnspec(&rc.ns, "127.0.0.1", 53); } } @@ -1678,23 +1676,21 @@ const ResolvConf = struct { answers: [][]u8, answer_bufs: []const []u8, ) !void { - const gpa = rc.gpa; const timeout = 1000 * rc.timeout; const attempts = rc.attempts; var sl: posix.socklen_t = @sizeOf(posix.sockaddr.in); var family: posix.sa_family_t = posix.AF.INET; - var ns_list: ArrayList(Address) = .empty; - defer ns_list.deinit(gpa); - - try ns_list.resize(gpa, rc.ns.items.len); + var ns_arr: [3]?Address = .{ null, null, null }; - for (ns_list.items, rc.ns.items) |*ns, iplit| { - ns.* = iplit.addr; - assert(ns.getPort() == 53); - if (iplit.addr.any.family != posix.AF.INET) { - family = posix.AF.INET6; + for (&ns_arr, rc.ns) |*ns, iplit| { + if (iplit) |ip| { + ns.* = ip.addr; + assert(ns.*.?.getPort() == 53); + if (ip.addr.any.family != posix.AF.INET) { + family = posix.AF.INET6; + } } } @@ -1724,13 +1720,15 @@ const ResolvConf = struct { std.os.linux.IPV6.V6ONLY, &mem.toBytes(@as(c_int, 0)), ); - for (ns_list.items) |*ns| { - if (ns.any.family != posix.AF.INET) continue; - mem.writeInt(u32, ns.in6.sa.addr[12..], ns.in.sa.addr, native_endian); - ns.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; - ns.any.family = posix.AF.INET6; - ns.in6.sa.flowinfo = 0; - ns.in6.sa.scope_id = 0; + for (&ns_arr) |*ns| { + if (ns.*) |*n| { + if (n.*.any.family != posix.AF.INET) continue; + mem.writeInt(u32, n.*.in6.sa.addr[12..], n.*.in.sa.addr, native_endian); + n.*.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; + n.*.any.family = posix.AF.INET6; + n.*.in6.sa.flowinfo = 0; + n.*.in6.sa.scope_id = 0; + } } sl = @sizeOf(posix.sockaddr.in6); } @@ -1760,8 +1758,10 @@ const ResolvConf = struct { var i: usize = 0; while (i < queries.len) : (i += 1) { if (answers[i].len == 0) { - for (ns_list.items) |*ns| { - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; + for (&ns_arr) |*ns| { + if (ns.*) |*n| { + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &n.any, sl) catch undefined; + } } } } @@ -1782,8 +1782,10 @@ const ResolvConf = struct { if (rlen < 4) continue; // Ignore replies from addresses we didn't send to - const ns = for (ns_list.items) |*ns| { - if (ns.eql(sa)) break ns; + const ns = for (ns_arr) |ns| { + if (ns) |n| { + if (n.eql(sa)) break ns; + } } else continue; // Find which query this answer goes with, if any @@ -1802,7 +1804,7 @@ const ResolvConf = struct { 0, 3 => {}, 2 => if (servfail_retry != 0) { servfail_retry -= 1; - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.?.any, sl) catch undefined; }, else => continue, } @@ -1823,20 +1825,25 @@ const ResolvConf = struct { fn deinit(rc: *ResolvConf) void { const gpa = rc.gpa; - rc.ns.deinit(gpa); rc.search.deinit(gpa); rc.* = undefined; } }; fn linuxLookupNameFromNumericUnspec( - gpa: Allocator, - addrs: *ArrayList(LookupAddr), + addrs: *[3]?LookupAddr, name: []const u8, port: u16, ) !void { - const addr = try Address.resolveIp(name, port); - try addrs.append(gpa, .{ .addr = addr }); + const address = try Address.resolveIp(name, port); + for (addrs) |*addr| { + if (addr.* == null) { + addr.* = .{ .addr = address }; + return; + } + } + + return error.OutOfMemory; } fn dnsParse( From d82ea101f677a36a3f47b50aae757bfb3cdbaee4 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 14:52:36 +0200 Subject: [PATCH 02/17] remove unneeded eql check --- lib/std/net.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 7054602d06fe..11d3dc9020d9 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1665,9 +1665,11 @@ const ResolvConf = struct { else => |e| return e, } - if (&rc.ns == &[3]?LookupAddr{ null, null, null }) { - return linuxLookupNameFromNumericUnspec(&rc.ns, "127.0.0.1", 53); + for (rc.ns) |n| { + if (n != null) return; } + + return linuxLookupNameFromNumericUnspec(&rc.ns, "127.0.0.1", 53); } fn resMSendRc( From 3d352c9f8b1c67014230ea178b683993ed5e9339 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 15:04:50 +0200 Subject: [PATCH 03/17] remove uneeded optional --- lib/std/net.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 11d3dc9020d9..87764dbd4650 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1786,7 +1786,7 @@ const ResolvConf = struct { // Ignore replies from addresses we didn't send to const ns = for (ns_arr) |ns| { if (ns) |n| { - if (n.eql(sa)) break ns; + if (n.eql(sa)) break n; } } else continue; @@ -1806,7 +1806,7 @@ const ResolvConf = struct { 0, 3 => {}, 2 => if (servfail_retry != 0) { servfail_retry -= 1; - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.?.any, sl) catch undefined; + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; }, else => continue, } From 0bc3aaaaa88a157b3ca6f13de0599f7d77c1b363 Mon Sep 17 00:00:00 2001 From: usebeforefree Date: Tue, 16 Sep 2025 16:38:38 +0000 Subject: [PATCH 04/17] Update lib/std/net.zig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Андрей Краевский <75577902+AndrewKraevskii@users.noreply.github.com> --- lib/std/net.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 87764dbd4650..55219c8d0170 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1724,12 +1724,12 @@ const ResolvConf = struct { ); for (&ns_arr) |*ns| { if (ns.*) |*n| { - if (n.*.any.family != posix.AF.INET) continue; - mem.writeInt(u32, n.*.in6.sa.addr[12..], n.*.in.sa.addr, native_endian); - n.*.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; - n.*.any.family = posix.AF.INET6; - n.*.in6.sa.flowinfo = 0; - n.*.in6.sa.scope_id = 0; + if (n.any.family != posix.AF.INET) continue; + mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); + n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; + n.any.family = posix.AF.INET6; + n.in6.sa.flowinfo = 0; + n.in6.sa.scope_id = 0; } } sl = @sizeOf(posix.sockaddr.in6); From 9533276caaf3f4ba2c73b3dc249a299038262b18 Mon Sep 17 00:00:00 2001 From: usebeforefree Date: Tue, 16 Sep 2025 16:39:21 +0000 Subject: [PATCH 05/17] Update lib/std/net.zig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Андрей Краевский <75577902+AndrewKraevskii@users.noreply.github.com> --- lib/std/net.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 55219c8d0170..a8b345356e9a 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1595,7 +1595,7 @@ const ResolvConf = struct { /// Returns `error.StreamTooLong` if a line is longer than 512 bytes. /// TODO: https://github.com/ziglang/zig/issues/2765 and https://github.com/ziglang/zig/issues/2761 - fn init(rc: *ResolvConf, gpa: Allocator) !void { + fn init(rc: *ResolvConf) !void { rc.* = .{ .gpa = gpa, .ns = .{ null, null, null }, From c0e07045f9c6f036cc141d8e875b101cc94b6e80 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 18:51:46 +0200 Subject: [PATCH 06/17] move gpa out of ResolvConf struct --- lib/std/net.zig | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index a8b345356e9a..d80257bd03b7 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1483,7 +1483,7 @@ fn linuxLookupNameFromDnsSearch( ) !void { var rc: ResolvConf = undefined; rc.init(gpa) catch return error.ResolveConfParseFailed; - defer rc.deinit(); + defer rc.deinit(gpa); // Count dots, suppress search when >=ndots or name ends in // a dot, which is an explicit request for global scope. @@ -1586,7 +1586,6 @@ fn linuxLookupNameFromDns( } const ResolvConf = struct { - gpa: Allocator, attempts: u32, ndots: u32, timeout: u32, @@ -1595,16 +1594,15 @@ const ResolvConf = struct { /// Returns `error.StreamTooLong` if a line is longer than 512 bytes. /// TODO: https://github.com/ziglang/zig/issues/2765 and https://github.com/ziglang/zig/issues/2761 - fn init(rc: *ResolvConf) !void { + fn init(rc: *ResolvConf, gpa: Allocator) !void { rc.* = .{ - .gpa = gpa, .ns = .{ null, null, null }, .search = .empty, .ndots = 1, .timeout = 5, .attempts = 2, }; - errdefer rc.deinit(); + errdefer rc.deinit(gpa); const file = fs.openFileAbsoluteZ("/etc/resolv.conf", .{}) catch |err| switch (err) { error.FileNotFound, @@ -1617,7 +1615,7 @@ const ResolvConf = struct { var line_buf: [512]u8 = undefined; var file_reader = file.reader(&line_buf); - return parse(rc, &file_reader.interface) catch |err| switch (err) { + return parse(rc, gpa, &file_reader.interface) catch |err| switch (err) { error.ReadFailed => return file_reader.err.?, else => |e| return e, }; @@ -1626,8 +1624,7 @@ const ResolvConf = struct { const Directive = enum { options, nameserver, domain, search }; const Option = enum { ndots, attempts, timeout }; - fn parse(rc: *ResolvConf, reader: *Io.Reader) !void { - const gpa = rc.gpa; + fn parse(rc: *ResolvConf, gpa: Allocator, reader: *Io.Reader) !void { while (reader.takeSentinel('\n')) |line_with_comment| { const line = line: { var split = mem.splitScalar(u8, line_with_comment, '#'); @@ -1825,8 +1822,7 @@ const ResolvConf = struct { } } - fn deinit(rc: *ResolvConf) void { - const gpa = rc.gpa; + fn deinit(rc: *ResolvConf, gpa: Allocator) void { rc.search.deinit(gpa); rc.* = undefined; } From feea90e225afacfc9df0c0d7ed5cab90b281b758 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 20:59:08 +0200 Subject: [PATCH 07/17] make ns a buffer --- lib/std/net.zig | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index d80257bd03b7..28066e4728fc 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1590,13 +1590,15 @@ const ResolvConf = struct { ndots: u32, timeout: u32, search: ArrayList(u8), - ns: [3]?LookupAddr, + ns_buffer: [3]LookupAddr, + ns_len: u2, /// Returns `error.StreamTooLong` if a line is longer than 512 bytes. /// TODO: https://github.com/ziglang/zig/issues/2765 and https://github.com/ziglang/zig/issues/2761 fn init(rc: *ResolvConf, gpa: Allocator) !void { rc.* = .{ - .ns = .{ null, null, null }, + .ns_buffer = undefined, + .ns_len = 0, .search = .empty, .ndots = 1, .timeout = 5, @@ -1608,7 +1610,7 @@ const ResolvConf = struct { error.FileNotFound, error.NotDir, error.AccessDenied, - => return linuxLookupNameFromNumericUnspec(&rc.ns, "127.0.0.1", 53), + => return linuxLookupNameFromNumericUnspec(&rc.ns_buffer, &rc.ns_len, "127.0.0.1", 53), else => |e| return e, }; defer file.close(); @@ -1650,7 +1652,7 @@ const ResolvConf = struct { }, .nameserver => { const ip_txt = line_it.next() orelse continue; - try linuxLookupNameFromNumericUnspec(&rc.ns, ip_txt, 53); + try linuxLookupNameFromNumericUnspec(&rc.ns_buffer, &rc.ns_len, ip_txt, 53); }, .domain, .search => { rc.search.items.len = 0; @@ -1662,11 +1664,9 @@ const ResolvConf = struct { else => |e| return e, } - for (rc.ns) |n| { - if (n != null) return; + if (rc.ns_len == 0) { + return linuxLookupNameFromNumericUnspec(&rc.ns_buffer, &rc.ns_len, "127.0.0.1", 53); } - - return linuxLookupNameFromNumericUnspec(&rc.ns, "127.0.0.1", 53); } fn resMSendRc( @@ -1681,15 +1681,14 @@ const ResolvConf = struct { var sl: posix.socklen_t = @sizeOf(posix.sockaddr.in); var family: posix.sa_family_t = posix.AF.INET; - var ns_arr: [3]?Address = .{ null, null, null }; + var ns_arr: [3]Address = .{ null, null, null }; - for (&ns_arr, rc.ns) |*ns, iplit| { - if (iplit) |ip| { - ns.* = ip.addr; - assert(ns.*.?.getPort() == 53); - if (ip.addr.any.family != posix.AF.INET) { - family = posix.AF.INET6; - } + for (0..rc.ns_len) |index| { + const ip = rc.ns_buffer[index]; + ns_arr[index] = ip.addr; + assert(ns_arr[index].?.getPort() == 53); + if (ip.addr.any.family != posix.AF.INET) { + family = posix.AF.INET6; } } @@ -1829,19 +1828,16 @@ const ResolvConf = struct { }; fn linuxLookupNameFromNumericUnspec( - addrs: *[3]?LookupAddr, + addrs: *[3]LookupAddr, + addrs_len: *u2, name: []const u8, port: u16, ) !void { - const address = try Address.resolveIp(name, port); - for (addrs) |*addr| { - if (addr.* == null) { - addr.* = .{ .addr = address }; - return; - } - } + if (addrs_len.* >= addrs.len - 1) return error.OutOfMemory; - return error.OutOfMemory; + const address = try Address.resolveIp(name, port); + addrs[addrs_len.*] = .{ .addr = address }; + addrs_len.* += 1; } fn dnsParse( From f6eff776c647f7ddb3b8c55e936ec5387646815b Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 21:32:47 +0200 Subject: [PATCH 08/17] made ns_arr a buffer --- lib/std/net.zig | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 28066e4728fc..634eb5d177b8 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1681,12 +1681,12 @@ const ResolvConf = struct { var sl: posix.socklen_t = @sizeOf(posix.sockaddr.in); var family: posix.sa_family_t = posix.AF.INET; - var ns_arr: [3]Address = .{ null, null, null }; + var ns_buffer: [3]Address = undefined; for (0..rc.ns_len) |index| { const ip = rc.ns_buffer[index]; - ns_arr[index] = ip.addr; - assert(ns_arr[index].?.getPort() == 53); + ns_buffer[index] = ip.addr; + assert(ns_buffer[index].getPort() == 53); if (ip.addr.any.family != posix.AF.INET) { family = posix.AF.INET6; } @@ -1718,15 +1718,14 @@ const ResolvConf = struct { std.os.linux.IPV6.V6ONLY, &mem.toBytes(@as(c_int, 0)), ); - for (&ns_arr) |*ns| { - if (ns.*) |*n| { - if (n.any.family != posix.AF.INET) continue; - mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); - n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; - n.any.family = posix.AF.INET6; - n.in6.sa.flowinfo = 0; - n.in6.sa.scope_id = 0; - } + for (0..rc.ns_len) |index| { + var n = ns_buffer[index]; + if (n.any.family != posix.AF.INET) continue; + mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); + n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; + n.any.family = posix.AF.INET6; + n.in6.sa.flowinfo = 0; + n.in6.sa.scope_id = 0; } sl = @sizeOf(posix.sockaddr.in6); } @@ -1756,10 +1755,9 @@ const ResolvConf = struct { var i: usize = 0; while (i < queries.len) : (i += 1) { if (answers[i].len == 0) { - for (&ns_arr) |*ns| { - if (ns.*) |*n| { - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &n.any, sl) catch undefined; - } + for (0..rc.ns_len) |index| { + const n = ns_buffer[index]; + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &n.any, sl) catch undefined; } } } @@ -1780,10 +1778,9 @@ const ResolvConf = struct { if (rlen < 4) continue; // Ignore replies from addresses we didn't send to - const ns = for (ns_arr) |ns| { - if (ns) |n| { - if (n.eql(sa)) break n; - } + const ns = for (0..rc.ns_len) |index| { + const n = ns_buffer[index]; + if (n.eql(sa)) break n; } else continue; // Find which query this answer goes with, if any From 8f73c15fbccee661dc982d791acc70ace2e38548 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 21:37:26 +0200 Subject: [PATCH 09/17] handle index out of bounds --- lib/std/net.zig | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 634eb5d177b8..ca7cc5f59705 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1683,7 +1683,8 @@ const ResolvConf = struct { var ns_buffer: [3]Address = undefined; - for (0..rc.ns_len) |index| { + var index: u2 = 0; + while (index < rc.ns_len) : (index += 1) { const ip = rc.ns_buffer[index]; ns_buffer[index] = ip.addr; assert(ns_buffer[index].getPort() == 53); @@ -1718,8 +1719,9 @@ const ResolvConf = struct { std.os.linux.IPV6.V6ONLY, &mem.toBytes(@as(c_int, 0)), ); - for (0..rc.ns_len) |index| { - var n = ns_buffer[index]; + var i: u2 = 0; + while (i < rc.ns_len) : (i += 1) { + var n = ns_buffer[i]; if (n.any.family != posix.AF.INET) continue; mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; @@ -1755,8 +1757,9 @@ const ResolvConf = struct { var i: usize = 0; while (i < queries.len) : (i += 1) { if (answers[i].len == 0) { - for (0..rc.ns_len) |index| { - const n = ns_buffer[index]; + var idx: u2 = 0; + while (idx < rc.ns_len) : (idx += 1) { + const n = ns_buffer[idx]; _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &n.any, sl) catch undefined; } } @@ -1778,8 +1781,9 @@ const ResolvConf = struct { if (rlen < 4) continue; // Ignore replies from addresses we didn't send to - const ns = for (0..rc.ns_len) |index| { - const n = ns_buffer[index]; + var idx: u2 = 0; + const ns = while (idx < rc.ns_len) : (idx += 1) { + const n = ns_buffer[idx]; if (n.eql(sa)) break n; } else continue; From 85e99e832fe3d2341b09bba41f22048778c30e29 Mon Sep 17 00:00:00 2001 From: marko Date: Tue, 16 Sep 2025 21:44:53 +0200 Subject: [PATCH 10/17] named ns_buffer to ns_addr_buffer --- lib/std/net.zig | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index ca7cc5f59705..d435314a0ed1 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1681,13 +1681,13 @@ const ResolvConf = struct { var sl: posix.socklen_t = @sizeOf(posix.sockaddr.in); var family: posix.sa_family_t = posix.AF.INET; - var ns_buffer: [3]Address = undefined; + var ns_addr_buffer: [3]Address = undefined; var index: u2 = 0; while (index < rc.ns_len) : (index += 1) { const ip = rc.ns_buffer[index]; - ns_buffer[index] = ip.addr; - assert(ns_buffer[index].getPort() == 53); + ns_addr_buffer[index] = ip.addr; + assert(ns_addr_buffer[index].getPort() == 53); if (ip.addr.any.family != posix.AF.INET) { family = posix.AF.INET6; } @@ -1721,7 +1721,7 @@ const ResolvConf = struct { ); var i: u2 = 0; while (i < rc.ns_len) : (i += 1) { - var n = ns_buffer[i]; + var n = ns_addr_buffer[i]; if (n.any.family != posix.AF.INET) continue; mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; @@ -1759,7 +1759,7 @@ const ResolvConf = struct { if (answers[i].len == 0) { var idx: u2 = 0; while (idx < rc.ns_len) : (idx += 1) { - const n = ns_buffer[idx]; + const n = ns_addr_buffer[idx]; _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &n.any, sl) catch undefined; } } @@ -1782,8 +1782,8 @@ const ResolvConf = struct { // Ignore replies from addresses we didn't send to var idx: u2 = 0; - const ns = while (idx < rc.ns_len) : (idx += 1) { - const n = ns_buffer[idx]; + const ns_addr = while (idx < rc.ns_len) : (idx += 1) { + const n = ns_addr_buffer[idx]; if (n.eql(sa)) break n; } else continue; @@ -1803,7 +1803,7 @@ const ResolvConf = struct { 0, 3 => {}, 2 => if (servfail_retry != 0) { servfail_retry -= 1; - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns_addr.any, sl) catch undefined; }, else => continue, } From c0ff44a7af65088e493102b4d76d2ac2611242dd Mon Sep 17 00:00:00 2001 From: usebeforefree Date: Wed, 17 Sep 2025 10:30:13 +0000 Subject: [PATCH 11/17] Update lib/std/net.zig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Андрей Краевский <75577902+AndrewKraevskii@users.noreply.github.com> --- lib/std/net.zig | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index d435314a0ed1..4ebcfc45ddf2 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1683,11 +1683,9 @@ const ResolvConf = struct { var ns_addr_buffer: [3]Address = undefined; - var index: u2 = 0; - while (index < rc.ns_len) : (index += 1) { - const ip = rc.ns_buffer[index]; - ns_addr_buffer[index] = ip.addr; - assert(ns_addr_buffer[index].getPort() == 53); + for (rc.ns_buffer[0..rc.ns_len], ns_addr_buffer[0..rc.ns_len]) |ip, *addr| + addr.* = ip.addr; + assert(addr.getPort() == 53); if (ip.addr.any.family != posix.AF.INET) { family = posix.AF.INET6; } From d9d06adeadbe26b052096ac436ad409e8f54acbc Mon Sep 17 00:00:00 2001 From: usebeforefree Date: Wed, 17 Sep 2025 10:30:31 +0000 Subject: [PATCH 12/17] Update lib/std/net.zig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Андрей Краевский <75577902+AndrewKraevskii@users.noreply.github.com> --- lib/std/net.zig | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 4ebcfc45ddf2..6ed1197b8590 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1717,9 +1717,7 @@ const ResolvConf = struct { std.os.linux.IPV6.V6ONLY, &mem.toBytes(@as(c_int, 0)), ); - var i: u2 = 0; - while (i < rc.ns_len) : (i += 1) { - var n = ns_addr_buffer[i]; + for (ns_addr_buffer[0..rc.ns_len]) |*n| { if (n.any.family != posix.AF.INET) continue; mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; From 08c887af67a772f748ff13aa72ee556414c69a95 Mon Sep 17 00:00:00 2001 From: usebeforefree Date: Wed, 17 Sep 2025 10:30:50 +0000 Subject: [PATCH 13/17] Update lib/std/net.zig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Андрей Краевский <75577902+AndrewKraevskii@users.noreply.github.com> --- lib/std/net.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 6ed1197b8590..baa29825821e 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1830,7 +1830,7 @@ fn linuxLookupNameFromNumericUnspec( name: []const u8, port: u16, ) !void { - if (addrs_len.* >= addrs.len - 1) return error.OutOfMemory; + if (addrs_len.* == addrs.len) return error.OutOfMemory; const address = try Address.resolveIp(name, port); addrs[addrs_len.*] = .{ .addr = address }; From 8ace2ad67acb2ae4403da4b5c798b0d0f9ae3cfd Mon Sep 17 00:00:00 2001 From: marko Date: Wed, 17 Sep 2025 12:34:18 +0200 Subject: [PATCH 14/17] add missing bracket --- lib/std/net.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index baa29825821e..25101bc73f85 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1683,7 +1683,7 @@ const ResolvConf = struct { var ns_addr_buffer: [3]Address = undefined; - for (rc.ns_buffer[0..rc.ns_len], ns_addr_buffer[0..rc.ns_len]) |ip, *addr| + for (rc.ns_buffer[0..rc.ns_len], ns_addr_buffer[0..rc.ns_len]) |ip, *addr| { addr.* = ip.addr; assert(addr.getPort() == 53); if (ip.addr.any.family != posix.AF.INET) { From 9828bdc92484ece97899b69ef8a651887361ec3c Mon Sep 17 00:00:00 2001 From: marko Date: Wed, 17 Sep 2025 13:57:02 +0200 Subject: [PATCH 15/17] simplified loops --- lib/std/net.zig | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 25101bc73f85..9c469051b57b 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1717,13 +1717,13 @@ const ResolvConf = struct { std.os.linux.IPV6.V6ONLY, &mem.toBytes(@as(c_int, 0)), ); - for (ns_addr_buffer[0..rc.ns_len]) |*n| { - if (n.any.family != posix.AF.INET) continue; - mem.writeInt(u32, n.in6.sa.addr[12..], n.in.sa.addr, native_endian); - n.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; - n.any.family = posix.AF.INET6; - n.in6.sa.flowinfo = 0; - n.in6.sa.scope_id = 0; + for (ns_addr_buffer[0..rc.ns_len]) |*ns| { + if (ns.any.family != posix.AF.INET) continue; + mem.writeInt(u32, ns.in6.sa.addr[12..], ns.in.sa.addr, native_endian); + ns.in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; + ns.any.family = posix.AF.INET6; + ns.in6.sa.flowinfo = 0; + ns.in6.sa.scope_id = 0; } sl = @sizeOf(posix.sockaddr.in6); } @@ -1753,10 +1753,8 @@ const ResolvConf = struct { var i: usize = 0; while (i < queries.len) : (i += 1) { if (answers[i].len == 0) { - var idx: u2 = 0; - while (idx < rc.ns_len) : (idx += 1) { - const n = ns_addr_buffer[idx]; - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &n.any, sl) catch undefined; + for (ns_addr_buffer[0..rc.ns_len]) |*ns| { + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; } } } @@ -1777,10 +1775,8 @@ const ResolvConf = struct { if (rlen < 4) continue; // Ignore replies from addresses we didn't send to - var idx: u2 = 0; - const ns_addr = while (idx < rc.ns_len) : (idx += 1) { - const n = ns_addr_buffer[idx]; - if (n.eql(sa)) break n; + const ns_addr = for (ns_addr_buffer[0..rc.ns_len]) |*ns| { + if (ns.eql(sa)) break ns; } else continue; // Find which query this answer goes with, if any From fd06cfd061245d77da32e43e331d4d71f5c8fc93 Mon Sep 17 00:00:00 2001 From: marko Date: Wed, 17 Sep 2025 14:04:23 +0200 Subject: [PATCH 16/17] removed pointless renames --- lib/std/net.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 9c469051b57b..6cffb36fa3d3 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1683,10 +1683,10 @@ const ResolvConf = struct { var ns_addr_buffer: [3]Address = undefined; - for (rc.ns_buffer[0..rc.ns_len], ns_addr_buffer[0..rc.ns_len]) |ip, *addr| { - addr.* = ip.addr; - assert(addr.getPort() == 53); - if (ip.addr.any.family != posix.AF.INET) { + for (ns_addr_buffer[0..rc.ns_len], rc.ns_buffer[0..rc.ns_len]) |*ns, iplit| { + ns.* = iplit.addr; + assert(ns.getPort() == 53); + if (iplit.addr.any.family != posix.AF.INET) { family = posix.AF.INET6; } } @@ -1753,7 +1753,7 @@ const ResolvConf = struct { var i: usize = 0; while (i < queries.len) : (i += 1) { if (answers[i].len == 0) { - for (ns_addr_buffer[0..rc.ns_len]) |*ns| { + for (ns_addr_buffer[0..rc.ns_len]) |*ns| { _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; } } @@ -1775,7 +1775,7 @@ const ResolvConf = struct { if (rlen < 4) continue; // Ignore replies from addresses we didn't send to - const ns_addr = for (ns_addr_buffer[0..rc.ns_len]) |*ns| { + const ns = for (ns_addr_buffer[0..rc.ns_len]) |*ns| { if (ns.eql(sa)) break ns; } else continue; @@ -1795,7 +1795,7 @@ const ResolvConf = struct { 0, 3 => {}, 2 => if (servfail_retry != 0) { servfail_retry -= 1; - _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns_addr.any, sl) catch undefined; + _ = posix.sendto(fd, queries[i], posix.MSG.NOSIGNAL, &ns.any, sl) catch undefined; }, else => continue, } From 20ba56b64e636e7fe6278a165545b4c1a05da237 Mon Sep 17 00:00:00 2001 From: marko Date: Wed, 17 Sep 2025 18:27:23 +0200 Subject: [PATCH 17/17] ignore entries past the third one --- lib/std/net.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 6cffb36fa3d3..5426d768ac71 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1826,7 +1826,8 @@ fn linuxLookupNameFromNumericUnspec( name: []const u8, port: u16, ) !void { - if (addrs_len.* == addrs.len) return error.OutOfMemory; + // Ignore new nameserver entries past the third one + if (addrs_len.* == addrs.len) return; const address = try Address.resolveIp(name, port); addrs[addrs_len.*] = .{ .addr = address };