aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>2014-05-13 14:16:28 +0300
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>2014-05-13 15:27:14 +0300
commitd5b912c905efd531a6228d08c98cd92d49cdd94c (patch)
tree5e90a778d6ac6f0bdcf1094e3de7a26ab18c6e6e
parentb65a258a512049eba567283d93bd63b22b77916a (diff)
downloadpostgresql-d5b912c905efd531a6228d08c98cd92d49cdd94c.tar.gz
postgresql-d5b912c905efd531a6228d08c98cd92d49cdd94c.zip
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.
-rw-r--r--contrib/btree_gist/btree_bit.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/contrib/btree_gist/btree_bit.c b/contrib/btree_gist/btree_bit.c
index d94abcb3cf5..94eaf6df3e8 100644
--- a/contrib/btree_gist/btree_bit.c
+++ b/contrib/btree_gist/btree_bit.c
@@ -83,10 +83,14 @@ static bytea *
gbt_bit_xfrm(bytea *leaf)
{
bytea *out = leaf;
- int s = INTALIGN(VARBITBYTES(leaf) + VARHDRSZ);
-
- out = palloc(s);
- SET_VARSIZE(out, s);
+ int sz = VARBITBYTES(leaf) + VARHDRSZ;
+ int padded_sz = INTALIGN(sz);
+
+ out = (bytea *) palloc(padded_sz);
+ /* initialize the padding bytes to zero */
+ while (sz < padded_sz)
+ ((char *) out)[sz++] = 0;
+ SET_VARSIZE(out, padded_sz);
memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
return out;
}