mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
Replace catch bun.outOfMemory() with safer alternatives (#22141)
Replace `catch bun.outOfMemory()`, which can accidentally catch non-OOM-related errors, with either `bun.handleOom` or a manual `catch |err| switch (err)`. (For internal tracking: fixes STAB-1070) --------- Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
This commit is contained in:
@@ -124,7 +124,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
pub fn toUTF8(this: WTFStringImpl, allocator: std.mem.Allocator) ZigString.Slice {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()))) |utf8| {
|
||||
return ZigString.Slice.init(allocator, utf8.items);
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
return ZigString.Slice.init(
|
||||
allocator,
|
||||
bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch bun.outOfMemory(),
|
||||
bun.handleOom(bun.strings.toUTF8Alloc(allocator, this.utf16Slice())),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
pub fn toUTF8WithoutRef(this: WTFStringImpl, allocator: std.mem.Allocator) ZigString.Slice {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()))) |utf8| {
|
||||
return ZigString.Slice.init(allocator, utf8.items);
|
||||
}
|
||||
|
||||
@@ -150,24 +150,24 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
return ZigString.Slice.init(
|
||||
allocator,
|
||||
bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch bun.outOfMemory(),
|
||||
bun.handleOom(bun.strings.toUTF8Alloc(allocator, this.utf16Slice())),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn toOwnedSliceZ(this: WTFStringImpl, allocator: std.mem.Allocator) [:0]u8 {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1Z(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1Z(allocator, this.latin1Slice()))) |utf8| {
|
||||
return utf8.items[0 .. utf8.items.len - 1 :0];
|
||||
}
|
||||
|
||||
return allocator.dupeZ(u8, this.latin1Slice()) catch bun.outOfMemory();
|
||||
return bun.handleOom(allocator.dupeZ(u8, this.latin1Slice()));
|
||||
}
|
||||
return bun.strings.toUTF8AllocZ(allocator, this.utf16Slice()) catch bun.outOfMemory();
|
||||
return bun.handleOom(bun.strings.toUTF8AllocZ(allocator, this.utf16Slice()));
|
||||
}
|
||||
|
||||
pub fn toUTF8IfNeeded(this: WTFStringImpl, allocator: std.mem.Allocator) ?ZigString.Slice {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()))) |utf8| {
|
||||
return ZigString.Slice.init(allocator, utf8.items);
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
return ZigString.Slice.init(
|
||||
allocator,
|
||||
bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch bun.outOfMemory(),
|
||||
bun.handleOom(bun.strings.toUTF8Alloc(allocator, this.utf16Slice())),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user