Files
bun.sh/test/regression/issue/22656.macro.ts
robobun 57b93f6ea3 Fix panic when macros return collections with 3+ arrays/objects (#22827)
## Summary

Fixes #22656, #11730, and #7116

Fixes a panic that occurred when macros returned collections containing
three or more arrays or objects.

## Problem

The issue was caused by hash table resizing during recursive processing.
When `this.run()` was called recursively to process nested
arrays/objects, it could add more entries to the `visited` map,
triggering a resize. This would invalidate the `_entry.value_ptr`
pointer obtained from `getOrPut`, leading to memory corruption and
crashes.

## Solution

The fix ensures we handle hash table resizing safely:

1. Use `getOrPut` to reserve an entry and store a placeholder
2. Process all children (which may trigger hash table resizing)
3. Create the final expression with all data
4. Use `put` to update the entry (safe even after resizing)

This approach is applied consistently to both arrays and objects.

## Verification

All three issues have been tested and verified as fixed:

###  #22656 - "Panic when returning collections with three or more
arrays or objects"
- **Before**: `panic(main thread): switch on corrupt value`
- **After**: Works correctly

###  #11730 - "Constructing deep objects in macros causes segfaults"
- **Before**: `Segmentation fault at address 0x8` with deep nested
structures
- **After**: Handles deep nesting without crashes

###  #7116 - "[macro] crash with large complex array"
- **Before**: Crashes with objects containing 50+ properties (hash table
stress)
- **After**: Processes large complex arrays successfully

## Test Plan

Added comprehensive regression tests that cover:
- Collections with 3+ arrays
- Collections with 3+ objects
- Deeply nested structures (5+ levels)
- Objects with many properties (50+) to stress hash table operations
- Mixed collections of arrays and objects

All tests pass with the fix applied.

🤖 Generated with [Claude Code](https://claude.ai/code)

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-09-24 18:25:39 -07:00

62 lines
1.6 KiB
TypeScript

// Issue #22656 - Panic when macros return collections with 3+ arrays/objects
// https://github.com/oven-sh/bun/issues/22656
export function collectionOfArrays() {
// Returns an array containing 3+ arrays - this triggers the crash
return [
{ a: [] },
{ b: [] },
{ c: [] }
];
}
export function collectionOfObjects() {
// Returns an array containing 3+ objects - this also triggers the crash
return [
{ a: {} },
{ b: {} },
{ c: {} }
];
}
export function deeplyNested() {
// Complex nested structure with multiple arrays and objects
const base = {
type: 'root',
children: [
{ type: 'child1', data: { arrays: [[], [], []] } },
{ type: 'child2', data: { objects: [{}, {}, {}] } },
{ type: 'child3', nested: { deep: { values: [1, 2, 3] } } }
],
meta: Array.from({ length: 5 }, (_, i) => ({
id: i,
value: { empty: {} }
}))
};
// Return multiple copies using JSON parse/stringify pattern (from issue #11730)
const makeObject = () => JSON.parse(JSON.stringify(base));
return [makeObject(), makeObject(), makeObject()];
}
export function largeArrayWithSpreading() {
// From issue #7116 - large arrays with spreading pattern
const baseArray = Array.from({ length: 5 }, (_, i) => ({
name: `item_${i}`,
data: {
arrays: [[], [], []],
objects: [{}, {}, {}]
}
}));
const arr = () => baseArray.map(x => ({
...x,
extra: {
arrays: [{ a: [] }, { b: [] }, { c: [] }],
objects: [{ x: {} }, { y: {} }, { z: {} }]
}
}));
// Spreading multiple times
return [...arr(), ...arr(), ...arr()];
}