diff options
author | 2022-09-09 19:20:24 -0700 | |
---|---|---|
committer | 2022-09-09 19:20:24 -0700 | |
commit | 3d8edcb77b7579090ebf370f12ce6107f3b3fb04 (patch) | |
tree | 59547c1d80f1856432b9e0f264cae0780f019c6a /src | |
parent | f496740c1926d2349434ba01ceddc6c8b6aadfa0 (diff) | |
download | bun-3d8edcb77b7579090ebf370f12ce6107f3b3fb04.tar.gz bun-3d8edcb77b7579090ebf370f12ce6107f3b3fb04.tar.zst bun-3d8edcb77b7579090ebf370f12ce6107f3b3fb04.zip |
Fix potential crash when returning an empty string
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/base.zig | 16 | ||||
-rw-r--r-- | src/bun.js/node/types.zig | 6 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig index 8439ff7e1..eb3b84118 100644 --- a/src/bun.js/base.zig +++ b/src/bun.js/base.zig @@ -2399,6 +2399,8 @@ pub const ArrayBuffer = extern struct { value: JSC.JSValue = JSC.JSValue.zero, shared: bool = false, + pub const empty = ArrayBuffer{ .offset = 0, .len = 0, .byte_len = 0, .typed_array_type = .Uint8Array, .ptr = undefined }; + pub const name = "Bun__ArrayBuffer"; pub const Stream = std.io.FixedBufferStream([]u8); @@ -2580,6 +2582,11 @@ pub const MarkedArrayBuffer = struct { }; } + pub const empty = MarkedArrayBuffer{ + .allocator = null, + .buffer = ArrayBuffer.empty, + }; + pub inline fn slice(this: *const @This()) []u8 { return this.buffer.slice(); } @@ -2608,6 +2615,15 @@ pub const MarkedArrayBuffer = struct { if (!this.buffer.value.isEmptyOrUndefinedOrNull()) { return this.buffer.value.asObjectRef(); } + if (this.buffer.byte_len == 0) { + return js.JSObjectMakeTypedArray( + ctx, + this.buffer.typed_array_type.toC(), + 0, + exception, + ); + } + return js.JSObjectMakeTypedArrayWithBytesNoCopy( ctx, this.buffer.typed_array_type.toC(), diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig index 457ddc012..313dad703 100644 --- a/src/bun.js/node/types.zig +++ b/src/bun.js/node/types.zig @@ -193,6 +193,9 @@ pub const StringOrBuffer = union(Tag) { pub fn toJS(this: StringOrBuffer, ctx: JSC.C.JSContextRef, exception: JSC.C.ExceptionRef) JSC.C.JSValueRef { return switch (this) { .string => { + if (this.string.len == 0) + return JSC.ZigString.Empty.toValue(ctx).asObjectRef(); + const input = this.string; if (strings.toUTF16Alloc(bun.default_allocator, input, false) catch null) |utf16| { bun.default_allocator.free(bun.constStrToU8(input)); @@ -240,6 +243,9 @@ pub const StringOrNodeBuffer = union(Tag) { return switch (this) { .string => { const input = this.string; + if (this.string.len == 0) + return JSC.ZigString.Empty.toValue(ctx).asObjectRef(); + if (strings.toUTF16Alloc(bun.default_allocator, input, false) catch null) |utf16| { bun.default_allocator.free(bun.constStrToU8(input)); return JSC.ZigString.toExternalU16(utf16.ptr, utf16.len, ctx.ptr()).asObjectRef(); |