Skip to content

pg-protocol: stop parseErrorMessage looping on an empty error or notice body - #3874

Closed
aaronjmars wants to merge 1 commit into
brianc:masterfrom
aaronjmars:fix-empty-error-message-loop
Closed

aaronjmars wants to merge 1 commit into
brianc:masterfrom
aaronjmars:fix-empty-error-message-loop

Conversation

@aaronjmars

Copy link
Copy Markdown

An ErrorResponse (E) or NoticeResponse (N) frame with length 4 has no body. parseErrorMessage reads a field type with reader.string(1) and loops until it sees \0. With an empty body that first read is already past the frame: it either consumes the next message, or, at the end of the buffer, keeps returning '' so the loop never ends and the parse call never returns. For example, new Parser().parse(Buffer.from([0x45, 0, 0, 0, 4]), cb) hangs.

This is a different loop from the one fixed in #3691. That PR made cstring() stop on any falsy byte; the outer while (fieldType !== '\0') loop in parseErrorMessage still spins when reader.string(1) returns ''.

Changes:

  • Point the reader at just the declared body for E and N messages, so a short frame cannot read into the next message.
  • Return a DatabaseError if the field list ends without its terminator, instead of looping.

Normal PostgreSQL servers never send this frame, so behavior for real error and notice messages is unchanged.

Tests: added cases in inbound-parser.test.ts for an empty E and N frame, both alone and followed by ReadyForQuery. Without the fix the alone case hangs; with it all 82 pg-protocol tests pass.

…ce body

An ErrorResponse or NoticeResponse with length 4 has no body. The first
reader.string(1) then reads past the frame, and once past the end of the
buffer it returns '' forever, so the field loop never ends.

Clamp error and notice reads to the declared body and return a
DatabaseError when the field list has no terminator.
@charmander

charmander commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

incomplete/misleading title → bad test. bad implementation. not a fan of undisclosed AI-generated text and code. just open an issue.

@charmander charmander closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants