diff options
author | 2023-09-01 15:27:59 -0700 | |
---|---|---|
committer | 2023-09-01 15:27:59 -0700 | |
commit | 0fd0ad993bf30dc9af1cbe0459bc49db0dd52137 (patch) | |
tree | 3e34e8d4a57fba37ac19c3a9796ad84f19382f3c | |
parent | 27de8bbf7b29dc142c4d3890ce8533a75d8d920f (diff) | |
download | bun-0fd0ad993bf30dc9af1cbe0459bc49db0dd52137.tar.gz bun-0fd0ad993bf30dc9af1cbe0459bc49db0dd52137.tar.zst bun-0fd0ad993bf30dc9af1cbe0459bc49db0dd52137.zip |
fix(runtime): fix dns_resolver crash (#4435)
* fix incorrect c pointer
* format
* lets go
* random other test case fixed
* hanassagi patch
* Update dns_resolver.zig
* Revert "Update dns_resolver.zig"
This reverts commit 53eb338048583a338e7c01d2b351f1c679db3e15.
* See if the tests pass
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
-rw-r--r-- | src/bun.js/api/bun/dns_resolver.zig | 22 | ||||
-rw-r--r-- | src/deps/c_ares.zig | 8 | ||||
-rw-r--r-- | test/cli/bun.test.ts | 6 |
3 files changed, 19 insertions, 17 deletions
diff --git a/src/bun.js/api/bun/dns_resolver.zig b/src/bun.js/api/bun/dns_resolver.zig index 7364c2ffa..8232318a2 100644 --- a/src/bun.js/api/bun/dns_resolver.zig +++ b/src/bun.js/api/bun/dns_resolver.zig @@ -1497,8 +1497,10 @@ pub const DNSResolver = struct { var pending: ?*CAresReverse = key.lookup.head.next; var prev_global = key.lookup.head.globalThis; + // The callback need not and should not attempt to free the memory + // pointed to by hostent; the ares library will free it when the + // callback returns. var array = addr.toJSReponse(this.vm.allocator, prev_global, ""); - defer addr.deinit(); array.ensureStillAlive(); key.lookup.head.onComplete(array); bun.default_allocator.destroy(key.lookup); @@ -2320,7 +2322,7 @@ pub const DNSResolver = struct { }, }; - var servers: [*c]c_ares.struct_ares_addr_port_node = undefined; + var servers: ?*c_ares.struct_ares_addr_port_node = null; const r = c_ares.ares_get_servers_ports(channel, &servers); if (r != c_ares.ARES_SUCCESS) { const err = c_ares.Error.get(r).?; @@ -2333,21 +2335,21 @@ pub const DNSResolver = struct { var i: u32 = 0; var cur = servers; - while (cur != null) : ({ + while (cur) |current| : ({ i += 1; - cur = cur.*.next; + cur = current.next; }) { // Formatting reference: https://nodejs.org/api/dns.html#dnsgetservers // Brackets '[' and ']' consume 2 bytes, used for IPv6 format (e.g., '[2001:4860:4860::8888]:1053'). // Port range is 6 bytes (e.g., ':65535'). // Null terminator '\0' uses 1 byte. var buf: [INET6_ADDRSTRLEN + 2 + 6 + 1]u8 = undefined; - const family = cur.*.family; + const family = current.family; const ip = if (family == std.os.AF.INET6) blk: { - break :blk c_ares.ares_inet_ntop(family, &cur.*.addr.addr6, buf[1..], @sizeOf(@TypeOf(buf)) - 1); + break :blk c_ares.ares_inet_ntop(family, ¤t.addr.addr6, buf[1..], @sizeOf(@TypeOf(buf)) - 1); } else blk: { - break :blk c_ares.ares_inet_ntop(family, &cur.*.addr.addr4, buf[1..], @sizeOf(@TypeOf(buf)) - 1); + break :blk c_ares.ares_inet_ntop(family, ¤t.addr.addr4, buf[1..], @sizeOf(@TypeOf(buf)) - 1); }; if (ip == null) { globalThis.throwValue(globalThis.createErrorInstance( @@ -2357,15 +2359,15 @@ pub const DNSResolver = struct { return .zero; } - var port = cur.*.tcp_port; + var port = current.tcp_port; if (port == 0) { - port = cur.*.udp_port; + port = current.udp_port; } if (port == 0) { port = IANA_DNS_PORT; } - const size = bun.len(bun.cast([*:0]u8, &buf)); + const size = bun.len(bun.cast([*:0]u8, buf[1..])) + 1; if (port == IANA_DNS_PORT) { values.putIndex(globalThis, i, JSC.ZigString.init(buf[1..size]).withEncoding().toValueGC(globalThis)); } else { diff --git a/src/deps/c_ares.zig b/src/deps/c_ares.zig index b80342996..5098f4e19 100644 --- a/src/deps/c_ares.zig +++ b/src/deps/c_ares.zig @@ -1167,7 +1167,7 @@ const union_unnamed_3 = extern union { addr6: struct_ares_in6_addr, }; pub const struct_ares_addr_node = extern struct { - next: [*c]struct_ares_addr_node, + next: ?*struct_ares_addr_node, family: c_int, addr: union_unnamed_3, }; @@ -1176,7 +1176,7 @@ const union_unnamed_4 = extern union { addr6: struct_ares_in6_addr, }; pub const struct_ares_addr_port_node = extern struct { - next: [*c]struct_ares_addr_port_node, + next: ?*struct_ares_addr_port_node, family: c_int, addr: union_unnamed_4, udp_port: c_int, @@ -1186,8 +1186,8 @@ pub extern fn ares_set_servers(channel: *Channel, servers: [*c]struct_ares_addr_ pub extern fn ares_set_servers_ports(channel: *Channel, servers: [*c]struct_ares_addr_port_node) c_int; pub extern fn ares_set_servers_csv(channel: *Channel, servers: [*c]const u8) c_int; pub extern fn ares_set_servers_ports_csv(channel: *Channel, servers: [*c]const u8) c_int; -pub extern fn ares_get_servers(channel: *Channel, servers: [*c][*c]struct_ares_addr_node) c_int; -pub extern fn ares_get_servers_ports(channel: *Channel, servers: [*c][*c]struct_ares_addr_port_node) c_int; +pub extern fn ares_get_servers(channel: *Channel, servers: *?*struct_ares_addr_port_node) c_int; +pub extern fn ares_get_servers_ports(channel: *Channel, servers: *?*struct_ares_addr_port_node) c_int; pub extern fn ares_inet_ntop(af: c_int, src: ?*const anyopaque, dst: [*c]u8, size: ares_socklen_t) [*c]const u8; pub extern fn ares_inet_pton(af: c_int, src: [*c]const u8, dst: ?*anyopaque) c_int; pub const ARES_SUCCESS = 0; diff --git a/test/cli/bun.test.ts b/test/cli/bun.test.ts index 6deedcec8..5c5715168 100644 --- a/test/cli/bun.test.ts +++ b/test/cli/bun.test.ts @@ -1,6 +1,6 @@ import { describe, test, expect } from "bun:test"; import { spawnSync } from "bun"; -import { bunExe } from "harness"; +import { bunEnv, bunExe } from "harness"; import { tmpdir } from "node:os"; import fs from "node:fs"; @@ -39,14 +39,14 @@ describe("bun", () => { test("revision generates version numbers correctly", () => { var { stdout, exitCode } = Bun.spawnSync({ cmd: [bunExe(), "--version"], - env: {}, + env: bunEnv, stderr: "inherit", }); var version = stdout.toString().trim(); var { stdout, exitCode } = Bun.spawnSync({ cmd: [bunExe(), "--revision"], - env: {}, + env: bunEnv, stderr: "inherit", }); var revision = stdout.toString().trim(); |