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:
taylor.fish
2025-08-26 12:50:25 -07:00
committed by GitHub
parent 300f486125
commit 437e15bae5
284 changed files with 1835 additions and 1662 deletions

View File

@@ -364,7 +364,7 @@ pub const JunitReporter = struct {
this.getHostname() orelse "",
});
this.contents.insertSlice(bun.default_allocator, suite_info.offset_of_attributes, summary) catch bun.outOfMemory();
bun.handleOom(this.contents.insertSlice(bun.default_allocator, suite_info.offset_of_attributes, summary));
const indent = getIndent(this.current_depth);
try this.contents.appendSlice(bun.default_allocator, indent);
@@ -548,8 +548,8 @@ pub const JunitReporter = struct {
metrics.skipped,
elapsed_time,
});
this.contents.insertSlice(bun.default_allocator, this.offset_of_testsuites_value, summary) catch bun.outOfMemory();
this.contents.appendSlice(bun.default_allocator, "</testsuites>\n") catch bun.outOfMemory();
bun.handleOom(this.contents.insertSlice(bun.default_allocator, this.offset_of_testsuites_value, summary));
bun.handleOom(this.contents.appendSlice(bun.default_allocator, "</testsuites>\n"));
}
var junit_path_buf: bun.PathBuffer = undefined;
@@ -686,14 +686,14 @@ pub const CommandLineReporter = struct {
if (!strings.eql(junit.current_file, filename)) {
while (junit.suite_stack.items.len > 0 and !junit.suite_stack.items[junit.suite_stack.items.len - 1].is_file_suite) {
junit.endTestSuite() catch bun.outOfMemory();
bun.handleOom(junit.endTestSuite());
}
if (junit.current_file.len > 0) {
junit.endTestSuite() catch bun.outOfMemory();
bun.handleOom(junit.endTestSuite());
}
junit.beginTestSuite(filename) catch bun.outOfMemory();
bun.handleOom(junit.beginTestSuite(filename));
}
// To make the juint reporter generate nested suites, we need to find the needed suites and create/print them.
@@ -705,7 +705,7 @@ pub const CommandLineReporter = struct {
const index = (scopes.len - 1) - i;
const scope = scopes[index];
if (scope.label.len > 0) {
needed_suites.append(scope) catch bun.outOfMemory();
bun.handleOom(needed_suites.append(scope));
}
}
@@ -720,7 +720,7 @@ pub const CommandLineReporter = struct {
while (current_suite_depth > needed_suites.items.len) {
if (junit.suite_stack.items.len > 0 and !junit.suite_stack.items[junit.suite_stack.items.len - 1].is_file_suite) {
junit.endTestSuite() catch bun.outOfMemory();
bun.handleOom(junit.endTestSuite());
current_suite_depth -= 1;
} else {
break;
@@ -747,7 +747,7 @@ pub const CommandLineReporter = struct {
while (suites_to_close > 0) {
if (junit.suite_stack.items.len > 0 and !junit.suite_stack.items[junit.suite_stack.items.len - 1].is_file_suite) {
junit.endTestSuite() catch bun.outOfMemory();
bun.handleOom(junit.endTestSuite());
current_suite_depth -= 1;
suites_to_close -= 1;
} else {
@@ -764,7 +764,7 @@ pub const CommandLineReporter = struct {
while (describe_suite_index < needed_suites.items.len) {
const scope = needed_suites.items[describe_suite_index];
junit.beginTestSuiteWithLine(scope.label, scope.line_number, false) catch bun.outOfMemory();
bun.handleOom(junit.beginTestSuiteWithLine(scope.label, scope.line_number, false));
describe_suite_index += 1;
}
@@ -779,15 +779,15 @@ pub const CommandLineReporter = struct {
for (scopes) |scope| {
if (scope.label.len > 0) {
if (initial_length != concatenated_describe_scopes.items.len) {
concatenated_describe_scopes.appendSlice(" &gt; ") catch bun.outOfMemory();
bun.handleOom(concatenated_describe_scopes.appendSlice(" &gt; "));
}
escapeXml(scope.label, concatenated_describe_scopes.writer()) catch bun.outOfMemory();
bun.handleOom(escapeXml(scope.label, concatenated_describe_scopes.writer()));
}
}
}
junit.writeTestCase(status, filename, display_label, concatenated_describe_scopes.items, assertions, elapsed_ns, line_number) catch bun.outOfMemory();
bun.handleOom(junit.writeTestCase(status, filename, display_label, concatenated_describe_scopes.items, assertions, elapsed_ns, line_number));
},
}
}
@@ -1424,7 +1424,7 @@ pub const TestCommand = struct {
//
try vm.ensureDebugger(false);
var scanner = Scanner.init(ctx.allocator, &vm.transpiler, ctx.positionals.len) catch bun.outOfMemory();
var scanner = bun.handleOom(Scanner.init(ctx.allocator, &vm.transpiler, ctx.positionals.len));
defer scanner.deinit();
const has_relative_path = for (ctx.positionals) |arg| {
if (std.fs.path.isAbsolute(arg) or
@@ -1487,7 +1487,7 @@ pub const TestCommand = struct {
};
}
const test_files = scanner.takeFoundTestFiles() catch bun.outOfMemory();
const test_files = bun.handleOom(scanner.takeFoundTestFiles());
defer ctx.allocator.free(test_files);
const search_count = scanner.search_count;