Skip to content

Commit 1913d0f

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 c2a4bb3 commit 1913d0f

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
@@ -81,10 +81,14 @@ static bytea *
8181
gbt_bit_xfrm(bytea *leaf)
8282
{
8383
bytea *out = leaf;
84-
int s = INTALIGN(VARBITBYTES(leaf) + VARHDRSZ);
85-
86-
out = palloc(s);
87-
SET_VARSIZE(out, s);
84+
int sz = VARBITBYTES(leaf) + VARHDRSZ;
85+
int padded_sz = INTALIGN(sz);
86+
87+
out = (bytea *) palloc(padded_sz);
88+
/* initialize the padding bytes to zero */
89+
while (sz < padded_sz)
90+
((char *) out)[sz++] = 0;
91+
SET_VARSIZE(out, padded_sz);
8892
memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
8993
return out;
9094
}

0 commit comments

Comments
 (0)