Skip to content

Commit 0d8d0d0

Browse files
committed
Initialize padding bytes in btree_gist varbit support.
The code expands a varbit gist leaf key to a node key by copying the bit data twice in a varlen datum, as both the lower and upper key. The lower key was expanded to INTALIGN size, but the padding bytes were not initialized. That's a problem because when the lower/upper keys are compared, the padding bytes are used compared too, when the values are otherwise equal. That could lead to incorrect query results. REINDEX is advised for any btree_gist indexes on bit or bit varying data type, to fix any garbage padding bytes on disk. Per Valgrind, reported by Andres Freund. Backpatch to all supported versions.
1 parent ada2ff4 commit 0d8d0d0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

contrib/btree_gist/btree_bit.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ static bytea *
8383
gbt_bit_xfrm(bytea *leaf)
8484
{
8585
bytea *out = leaf;
86-
int s = INTALIGN(VARBITBYTES(leaf) + VARHDRSZ);
87-
88-
out = palloc(s);
89-
SET_VARSIZE(out, s);
86+
int sz = VARBITBYTES(leaf) + VARHDRSZ;
87+
int padded_sz = INTALIGN(sz);
88+
89+
out = (bytea *) palloc(padded_sz);
90+
/* initialize the padding bytes to zero */
91+
while (sz < padded_sz)
92+
((char *) out)[sz++] = 0;
93+
SET_VARSIZE(out, padded_sz);
9094
memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
9195
return out;
9296
}

0 commit comments

Comments
 (0)