translate: read by-value DXL datums without over-reading the payload - #14
Merged
Conversation
TranslateDXLDatumGenericToScalar() copied sizeof(Datum) bytes out of a CDXLDatumGeneric payload that only holds typlen bytes, so every by-value type narrower than a Datum -- date, float4, "char", xid, cid, the reg* types and user enums -- picked up memory past the end of that buffer and dropped it into the high half of Const->constvalue. With the palloc-backed gpos pool the read lands in the alignment padding of the surrounding chunk, so under an assert build the constant for DATE '2026-03-01' came out as 0x7f7f7f7f00002554 (the 0x7f bytes being CLOBBER_FREED_MEMORY fill); "char", at one payload byte, drags in seven foreign bytes. Read exactly the payload with fetch_att() instead. Besides bounding the read it widens the value the way PostgreSQL does when it builds such a Const itself: Int32GetDatum() and friends sign-extend, so DATE '1990-01-01' yields 0xfffffffffffff1bc, matching date_in(). A zero-extending copy of constlen bytes would also stop the over-read but produce a non-canonical Datum that no longer compares equal to a PG-built constant of the same value under datumIsEqual(), which is what _equalConst() uses for by-value types. The payload length is validated against typlen up front, so a DXL datum that disagrees with the metadata raises a conversion error and falls back rather than reading out of bounds in a release build. The TVF-evaluates-to-const path in CTranslatorDXLToPlStmt carried its own copy of this code that additionally assumed every datum is generic and passed by reference, leaving constbyval unset for by-value types and mis-casting int4, bool and oid datums. Route it through the scalar translator, as every other Const site in that file already does. Found via warehouse-pg/warehouse-pg#114, which fixes the same over-read with a zero-extending memcpy in a CTranslatorUtils helper; this takes a different approach, so expect a conflict when that commit is merged.
|
Seems you are using me but didn't get OPENAI_API_KEY seted in Variables/Secrets for this repo. you could follow readme for more information |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TranslateDXLDatumGenericToScalar() copied sizeof(Datum) bytes out of a CDXLDatumGeneric payload that only holds typlen bytes, so every by-value type narrower than a Datum -- date, float4, "char", xid, cid, the reg* types and user enums -- picked up memory past the end of that buffer and dropped it into the high half of Const->constvalue. With the palloc-backed gpos pool the read lands in the alignment padding of the surrounding chunk, so under an assert build the constant for DATE '2026-03-01' came out as 0x7f7f7f7f00002554 (the 0x7f bytes being CLOBBER_FREED_MEMORY fill); "char", at one payload byte, drags in seven foreign bytes.
Read exactly the payload with fetch_att() instead. Besides bounding the read it widens the value the way PostgreSQL does when it builds such a Const itself: Int32GetDatum() and friends sign-extend, so DATE '1990-01-01' yields 0xfffffffffffff1bc, matching date_in(). A zero-extending copy of constlen bytes would also stop the over-read but produce a non-canonical Datum that no longer compares equal to a PG-built constant of the same value under datumIsEqual(), which is what _equalConst() uses for by-value types. The payload length is validated against typlen up front, so a DXL datum that disagrees with the metadata raises a conversion error and falls back rather than reading out of bounds in a release build.
The TVF-evaluates-to-const path in CTranslatorDXLToPlStmt carried its own copy of this code that additionally assumed every datum is generic and passed by reference, leaving constbyval unset for by-value types and mis-casting int4, bool and oid datums. Route it through the scalar translator, as every other Const site in that file already does.
Found via warehouse-pg/warehouse-pg#114, which fixes the same over-read with a zero-extending memcpy in a CTranslatorUtils helper; this takes a different approach, so expect a conflict when that commit is merged.