From 1c01ff9e173105c1c53772bf69fe661a638030af Mon Sep 17 00:00:00 2001 From: Shinya Kato Date: Wed, 25 Nov 2026 22:21:29 +0900 Subject: [PATCH v2 1/4] Zero the length word in pq_begintypsend() with a single store pq_begintypsend() reserved the length word with four appendStringInfoCharMacro() calls, each of which reads and updates the StringInfo length and checks for room. The buffer was just allocated with room to spare, so zero the four bytes with one memset() instead. Author: Andres Freund Author: Shinya Kato Reviewed-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/CAOzEurReWaX8i72CBbBzYZzNSP=XRQdp1=cB+rYhir1CB9DUUg@mail.gmail.com --- src/backend/libpq/pqformat.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/backend/libpq/pqformat.c b/src/backend/libpq/pqformat.c index 8b41aa4f1cb..c16e8365fcc 100644 --- a/src/backend/libpq/pqformat.c +++ b/src/backend/libpq/pqformat.c @@ -325,11 +325,16 @@ void pq_begintypsend(StringInfo buf) { initStringInfo(buf); - /* Reserve four bytes for the bytea length word */ - appendStringInfoCharMacro(buf, '\0'); - appendStringInfoCharMacro(buf, '\0'); - appendStringInfoCharMacro(buf, '\0'); - appendStringInfoCharMacro(buf, '\0'); + + /* + * Reserve space for the length word, which pq_endtypsend() fills in. It + * is zeroed so that a send function that never calls pq_endtypsend() + * produces a zero length, which callers reject. + */ + Assert(buf->maxlen >= VARHDRSZ + 1); + memset(buf->data, 0, VARHDRSZ); + buf->len = VARHDRSZ; + buf->data[VARHDRSZ] = '\0'; } /* -------------------------------- -- 2.47.3