Skip to content

translate: read by-value DXL datums without over-reading the payload - #14

Merged
yjhjstz merged 1 commit into
mainfrom
fix/dxl-byval-datum-overread
Aug 25, 2026
Merged

translate: read by-value DXL datums without over-reading the payload#14
yjhjstz merged 1 commit into
mainfrom
fix/dxl-byval-datum-overread

Conversation

@yjhjstz

@yjhjstz yjhjstz commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

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.

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.
@cr-gpt

cr-gpt Bot commented Aug 25, 2026

Copy link
Copy Markdown

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

@yjhjstz
yjhjstz merged commit 36a64fd into main Aug 25, 2026
9 checks passed
@yjhjstz
yjhjstz deleted the fix/dxl-byval-datum-overread branch August 25, 2026 16:52
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.

1 participant