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
v1-0001-pg_stash_advice-Reject-embedded-NUL-bytes-in-persistence-files.patch
Description: Binary data
