PostgreSQL
Check-in [d05b3130ac]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix valgrind warning for btree_gist indexes on macaddr. 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.
Timelines: family | ancestors | descendants | both | trunk | WIN32_DEV | REL9_0_ALPHA4_BRANCH
Files: files | file ages | folders
SHA1:d05b3130ac86aa8491a4613922a431a8741ba42a
User & Date: tgl@sss.pgh.pa.us 2014-05-16 19:11:51
Context
2014-05-16
19:18
Fix a second cause of undersized pallocs for btree_gist indexes on macaddr. gbt_macad_union also allocated 12-byte structs where we really need 16. Per report from Andres Freund. No back-patch sinc... check-in: 9fae83a1b0 user: tgl@sss.pgh.pa.us tags: trunk, WIN32_DEV, REL9_0_ALPHA4_BRANCH
19:11
Fix valgrind warning for btree_gist indexes on macaddr. 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. I... check-in: d05b3130ac user: tgl@sss.pgh.pa.us tags: trunk, WIN32_DEV, REL9_0_ALPHA4_BRANCH
08:34
Add test case for logical decoding of prepared transactions. Andres Freund check-in: 96f4eeef2c user: heikki.linnakangas@iki.fi tags: trunk, WIN32_DEV, REL9_0_ALPHA4_BRANCH
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to contrib/btree_gist/btree_cash.c.

74
75
76
77
78
79
80

81
82
83
84
85
86
87
}


static const gbtree_ninfo tinfo =
{
	gbt_t_cash,
	sizeof(Cash),

	gbt_cashgt,
	gbt_cashge,
	gbt_casheq,
	gbt_cashle,
	gbt_cashlt,
	gbt_cashkey_cmp,
	gbt_cash_dist







>







74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}


static const gbtree_ninfo tinfo =
{
	gbt_t_cash,
	sizeof(Cash),
	16,							/* sizeof(gbtreekey16) */
	gbt_cashgt,
	gbt_cashge,
	gbt_casheq,
	gbt_cashle,
	gbt_cashlt,
	gbt_cashkey_cmp,
	gbt_cash_dist

Changes to contrib/btree_gist/btree_date.c.

92
93
94
95
96
97
98

99
100
101
102
103
104
105
}


static const gbtree_ninfo tinfo =
{
	gbt_t_date,
	sizeof(DateADT),

	gbt_dategt,
	gbt_datege,
	gbt_dateeq,
	gbt_datele,
	gbt_datelt,
	gbt_datekey_cmp,
	gdb_date_dist







>







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}


static const gbtree_ninfo tinfo =
{
	gbt_t_date,
	sizeof(DateADT),
	8,							/* sizeof(gbtreekey8) */
	gbt_dategt,
	gbt_datege,
	gbt_dateeq,
	gbt_datele,
	gbt_datelt,
	gbt_datekey_cmp,
	gdb_date_dist

Changes to contrib/btree_gist/btree_float4.c.

73
74
75
76
77
78
79

80
81
82
83
84
85
86
}


static const gbtree_ninfo tinfo =
{
	gbt_t_float4,
	sizeof(float4),

	gbt_float4gt,
	gbt_float4ge,
	gbt_float4eq,
	gbt_float4le,
	gbt_float4lt,
	gbt_float4key_cmp,
	gbt_float4_dist







>







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}


static const gbtree_ninfo tinfo =
{
	gbt_t_float4,
	sizeof(float4),
	8,							/* sizeof(gbtreekey8) */
	gbt_float4gt,
	gbt_float4ge,
	gbt_float4eq,
	gbt_float4le,
	gbt_float4lt,
	gbt_float4key_cmp,
	gbt_float4_dist

Changes to contrib/btree_gist/btree_float8.c.

81
82
83
84
85
86
87

88
89
90
91
92
93
94
}


static const gbtree_ninfo tinfo =
{
	gbt_t_float8,
	sizeof(float8),

	gbt_float8gt,
	gbt_float8ge,
	gbt_float8eq,
	gbt_float8le,
	gbt_float8lt,
	gbt_float8key_cmp,
	gbt_float8_dist







>







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
}


static const gbtree_ninfo tinfo =
{
	gbt_t_float8,
	sizeof(float8),
	16,							/* sizeof(gbtreekey16) */
	gbt_float8gt,
	gbt_float8ge,
	gbt_float8eq,
	gbt_float8le,
	gbt_float8lt,
	gbt_float8key_cmp,
	gbt_float8_dist

Changes to contrib/btree_gist/btree_inet.c.

70
71
72
73
74
75
76

77
78
79
80
81
82
83
}


static const gbtree_ninfo tinfo =
{
	gbt_t_inet,
	sizeof(double),

	gbt_inetgt,
	gbt_inetge,
	gbt_ineteq,
	gbt_inetle,
	gbt_inetlt,
	gbt_inetkey_cmp,
	NULL







>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
}


