]> granicus.if.org Git - postgresql/commitdiff
Fix valgrind warning for btree_gist indexes on macaddr.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 16 May 2014 19:11:51 +0000 (15:11 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 16 May 2014 19:11:51 +0000 (15:11 -0400)
The macaddr opclass stores two macaddr structs (each of size 6) in an
index column that's declared as being of type gbtreekey16, ie 16 bytes.
In the original coding this led to passing a palloc'd value of size 12
to the index insertion code, so that data would be fetched past the
end of the allocated value during index tuple construction.  This makes
valgrind unhappy.  In principle it could result in a SIGSEGV, though
with the current implementation of palloc there's no risk since
the 12-byte request size would be rounded up to 16 bytes anyway.

To fix, add a field to struct gbtree_ninfo showing the declared size of
the index datums, and use that in the palloc requests; and use palloc0
to be sure that any wasted bytes are cleanly initialized.

Per report from Andres Freund.  No back-patch since there's no current
risk of a real problem.

15 files changed:
contrib/btree_gist/btree_cash.c
contrib/btree_gist/btree_date.c
contrib/btree_gist/btree_float4.c
contrib/btree_gist/btree_float8.c
contrib/btree_gist/btree_inet.c
contrib/btree_gist/btree_int2.c
contrib/btree_gist/btree_int4.c
contrib/btree_gist/btree_int8.c
contrib/btree_gist/btree_interval.c
contrib/btree_gist/btree_macaddr.c
contrib/btree_gist/btree_oid.c
contrib/btree_gist/btree_time.c
contrib/btree_gist/btree_ts.c
contrib/btree_gist/btree_utils_num.c
contrib/btree_gist/btree_utils_num.h

index 8de3716c945ec34aa9837494352193548ce1900e..63f86ebeefe03e26a8ba85dff102f47055e8e253 100644 (file)
@@ -78,6 +78,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_cash,
        sizeof(Cash),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_cashgt,
        gbt_cashge,
        gbt_casheq,
index 9cab7ec42f5d17cd9a51d5195a1f088c5c5c1cd9..7a4c6aa6003fa05c6ab629bf2ae4f8035630eccc 100644 (file)
@@ -96,6 +96,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_date,
        sizeof(DateADT),
+       8,                                                      /* sizeof(gbtreekey8) */
        gbt_dategt,
        gbt_datege,
        gbt_dateeq,
index 55e1c4c1c6d676cefee4d3ac7af2e370b4b3c589..778d8dad84407c68fd90d567eaf26f3181c3373a 100644 (file)
@@ -77,6 +77,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_float4,
        sizeof(float4),
+       8,                                                      /* sizeof(gbtreekey8) */
        gbt_float4gt,
        gbt_float4ge,
        gbt_float4eq,
index 62271dec84aa94a9f8f9300743bdea2e7c556505..c898bf2d975ce26e4761d06a969654258e724133 100644 (file)
@@ -85,6 +85,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_float8,
        sizeof(float8),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_float8gt,
        gbt_float8ge,
        gbt_float8eq,
index 24ae6bf36935d0b467d7ed0fe48f2fa5d44228e0..822786125d99b12cef2aa5cb8f9db88c120e811a 100644 (file)
@@ -74,6 +74,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_inet,
        sizeof(double),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_inetgt,
        gbt_inetge,
        gbt_ineteq,
index d51ad0c2ab2efb7f1e1b00705f8650365e06addc..a88aae64537c32d2caeeb18d2d58da96eda5e2d9 100644 (file)
@@ -77,6 +77,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_int2,
        sizeof(int16),
+       4,                                                      /* sizeof(gbtreekey4) */
        gbt_int2gt,
        gbt_int2ge,
        gbt_int2eq,
index e7641f22847a975d7b4de7b9a75941d99887bd8d..889a5120783346855056ecc16603653678957058 100644 (file)
@@ -78,6 +78,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_int4,
        sizeof(int32),
+       8,                                                      /* sizeof(gbtreekey8) */
        gbt_int4gt,
        gbt_int4ge,
        gbt_int4eq,
index 8bc8cb5fdf7de08ff5ec2e759eb39bfd36b8227c..8685cee1760ac24eccdeca9a747c885bac79bf35 100644 (file)
@@ -78,6 +78,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_int8,
        sizeof(int64),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_int8gt,
        gbt_int8ge,
        gbt_int8eq,
index 93a341eb77e4abf9ac287a275379e532de39d809..68d80e8e0ae765b56c18c30b0f79a0754b46f0b6 100644 (file)
@@ -87,7 +87,9 @@ gbt_intv_dist(const void *a, const void *b)
 /*
  * INTERVALSIZE should be the actual size-on-disk of an Interval, as shown
  * in pg_type.  This might be less than sizeof(Interval) if the compiler
- * insists on adding alignment padding at the end of the struct.
+ * insists on adding alignment padding at the end of the struct.  (Note:
+ * this concern is obsolete with the current definition of Interval, but
+ * was real before a separate "day" field was added to it.)
  */
 #define INTERVALSIZE 16
 
@@ -95,6 +97,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_intv,
        sizeof(Interval),
+       32,                                                     /* sizeof(gbtreekey32) */
        gbt_intvgt,
        gbt_intvge,
        gbt_intveq,
index 6255564ac5abaaad828db3168dcec76c28a70144..244b95154bb7b7e137420755071d505a5c4653c4 100644 (file)
@@ -74,6 +74,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_macad,
        sizeof(macaddr),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_macadgt,
        gbt_macadge,
        gbt_macadeq,
index dcd0765417bf6833a974fcb03af1f7d96e64e50a..f6b7bfa05b729bc97d28a65446710f8390f83c26 100644 (file)
@@ -84,6 +84,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_oid,
        sizeof(Oid),
+       8,                                                      /* sizeof(gbtreekey8) */
        gbt_oidgt,
        gbt_oidge,
        gbt_oideq,
index e0e32428e22f1b96faac017a9ab9952c82f8afcc..cdf81711e76513022692fabd228a0e892b08d3f8 100644 (file)
@@ -124,6 +124,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_time,
        sizeof(TimeADT),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_timegt,
        gbt_timege,
        gbt_timeeq,
index 10f325d67202e77ca3ff7d6f7a01750e5d034406..a13dcc8beaa3d42a5c5dbc4f533c0d1cbca3189c 100644 (file)
@@ -127,6 +127,7 @@ static const gbtree_ninfo tinfo =
 {
        gbt_t_ts,
        sizeof(Timestamp),
+       16,                                                     /* sizeof(gbtreekey16) */
        gbt_tsgt,
        gbt_tsge,
        gbt_tseq,
index 5e52ab542bfe4f82ac57e31447ca2c18b2c30017..505633c98b815bf15c52e5254a64697fa6e959db 100644 (file)
@@ -28,7 +28,7 @@ gbt_num_compress(GISTENTRY *retval, GISTENTRY *entry, const gbtree_ninfo *tinfo)
                        Cash            ch;
                }                       v;
 
-               GBT_NUMKEY *r = (GBT_NUMKEY *) palloc0(2 * tinfo->size);
+               GBT_NUMKEY *r = (GBT_NUMKEY *) palloc0(tinfo->indexsize);
                void       *leaf = NULL;
 
                switch (tinfo->t)
@@ -77,6 +77,8 @@ gbt_num_compress(GISTENTRY *retval, GISTENTRY *entry, const gbtree_ninfo *tinfo)
                                leaf = DatumGetPointer(entry->key);
                }
 
+               Assert(tinfo->indexsize >= 2 * tinfo->size);
+
                memcpy((void *) &r[0], leaf, tinfo->size);
                memcpy((void *) &r[tinfo->size], leaf, tinfo->size);
                retval = palloc(sizeof(GISTENTRY));
@@ -165,7 +167,7 @@ gbt_num_bin_union(Datum *u, GBT_NUMKEY *e, const gbtree_ninfo *tinfo)
 
        if (!DatumGetPointer(*u))
        {
-               *u = PointerGetDatum(palloc(2 * tinfo->size));
+               *u = PointerGetDatum(palloc0(tinfo->indexsize));
                memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]), (void *) rd.lower, tinfo->size);
                memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]), (void *) rd.upper, tinfo->size);
        }
index d7a61d2242690ce00649f9a7eb4fedb9d820c3f0..0d79cd2a7f7c1d861d442fd6f381c94a5b952f01 100644 (file)
@@ -37,7 +37,8 @@ typedef struct
        /* Attribs */
 
        enum gbtree_type t;                     /* data type */
-       int32           size;                   /* size of type , 0 means variable */
+       int32           size;                   /* size of type, 0 means variable */
+       int32           indexsize;              /* size of datums stored in index */
 
        /* Methods */