Fix PostgreSQL StringBuilder assertion failure with empty error messages (#22558)

## Summary
- Fixed a debug build assertion failure in PostgreSQL error handling
when all error message fields are empty
- Added safety check before calling `StringBuilder.allocatedSlice()` to
handle zero-length messages
- Added regression test to prevent future occurrences

## The Problem
When PostgreSQL sends an error response with completely empty message
fields, the `ErrorResponse.toJS` function would:
1. Calculate `b.cap` but end up with `b.len = 0` (no actual content)
2. Call `b.allocatedSlice()[0..b.len]` unconditionally
3. Trigger an assertion in `StringBuilder.allocatedSlice()` that
requires `cap > 0`

This only affected debug builds since the assertion is compiled out in
release builds.

## The Fix
Check if `b.len > 0` before calling `allocatedSlice()`. If there's no
content, use an empty string instead.

## Test Plan
- [x] Added regression test that triggers the exact crash scenario
- [x] Verified test crashes without the fix (debug build)
- [x] Verified test passes with the fix
- [x] Confirmed release builds were not affected

🤖 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>
This commit is contained in:
robobun
2025-09-10 18:47:50 -07:00
committed by GitHub
parent 6e3349b55c
commit 09c56c8ba8
2 changed files with 49 additions and 1 deletions

View File

@@ -132,7 +132,9 @@ pub fn toJS(this: ErrorResponse, globalObject: *jsc.JSGlobalObject) JSValue {
const line_slice = if (line.isEmpty()) null else line.byteSlice();
const routine_slice = if (routine.isEmpty()) null else routine.byteSlice();
return createPostgresError(globalObject, b.allocatedSlice()[0..b.len], .{
const error_message = if (b.len > 0) b.allocatedSlice()[0..b.len] else "";
return createPostgresError(globalObject, error_message, .{
.code = error_code,
.errno = errno,
.detail = detail_slice,