[PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files

From: Haibo Yan <tristan(dot)yim(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: [PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
Date: 2026-11-17 02:35:51
Message-ID: CABXr29FCkRXumwV1sFvbZd_uEw5A4Jg_oDVnWgLjgRN6esUHTA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

The initial leads for this work came from several independent
LLM-assisted reviews of the plan advice code. The reviews identified
several candidate issues, which we then reproduced and checked
individually. Several issues were confirmed.

This is the first patch we are posting from that review. It addresses
one of the confirmed issues. The remaining fixes are independent and
will be submitted separately after their tests and reviews are
complete.

pg_stash_advice reads its persistence file into memory, but the loader
previously parsed the buffer as a NUL-terminated string. If the file
contained an embedded NUL byte, the loader silently treated it as the
end of the file and accepted only the preceding records.

A later persistence write could then replace the original file with
that partial state, permanently removing the stashes and entries after
the NUL byte.

The attached patch checks exactly the bytes returned by fread before
parsing. It reports ERRCODE_DATA_CORRUPTED if an embedded NUL is found
and keeps the parsing loop bounded by the number of bytes read.

The patch also adds TAP coverage for NUL bytes at the beginning of the
file, between records, inside an advice string, and at the final byte.
The tests verify that no partial state becomes visible, stash changes
remain locked out, and the damaged file remains byte-for-byte
unchanged after shutdown.

The complete pg_stash_advice test suite passes:

regress: 2/2
001_persist: 6/6
002_persist_nul: 41/41

I also ran the new test against the unmodified loader. It failed on
the expected assertions, confirming that it detects the original
silent truncation and subsequent rewrite.
Feedback is welcome.

Haibo

Attachment Content-Type Size
v1-0001-pg_stash_advice-Reject-embedded-NUL-bytes-in-persistence-files.patch application/octet-stream 9.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-11-17 02:53:57 Re: Document prepared transaction lock waits with two_phase subscriptions
Previous Message Chao Li 2026-11-17 02:28:32 Rework: Enable fast default for domains with non-volatile constraints