static const gbtree_ninfo tinfo =
{
	gbt_t_inet,
	sizeof(double),
	16,							/* sizeof(gbtreekey16) */
	gbt_inetgt,
	gbt_inetge,
	gbt_ineteq,
	gbt_inetle,
	gbt_inetlt,
	gbt_inetkey_cmp,
	NULL

Changes to contrib/btree_gist/btree_int2.c.

73
74
75
76
77
78
79

80
81
82
83
84
85
86
}


static const gbtree_ninfo tinfo =
{
	gbt_t_int2,
	sizeof(int16),

	gbt_int2gt,
	gbt_int2ge,
	gbt_int2eq,
	gbt_int2le,
	gbt_int2lt,
	gbt_int2key_cmp,
	gbt_int2_dist







>







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}


static const gbtree_ninfo tinfo =
{
	gbt_t_int2,
	sizeof(int16),
	4,							/* sizeof(gbtreekey4) */
	gbt_int2gt,
	gbt_int2ge,
	gbt_int2eq,
	gbt_int2le,
	gbt_int2lt,
	gbt_int2key_cmp,
	gbt_int2_dist

Changes to contrib/btree_gist/btree_int4.c.

74
75
76
77
78
79
80

81
82
83
84
85
86
87
}


static const gbtree_ninfo tinfo =
{
	gbt_t_int4,
	sizeof(int32),

	gbt_int4gt,
	gbt_int4ge,
	gbt_int4eq,
	gbt_int4le,
	gbt_int4lt,
	gbt_int4key_cmp,
	gbt_int4_dist







>







74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}


static const gbtree_ninfo tinfo =
{
	gbt_t_int4,
	sizeof(int32),
	8,							/* sizeof(gbtreekey8) */
	gbt_int4gt,
	gbt_int4ge,
	gbt_int4eq,
	gbt_int4le,
	gbt_int4lt,
	gbt_int4key_cmp,
	gbt_int4_dist

Changes to contrib/btree_gist/btree_int8.c.

74
75
76
77
78
79
80

81
82
83
84
85
86
87
}


static const gbtree_ninfo tinfo =
{
	gbt_t_int8,
	sizeof(int64),

	gbt_int8gt,
	gbt_int8ge,
	gbt_int8eq,
	gbt_int8le,
	gbt_int8lt,
	gbt_int8key_cmp,
	gbt_int8_dist







>







74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}


static const gbtree_ninfo tinfo =
{
	gbt_t_int8,
	sizeof(int64),
	16,							/* sizeof(gbtreekey16) */
	gbt_int8gt,
	gbt_int8ge,
	gbt_int8eq,
	gbt_int8le,
	gbt_int8lt,
	gbt_int8key_cmp,
	gbt_int8_dist

Changes to contrib/btree_gist/btree_interval.c.

83
84
85
86
87
88
89
90


91
92
93
94
95
96
97

98
99
100
101
102
103
104
{
	return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) 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.


 */
#define INTERVALSIZE 16

static const gbtree_ninfo tinfo =
{
	gbt_t_intv,
	sizeof(Interval),

	gbt_intvgt,
	gbt_intvge,
	gbt_intveq,
	gbt_intvle,
	gbt_intvlt,
	gbt_intvkey_cmp,
	gbt_intv_dist







|
>
>







>







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
	return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) 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.  (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

static const gbtree_ninfo tinfo =
{
	gbt_t_intv,
	sizeof(Interval),
	32,							/* sizeof(gbtreekey32) */
	gbt_intvgt,
	gbt_intvge,
	gbt_intveq,
	gbt_intvle,
	gbt_intvlt,
	gbt_intvkey_cmp,
	gbt_intv_dist

Changes to contrib/btree_gist/btree_macaddr.c.

70
71
72
73
74
75
76

77
78
79
80
81
82
83
}


static const gbtree_ninfo tinfo =
{
	gbt_t_macad,
	sizeof(macaddr),

	gbt_macadgt,
	gbt_macadge,
	gbt_macadeq,
	gbt_macadle,
	gbt_macadlt,
	gbt_macadkey_cmp,
	NULL







>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
}


static const gbtree_ninfo tinfo =
{
	gbt_t_macad,
	sizeof(macaddr),
	16,							/* sizeof(gbtreekey16) */
	gbt_macadgt,
	gbt_macadge,
	gbt_macadeq,
	gbt_macadle,
	gbt_macadlt,
	gbt_macadkey_cmp,
	NULL

Changes to contrib/btree_gist/btree_oid.c.

80
81
82
83
84
85
86

87
88
89
90
91
92
93
}


static const gbtree_ninfo tinfo =
{
	gbt_t_oid,
	sizeof(Oid),

	gbt_oidgt,
	gbt_oidge,
	gbt_oideq,
	gbt_oidle,
	gbt_oidlt,
	gbt_oidkey_cmp,
	gbt_oid_dist







>







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}


static const gbtree_ninfo tinfo =
{
	gbt_t_oid,
	sizeof(Oid),
	8,							/* sizeof(gbtreekey8) */
	gbt_oidgt,
	gbt_oidge,
	gbt_oideq,
	gbt_oidle,
	gbt_oidlt,
	gbt_oidkey_cmp,
	gbt_oid_dist

Changes to contrib/btree_gist/btree_time.c.

120
121
122
123
124
125
126

127
128
129
130
131
132
133
}


static const gbtree_ninfo tinfo =
{
	gbt_t_time,
	sizeof(TimeADT),

	gbt_timegt,
	gbt_timege,
	gbt_timeeq,
	gbt_timele,
	gbt_timelt,
	gbt_timekey_cmp,
	gbt_time_dist







>







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
}


static const gbtree_ninfo tinfo =
{
	gbt_t_time,
	sizeof(TimeADT),
	16,							/* sizeof(gbtreekey16) */
	gbt_timegt,
	gbt_timege,
	gbt_timeeq,
	gbt_timele,
	gbt_timelt,
	gbt_timekey_cmp,
	gbt_time_dist

Changes to contrib/btree_gist/btree_ts.c.

123
124
125
126
127
128
129

130
131
132
133
134
135
136
}


static const gbtree_ninfo tinfo =
{
	gbt_t_ts,
	sizeof(Timestamp),

	gbt_tsgt,
	gbt_tsge,
	gbt_tseq,
	gbt_tsle,
	gbt_tslt,
	gbt_tskey_cmp,
	gbt_ts_dist







>







123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
}


static const gbtree_ninfo tinfo =
{
	gbt_t_ts,
	sizeof(Timestamp),
	16,							/* sizeof(gbtreekey16) */
	gbt_tsgt,
	gbt_tsge,
	gbt_tseq,
	gbt_tsle,
	gbt_tslt,
	gbt_tskey_cmp,
	gbt_ts_dist

Changes to contrib/btree_gist/btree_utils_num.c.

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
72
73
74
75
76
77
78


79
80
81
82
83
84
85
...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
			float8		f8;
			DateADT		dt;
			TimeADT		tm;
			Timestamp	ts;
			Cash		ch;
		}			v;

		GBT_NUMKEY *r = (GBT_NUMKEY *) palloc0(2 * tinfo->size);
		void	   *leaf = NULL;

		switch (tinfo->t)
		{
			case gbt_t_int2:
				v.i2 = DatumGetInt16(entry->key);
				leaf = &v.i2;
................................................................................
			case gbt_t_cash:
				v.ch = DatumGetCash(entry->key);
				leaf = &v.ch;
				break;
			default:
				leaf = DatumGetPointer(entry->key);
		}



		memcpy((void *) &r[0], leaf, tinfo->size);
		memcpy((void *) &r[tinfo->size], leaf, tinfo->size);
		retval = palloc(sizeof(GISTENTRY));
		gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page,
					  entry->offset, FALSE);
	}
