@@ -313,6 +313,7 @@ pub const SocketConfig = struct {
313
313
314
314
pub fn fromJS (vm : * JSC.VirtualMachine , opts : JSC.JSValue , globalObject : * JSC.JSGlobalObject ) bun.JSError ! SocketConfig {
315
315
var hostname_or_unix : JSC.ZigString.Slice = JSC .ZigString .Slice .empty ;
316
+ errdefer hostname_or_unix .deinit ();
316
317
var port : ? u16 = null ;
317
318
var exclusive = false ;
318
319
var allowHalfOpen = false ;
@@ -332,23 +333,31 @@ pub const SocketConfig = struct {
332
333
}
333
334
}
334
335
336
+ errdefer {
337
+ if (ssl != null ) {
338
+ ssl .? .deinit ();
339
+ }
340
+ }
341
+
335
342
hostname_or_unix : {
336
343
if (try opts .getTruthy (globalObject , "fd" )) | fd_ | {
337
344
if (fd_ .isNumber ()) {
338
345
break :hostname_or_unix ;
339
346
}
340
347
}
341
348
342
- if (try opts .getTruthy (globalObject , "unix" )) | unix_socket | {
343
- if (! unix_socket .isString ()) {
344
- return globalObject .throwInvalidArguments ("Expected \" unix\" to be a string" , .{});
345
- }
349
+ if (try opts .getStringish (globalObject , "unix" )) | unix_socket | {
350
+ defer unix_socket .deref ();
346
351
347
- hostname_or_unix = unix_socket .getZigString ( globalObject ). toSlice (bun .default_allocator );
352
+ hostname_or_unix = try unix_socket .toUTF8WithoutRef ( bun . default_allocator ). cloneIfNeeded (bun .default_allocator );
348
353
349
354
if (strings .hasPrefixComptime (hostname_or_unix .slice (), "file://" ) or strings .hasPrefixComptime (hostname_or_unix .slice (), "unix://" ) or strings .hasPrefixComptime (hostname_or_unix .slice (), "sock://" )) {
350
- hostname_or_unix .ptr += 7 ;
351
- hostname_or_unix .len - |= 7 ;
355
+ // The memory allocator relies on the pointer address to
356
+ // free it, so if we simply moved the pointer up it would
357
+ // cause an issue when freeing it later.
358
+ const moved_bytes = try bun .default_allocator .dupeZ (u8 , hostname_or_unix .slice ()[7.. ]);
359
+ hostname_or_unix .deinit ();
360
+ hostname_or_unix = ZigString .Slice .init (bun .default_allocator , moved_bytes );
352
361
}
353
362
354
363
if (hostname_or_unix .len > 0 ) {
@@ -363,20 +372,21 @@ pub const SocketConfig = struct {
363
372
allowHalfOpen = true ;
364
373
}
365
374
366
- if (try opts .getTruthy (globalObject , "hostname" ) orelse try opts .getTruthy (globalObject , "host" )) | hostname | {
367
- if (! hostname .isString ()) {
368
- return globalObject .throwInvalidArguments ("Expected \" hostname\" to be a string" , .{});
369
- }
375
+ if (try opts .getStringish (globalObject , "hostname" ) orelse try opts .getStringish (globalObject , "host" )) | hostname | {
376
+ defer hostname .deref ();
370
377
371
378
var port_value = try opts .get (globalObject , "port" ) orelse JSValue .zero ;
372
- hostname_or_unix = hostname .getZigString ( globalObject ). toSlice (bun .default_allocator );
379
+ hostname_or_unix = try hostname .toUTF8WithoutRef ( bun . default_allocator ). cloneIfNeeded (bun .default_allocator );
373
380
374
381
if (port_value .isEmptyOrUndefinedOrNull () and hostname_or_unix .len > 0 ) {
375
382
const parsed_url = bun .URL .parse (hostname_or_unix .slice ());
376
383
if (parsed_url .getPort ()) | port_num | {
377
384
port_value = JSValue .jsNumber (port_num );
378
- hostname_or_unix .ptr = parsed_url .hostname .ptr ;
379
- hostname_or_unix .len = @as (u32 , @truncate (parsed_url .hostname .len ));
385
+ if (parsed_url .hostname .len > 0 ) {
386
+ const moved_bytes = try bun .default_allocator .dupeZ (u8 , parsed_url .hostname );
387
+ hostname_or_unix .deinit ();
388
+ hostname_or_unix = ZigString .Slice .init (bun .default_allocator , moved_bytes );
389
+ }
380
390
}
381
391
}
382
392
@@ -410,7 +420,6 @@ pub const SocketConfig = struct {
410
420
411
421
return globalObject .throwInvalidArguments ("Expected either \" hostname\" or \" unix\" " , .{});
412
422
}
413
- errdefer hostname_or_unix .deinit ();
414
423
415
424
var handlers = try Handlers .fromJS (globalObject , try opts .get (globalObject , "socket" ) orelse JSValue .zero );
416
425
0 commit comments