mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
## Summary Fixes a crash with `panic: attempt to use null value` in `html_rewriter.zig:1190` when accessing TextChunk properties after HTMLRewriter cleanup. The crash occurred in the `lastInTextNode` and `removed` methods when they tried to dereference a null `text_chunk` pointer using `this.text_chunk.?` without proper null checks. ## Root Cause The TextChunk methods `removed()` and `lastInTextNode()` were missing null checks that other methods like `getText()` and `remove()` already had. When TextChunk objects are accessed after the HTMLRewriter transformation completes and internal cleanup occurs, the `text_chunk` pointer becomes null, causing a panic. ## Changes - **src/bun.js/api/html_rewriter.zig**: - Add null check to `removed()` method - returns `false` when `text_chunk` is null - Add null check to `lastInTextNode()` method - returns `false` when `text_chunk` is null - **test/regression/issue/text-chunk-null-access.test.ts**: - Add regression test that reproduces the original crash scenario - Test verifies that accessing TextChunk properties after cleanup returns sensible defaults instead of crashing ## Crash Reproduction The regression test successfully reproduces the crash: - **Regular `bun test`**: ❌ CRASHES with `panic: attempt to use null value` - **With fix `bun bd test`**: ✅ PASSES ## Test Plan - [x] Existing HTMLRewriter tests still pass - [x] New regression test passes with the fix - [x] New regression test crashes without the fix (confirmed on regular bun) - [x] Both `removed` and `lastInTextNode` now return sensible defaults (`false`) when called on cleaned up TextChunk objects 🤖 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: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
29 lines
834 B
TypeScript
29 lines
834 B
TypeScript
import { expect, test } from "bun:test";
|
|
|
|
test("TextChunk methods handle null text_chunk gracefully", async () => {
|
|
// This test reproduces a crash where TextChunk methods are called
|
|
// after the underlying text_chunk has been cleaned up or is null
|
|
|
|
let textChunkRef: any;
|
|
|
|
const html = "<p>Test content</p>";
|
|
|
|
const rewriter = new HTMLRewriter().on("p", {
|
|
text(text) {
|
|
// Store reference to the text chunk
|
|
textChunkRef = text;
|
|
},
|
|
});
|
|
|
|
await rewriter.transform(new Response(html)).text();
|
|
|
|
// Force garbage collection to clean up internal references
|
|
if (typeof Bun !== "undefined" && Bun.gc) {
|
|
Bun.gc(true);
|
|
}
|
|
|
|
// It should be undefined to be consistent with the rest of the APIs.
|
|
expect(textChunkRef.removed).toBeUndefined();
|
|
expect(textChunkRef.lastInTextNode).toBeUndefined();
|
|
});
|