aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-01-27 19:38:40 -0800
committerGravatar GitHub <noreply@github.com> 2023-01-27 19:38:40 -0800
commitea55ef504b119a7ccf337cfdfa51a04881b2d655 (patch)
tree97a5c2fd2cd40e157495ca3389e949e64a93d934 /src
parent34b643e654c8f87456924b2e63492c63006d1ae4 (diff)
downloadbun-ea55ef504b119a7ccf337cfdfa51a04881b2d655.tar.gz
bun-ea55ef504b119a7ccf337cfdfa51a04881b2d655.tar.zst
bun-ea55ef504b119a7ccf337cfdfa51a04881b2d655.zip
ensure name is allocated with `toSliceClone` (#1917)
* ensure name is allocated with `toSliceClone` * shorten toSliceClone
Diffstat (limited to 'src')
-rw-r--r--src/bun.js/api/bun/dns_resolver.zig71
-rw-r--r--src/bun.js/bindings/bindings.zig30
2 files changed, 60 insertions, 41 deletions
diff --git a/src/bun.js/api/bun/dns_resolver.zig b/src/bun.js/api/bun/dns_resolver.zig
index 08202a09e..ef1c6bc4a 100644
--- a/src/bun.js/api/bun/dns_resolver.zig
+++ b/src/bun.js/api/bun/dns_resolver.zig
@@ -629,11 +629,10 @@ pub fn ResolveInfoRequest(comptime cares_type: type, comptime type_name: []const
pub fn init(
cache: DNSResolver.LookupCacheHit(@This()),
resolver: ?*DNSResolver,
- name_str: *const JSC.ZigString.Slice,
+ name: []const u8,
globalThis: *JSC.JSGlobalObject,
comptime cache_field: []const u8,
) !*@This() {
- const name = name_str.slice();
var request = try globalThis.allocator().create(@This());
var hasher = std.hash.Wyhash.init(0);
hasher.update(name);
@@ -643,7 +642,7 @@ pub fn ResolveInfoRequest(comptime cares_type: type, comptime type_name: []const
request.* = .{
.resolver_for_caching = resolver,
.hash = hash,
- .head = .{ .poll_ref = poll_ref, .globalThis = globalThis, .promise = JSC.JSPromise.Strong.init(globalThis), .allocated = false, .name = name_str },
+ .head = .{ .poll_ref = poll_ref, .globalThis = globalThis, .promise = JSC.JSPromise.Strong.init(globalThis), .allocated = false, .name = name },
};
request.tail = &request.head;
if (cache == .new) {
@@ -943,9 +942,9 @@ pub fn CAresLookup(comptime cares_type: type, comptime type_name: []const u8) ty
poll_ref: JSC.PollRef,
allocated: bool = false,
next: ?*@This() = null,
- name: *const JSC.ZigString.Slice,
+ name: []const u8,
- pub fn init(globalThis: *JSC.JSGlobalObject, allocator: std.mem.Allocator, name: *const JSC.ZigString.Slice) !*@This() {
+ pub fn init(globalThis: *JSC.JSGlobalObject, allocator: std.mem.Allocator, name: []const u8) !*@This() {
var this = try allocator.create(@This());
var poll_ref = JSC.PollRef.init();
poll_ref.ref(globalThis.bunVM());
@@ -998,7 +997,7 @@ pub fn CAresLookup(comptime cares_type: type, comptime type_name: []const u8) ty
pub fn deinit(this: *@This()) void {
this.poll_ref.unrefOnNextTick(this.globalThis.bunVM());
- this.name.deinit();
+ bun.default_allocator.free(this.name);
if (this.allocated)
this.globalThis.allocator().destroy(this);
@@ -1529,7 +1528,7 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
@@ -1546,25 +1545,25 @@ pub const DNSResolver = struct {
return resolver.doLookup(name.slice(), 0, options, globalThis);
},
RecordType.CNAME => {
- return resolver.doResolveCAres(c_ares.struct_hostent, "cname", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_hostent, "cname", name.slice(), globalThis);
},
RecordType.MX => {
- return resolver.doResolveCAres(c_ares.struct_ares_mx_reply, "mx", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_mx_reply, "mx", name.slice(), globalThis);
},
RecordType.NS => {
- return resolver.doResolveCAres(c_ares.struct_hostent, "ns", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_hostent, "ns", name.slice(), globalThis);
},
RecordType.PTR => {
- return resolver.doResolveCAres(c_ares.struct_hostent, "ptr", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_hostent, "ptr", name.slice(), globalThis);
},
RecordType.SOA => {
- return resolver.doResolveCAres(c_ares.struct_ares_soa_reply, "soa", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_soa_reply, "soa", name.slice(), globalThis);
},
RecordType.SRV => {
- return resolver.doResolveCAres(c_ares.struct_ares_srv_reply, "srv", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_srv_reply, "srv", name.slice(), globalThis);
},
RecordType.TXT => {
- return resolver.doResolveCAres(c_ares.struct_ares_txt_reply, "txt", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_txt_reply, "txt", name.slice(), globalThis);
},
}
}
@@ -1663,12 +1662,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_ares_srv_reply, "srv", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_srv_reply, "srv", name.slice(), globalThis);
}
pub fn resolveSoa(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1694,12 +1693,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_ares_soa_reply, "soa", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_soa_reply, "soa", name.slice(), globalThis);
}
pub fn resolveCaa(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1725,12 +1724,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_ares_caa_reply, "caa", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_caa_reply, "caa", name.slice(), globalThis);
}
pub fn resolveNs(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1756,12 +1755,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_hostent, "ns", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_hostent, "ns", name.slice(), globalThis);
}
pub fn resolvePtr(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1787,12 +1786,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_hostent, "ptr", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_hostent, "ptr", name.slice(), globalThis);
}
pub fn resolveCname(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1818,12 +1817,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_hostent, "cname", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_hostent, "cname", name.slice(), globalThis);
}
pub fn resolveMx(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1849,12 +1848,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_ares_mx_reply, "mx", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_mx_reply, "mx", name.slice(), globalThis);
}
pub fn resolveNaptr(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1880,12 +1879,12 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_ares_naptr_reply, "naptr", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_naptr_reply, "naptr", name.slice(), globalThis);
}
pub fn resolveTxt(globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSC.JSValue {
@@ -1911,17 +1910,15 @@ pub const DNSResolver = struct {
return .zero;
}
- const name = name_str.toSlice(globalThis, bun.default_allocator);
+ const name = name_str.toSliceClone(globalThis, bun.default_allocator);
var vm = globalThis.bunVM();
var resolver = vm.rareData().globalDNSResolver(vm);
- return resolver.doResolveCAres(c_ares.struct_ares_txt_reply, "txt", &name, globalThis);
+ return resolver.doResolveCAres(c_ares.struct_ares_txt_reply, "txt", name.slice(), globalThis);
}
- pub fn doResolveCAres(this: *DNSResolver, comptime cares_type: type, comptime type_name: []const u8, name_str: *const JSC.ZigString.Slice, globalThis: *JSC.JSGlobalObject) JSC.JSValue {
- var name = name_str.slice();
-
+ pub fn doResolveCAres(this: *DNSResolver, comptime cares_type: type, comptime type_name: []const u8, name: []const u8, globalThis: *JSC.JSGlobalObject) JSC.JSValue {
var channel: *c_ares.Channel = switch (this.getChannel()) {
.result => |res| res,
.err => |err| {
@@ -1943,7 +1940,7 @@ pub const DNSResolver = struct {
var cache = this.getOrPutIntoResolvePendingCache(ResolveInfoRequest(cares_type, type_name), key, cache_name);
if (cache == .inflight) {
// CAresLookup will have the name ownership
- var cares_lookup = CAresLookup(cares_type, type_name).init(globalThis, globalThis.allocator(), name_str) catch unreachable;
+ var cares_lookup = CAresLookup(cares_type, type_name).init(globalThis, globalThis.allocator(), name) catch unreachable;
cache.inflight.append(cares_lookup);
return cares_lookup.promise.value();
}
@@ -1951,7 +1948,7 @@ pub const DNSResolver = struct {
var request = ResolveInfoRequest(cares_type, type_name).init(
cache,
this,
- name_str, // CAresLookup will have the ownership
+ name, // CAresLookup will have the ownership
globalThis,
cache_name,
) catch unreachable;
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig
index 9a775990c..fa7efa728 100644
--- a/src/bun.js/bindings/bindings.zig
+++ b/src/bun.js/bindings/bindings.zig
@@ -368,7 +368,7 @@ pub const ZigString = extern struct {
}
pub fn sliceZ(this: Slice) [:0]const u8 {
- return this.ptr[0..this.len:0];
+ return this.ptr[0..this.len :0];
}
pub fn toSliceZ(this: Slice, buf: []u8) [:0]const u8 {
@@ -386,7 +386,7 @@ pub const ZigString = extern struct {
std.mem.copy(u8, buf[0..this.len], this.slice());
buf[this.len] = 0;
- return buf[0..this.len:0];
+ return buf[0..this.len :0];
}
pub fn mut(this: Slice) []u8 {
@@ -604,7 +604,7 @@ pub const ZigString = extern struct {
if (this.len == 0)
return Slice.empty;
if (is16Bit(&this)) {
- var buffer = this.toOwnedSlice(allocator) catch unreachable;
+ const buffer = this.toOwnedSlice(allocator) catch unreachable;
return Slice{
.allocator = NullableAllocator.init(allocator),
.ptr = buffer.ptr,
@@ -613,7 +613,7 @@ pub const ZigString = extern struct {
}
if (!this.isUTF8() and !strings.isAllASCII(untagged(this.ptr)[0..this.len])) {
- var buffer = this.toOwnedSlice(allocator) catch unreachable;
+ const buffer = this.toOwnedSlice(allocator) catch unreachable;
return Slice{
.allocator = NullableAllocator.init(allocator),
.ptr = buffer.ptr,
@@ -627,6 +627,17 @@ pub const ZigString = extern struct {
};
}
+ pub fn toSliceClone(this: ZigString, allocator: std.mem.Allocator) Slice {
+ if (this.len == 0)
+ return Slice.empty;
+ const buffer = this.toOwnedSlice(allocator) catch unreachable;
+ return Slice{
+ .allocator = NullableAllocator.init(allocator),
+ .ptr = buffer.ptr,
+ .len = @truncate(u32, buffer.len),
+ };
+ }
+
pub fn toSliceZ(this: ZigString, allocator: std.mem.Allocator) Slice {
if (this.len == 0)
return Slice.empty;
@@ -1305,6 +1316,7 @@ pub const JSString = extern struct {
return out;
}
+ // doesn't always allocate
pub fn toSlice(
this: *JSString,
global: *JSGlobalObject,
@@ -1315,6 +1327,16 @@ pub const JSString = extern struct {
return str.toSlice(allocator);
}
+ pub fn toSliceClone(
+ this: *JSString,
+ global: *JSGlobalObject,
+ allocator: std.mem.Allocator,
+ ) ZigString.Slice {
+ var str = ZigString.init("");
+ this.toZigString(global, &str);
+ return str.toSliceClone(allocator);
+ }
+
pub fn toSliceZ(
this: *JSString,
global: *JSGlobalObject,