[PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
[PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
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
Re: [PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Nov 17, 2026, at 10:35, Haibo Yan wrote: > > 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. Hi Haibo, Thanks for the patch. I'm just curious why a file might contain an embedded NUL byte. Is there a known scenario where pg_stash_advice.tsv could contain a NUL byte? If that's because of file corruption, couldn't any byte appear in the middle of the file? In that case, is checking only for NUL bytes good enough? Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: [PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
Hi Chao, Thanks for reviewing. I haven't found a case where normal writes or a crash left an embedded NUL in pg_stash_advice.tsv. The writer uses a temporary file and durable_rename(), and SQL text input rejects NUL even with SQL_ASCII. I inserted the NUL by hand for the test. Other byte changes can go undetected. NUL is worth checking because the loader mistakes it for EOF, reports success, and can discard the remaining records on the next save. Best regards, Haibo On Mon, Nov 16, 2026 at 11:26 PM Chao Li wrote: > > > > > On Nov 17, 2026, at 10:35, Haibo Yan wrote: > > > > 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. > > Hi Haibo, > > Thanks for the patch. > > I'm just curious why a file might contain an embedded NUL byte. Is there a known scenario where pg_stash_advice.tsv could contain a NUL byte? If that's because of file corruption, couldn't any byte appear in the middle of the file? In that case, is checking only for NUL bytes good enough? > > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ > > > >
Re: [PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
От:
Chao Li <li.evan.chao@gmail.com>
Дата:
> On Nov 18, 2026, at 02:27, Haibo Yan wrote: > > Hi Chao, > > Thanks for reviewing. I haven't found a case where normal writes or > a crash left an embedded NUL in pg_stash_advice.tsv. The writer uses a > temporary file and durable_rename(), and SQL text input rejects NUL > even with SQL_ASCII. I inserted the NUL by hand for the test. > > Other byte changes can go undetected. NUL is worth checking because > the loader mistakes it for EOF, reports success, and can discard the > remaining records on the next save. > > Best regards, > Haibo If this is defensive handling of a manually corrupted file, then the same concern may apply to many other text file readers. In that case, instead of adding a check specifically for the stash file, maybe a better way is to add a generic text file reader to handle all possible errors in a single place. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/
Re: [PATCH v1] pg_stash_advice: Reject embedded NUL bytes in persistence files
От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
On Tue, Nov 17, 2026 at 6:23 PM Chao Li wrote: > > > > > On Nov 18, 2026, at 02:27, Haibo Yan wrote: > > > > Hi Chao, > > > > Thanks for reviewing. I haven't found a case where normal writes or > > a crash left an embedded NUL in pg_stash_advice.tsv. The writer uses a > > temporary file and durable_rename(), and SQL text input rejects NUL > > even with SQL_ASCII. I inserted the NUL by hand for the test. > > > > Other byte changes can go undetected. NUL is worth checking because > > the loader mistakes it for EOF, reports success, and can discard the > > remaining records on the next save. > > > > Best regards, > > Haibo > > If this is defensive handling of a manually corrupted file, then the same concern may apply to many other text file readers. In that case, instead of adding a check specifically for the stash file, maybe a better way is to add a generic text file reader to handle all possible errors in a single place. > > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ > > > Hi Chao, Thanks for the suggestion. A shared helper could make sense if several readers need the same checks. Some parsers can detect NUL as they parse, so I'd avoid requiring a separate scan in a generic reader. The stash loader already reads the whole file into memory. A memchr() check on that buffer prevents it from treating an embedded NUL as EOF and accepting just the prefix. I'd keep the check here for now and revisit sharing it once we have another reader with the same needs. Best regards, Haibo