................................................................................
	GBT_NUMKEY_R rd;

	rd.lower = &e[0];
	rd.upper = &e[tinfo->size];

	if (!DatumGetPointer(*u))
	{
		*u = PointerGetDatum(palloc(2 * tinfo->size));
		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);
	}
	else
	{
		GBT_NUMKEY_R ur;








|







 







>
>







 







|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
			float8		f8;
			DateADT		dt;
			TimeADT		tm;
			Timestamp	ts;
			Cash		ch;
		}			v;

		GBT_NUMKEY *r = (GBT_NUMKEY *) palloc0(tinfo->indexsize);
		void	   *leaf = NULL;

		switch (tinfo->t)
		{
			case gbt_t_int2:
				v.i2 = DatumGetInt16(entry->key);
				leaf = &v.i2;
................................................................................
			case gbt_t_cash:
				v.ch = DatumGetCash(entry->key);
				leaf = &v.ch;
				break;
			default:
				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));
		gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page,
					  entry->offset, FALSE);
	}
................................................................................
	GBT_NUMKEY_R rd;

	rd.lower = &e[0];
	rd.upper = &e[tinfo->size];

	if (!DatumGetPointer(*u))
	{
		*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);
	}
	else
	{
		GBT_NUMKEY_R ur;

Changes to contrib/btree_gist/btree_utils_num.h.

33
34
35
36
37
38
39
40

41
42
43
44
45
46
47

typedef struct
{

	/* Attribs */

	enum gbtree_type t;			/* data type */
	int32		size;			/* size of type , 0 means variable */


	/* Methods */

	bool		(*f_gt) (const void *, const void *);	/* greater than */
	bool		(*f_ge) (const void *, const void *);	/* greater or equal */
	bool		(*f_eq) (const void *, const void *);	/* equal */
	bool		(*f_le) (const void *, const void *);	/* less or equal */







|
>







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

typedef struct
{

	/* Attribs */

	enum gbtree_type t;			/* data type */
	int32		size;			/* size of type, 0 means variable */
	int32		indexsize;		/* size of datums stored in index */

	/* Methods */

	bool		(*f_gt) (const void *, const void *);	/* greater than */
	bool		(*f_ge) (const void *, const void *);	/* greater or equal */
	bool		(*f_eq) (const void *, const void *);	/* equal */
	bool		(*f_le) (const void *, const void *);	/* less or equal */