Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9f652d4

Browse files
committed
Fix up several contrib modules that were using varlena datatypes in not-so-obvious
ways. I'm not totally sure that I caught everything, but at least now they pass their regression tests with VARSIZE/SET_VARSIZE defined to reverse byte order.
1 parent d1ce4f7 commit 9f652d4

24 files changed

+152
-147
lines changed

contrib/hstore/hstore.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __HSTORE_H__
33

44
#include "postgres.h"
5+
56
#include "funcapi.h"
67
#include "access/gist.h"
78
#include "access/itup.h"
@@ -23,12 +24,12 @@ typedef struct
2324

2425
typedef struct
2526
{
26-
int4 len;
27+
int32 vl_len_; /* varlena header (do not touch directly!) */
2728
int4 size;
2829
char data[1];
2930
} HStore;
3031

31-
#define HSHRDSIZE (2*sizeof(int4))
32+
#define HSHRDSIZE (VARHDRSZ + sizeof(int4))
3233
#define CALCDATASIZE(x, lenstr) ( (x) * sizeof(HEntry) + HSHRDSIZE + (lenstr) )
3334
#define ARRPTR(x) ( (HEntry*) ( (char*)(x) + HSHRDSIZE ) )
3435
#define STRPTR(x) ( (char*)(x) + HSHRDSIZE + ( sizeof(HEntry) * ((HStore*)x)->size ) )

contrib/hstore/hstore_gist.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "access/gist.h"
44
#include "access/itup.h"
5-
/*#include "access/rtree.h"*/
65
#include "crc32.h"
76

87
/* bigint defines */
@@ -38,7 +37,7 @@ typedef char *BITVECP;
3837

3938
typedef struct
4039
{
41-
int4 len;
40+
int32 vl_len_; /* varlena header (do not touch directly!) */
4241
int4 flag;
4342
char data[1];
4443
} GISTTYPE;
@@ -47,7 +46,7 @@ typedef struct
4746

4847
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
4948

50-
#define GTHDRSIZE ( sizeof(int4)*2 )
49+
#define GTHDRSIZE (VARHDRSZ + sizeof(int4))
5150
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
5251

5352
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
@@ -112,14 +111,13 @@ ghstore_compress(PG_FUNCTION_ARGS)
112111

113112
if (entry->leafkey)
114113
{
115-
GISTTYPE *res = (GISTTYPE *) palloc(CALCGTSIZE(0));
114+
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
116115
HStore *toastedval = (HStore *) DatumGetPointer(entry->key);
117116
HStore *val = (HStore *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
118117
HEntry *ptr = ARRPTR(val);
119118
char *words = STRPTR(val);
120119

121-
memset(res, 0, CALCGTSIZE(0));
122-
res->len = CALCGTSIZE(0);
120+
SET_VARSIZE(res, CALCGTSIZE(0));
123121

124122
while (ptr - ARRPTR(val) < val->size)
125123
{
@@ -156,7 +154,7 @@ ghstore_compress(PG_FUNCTION_ARGS)
156154
);
157155

158156
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
159-
res->len = CALCGTSIZE(ALLISTRUE);
157+
SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
160158
res->flag = ALLISTRUE;
161159

162160
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
@@ -286,10 +284,11 @@ ghstore_union(PG_FUNCTION_ARGS)
286284

287285
len = CALCGTSIZE(flag);
288286
result = (GISTTYPE *) palloc(len);
289-
*size = result->len = len;
287+
SET_VARSIZE(result, len);
290288
result->flag = flag;
291289
if (!ISALLTRUE(result))
292290
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
291+
*size = len;
293292

294293
PG_RETURN_POINTER(result);
295294
}
@@ -383,27 +382,27 @@ ghstore_picksplit(PG_FUNCTION_ARGS)
383382
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
384383
{
385384
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
386-
datum_l->len = GTHDRSIZE;
385+
SET_VARSIZE(datum_l, GTHDRSIZE);
387386
datum_l->flag = ALLISTRUE;
388387
}
389388
else
390389
{
391390
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
392-
datum_l->len = GTHDRSIZE + SIGLEN;
391+
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
393392
datum_l->flag = 0;
394393
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC))
395394
;
396395
}
397396
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
398397
{
399398
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
400-
datum_r->len = GTHDRSIZE;
399+
SET_VARSIZE(datum_r, GTHDRSIZE);
401400
datum_r->flag = ALLISTRUE;
402401
}
403402
else
404403
{
405404
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
406-
datum_r->len = GTHDRSIZE + SIGLEN;
405+
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
407406
datum_r->flag = 0;
408407
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
409408
}

contrib/hstore/hstore_io.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ hstore_in(PG_FUNCTION_ARGS)
363363
freeHSParse(&state);
364364
len = CALCDATASIZE(0, 0);
365365
out = palloc(len);
366-
out->len = len;
366+
SET_VARSIZE(out, len);
367367
out->size = 0;
368368
PG_RETURN_POINTER(out);
369369
}
@@ -372,7 +372,7 @@ hstore_in(PG_FUNCTION_ARGS)
372372

373373
len = CALCDATASIZE(state.pcur, buflen);
374374
out = palloc(len);
375-
out->len = len;
375+
SET_VARSIZE(out, len);
376376
out->size = state.pcur;
377377

378378
entries = ARRPTR(out);
@@ -436,7 +436,7 @@ hstore_out(PG_FUNCTION_ARGS)
436436
}
437437

438438
buflen = (4 /* " */ + 2 /* => */ + 2 /* , */ ) * in->size +
439-
2 /* esc */ * (in->len - CALCDATASIZE(in->size, 0));
439+
2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0));
440440

441441
out = ptr = palloc(buflen);
442442
for (i = 0; i < in->size; i++)

contrib/hstore/hstore_op.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ delete(PG_FUNCTION_ARGS)
105105
{
106106
HStore *hs = PG_GETARG_HS(0);
107107
text *key = PG_GETARG_TEXT_P(1);
108-
HStore *out = palloc(hs->len);
108+
HStore *out = palloc(VARSIZE(hs));
109109
char *ptrs,
110110
*ptrd;
111111
HEntry *es,
112112
*ed;
113113

114-
out->len = hs->len;
114+
SET_VARSIZE(out, VARSIZE(hs));
115115
out->size = hs->size; /* temporary! */
116116

117117
ptrs = STRPTR(hs);
@@ -142,7 +142,7 @@ delete(PG_FUNCTION_ARGS)
142142
out->size = ed - ARRPTR(out);
143143

144144
memmove(STRPTR(out), ptrd, buflen);
145-
out->len = CALCDATASIZE(out->size, buflen);
145+
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
146146
}
147147

148148

@@ -159,15 +159,15 @@ hs_concat(PG_FUNCTION_ARGS)
159159
{
160160
HStore *s1 = PG_GETARG_HS(0);
161161
HStore *s2 = PG_GETARG_HS(1);
162-
HStore *out = palloc(s1->len + s2->len);
162+
HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
163163
char *ps1,
164164
*ps2,
165165
*pd;
166166
HEntry *es1,
167167
*es2,
168168
*ed;
169169

170-
out->len = s1->len + s2->len;
170+
SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2));
171171
out->size = s1->size + s2->size;
172172

173173
ps1 = STRPTR(s1);
@@ -256,7 +256,7 @@ hs_concat(PG_FUNCTION_ARGS)
256256
out->size = ed - ARRPTR(out);
257257

258258
memmove(STRPTR(out), pd, buflen);
259-
out->len = CALCDATASIZE(out->size, buflen);
259+
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
260260
}
261261

262262
PG_FREE_IF_COPY(s1, 0);
@@ -277,7 +277,7 @@ tconvert(PG_FUNCTION_ARGS)
277277

278278
len = CALCDATASIZE(1, VARSIZE(key) + VARSIZE(val) - 2 * VARHDRSZ);
279279
out = palloc(len);
280-
out->len = len;
280+
SET_VARSIZE(out, len);
281281
out->size = 1;
282282

283283
ARRPTR(out)->keylen = VARSIZE(key) - VARHDRSZ;
@@ -399,8 +399,8 @@ setup_firstcall(FuncCallContext *funcctx, HStore * hs)
399399

400400
st = (AKStore *) palloc(sizeof(AKStore));
401401
st->i = 0;
402-
st->hs = (HStore *) palloc(hs->len);
403-
memcpy(st->hs, hs, hs->len);
402+
st->hs = (HStore *) palloc(VARSIZE(hs));
403+
memcpy(st->hs, hs, VARSIZE(hs));
404404

405405
funcctx->user_fctx = (void *) st;
406406
MemoryContextSwitchTo(oldcontext);
@@ -568,8 +568,8 @@ each(PG_FUNCTION_ARGS)
568568
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
569569
st = (AKStore *) palloc(sizeof(AKStore));
570570
st->i = 0;
571-
st->hs = (HStore *) palloc(hs->len);
572-
memcpy(st->hs, hs, hs->len);
571+
st->hs = (HStore *) palloc(VARSIZE(hs));
572+
memcpy(st->hs, hs, VARSIZE(hs));
573573
funcctx->user_fctx = (void *) st;
574574

575575
tupdesc = RelationNameGetTupleDesc("hs_each");

contrib/intarray/_int.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ typedef char *BITVECP;
8585
*/
8686
typedef struct
8787
{
88-
int4 len;
88+
int32 vl_len_; /* varlena header (do not touch directly!) */
8989
int4 flag;
9090
char data[1];
9191
} GISTTYPE;
@@ -94,7 +94,7 @@ typedef struct
9494

9595
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
9696

97-
#define GTHDRSIZE ( sizeof(int4)*2 )
97+
#define GTHDRSIZE (VARHDRSZ + sizeof(int4))
9898
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
9999

100100
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
@@ -145,12 +145,12 @@ typedef struct ITEM
145145

146146
typedef struct
147147
{
148-
int4 len;
148+
int32 vl_len_; /* varlena header (do not touch directly!) */
149149
int4 size;
150150
char data[1];
151151
} QUERYTYPE;
152152

153-
#define HDRSIZEQT ( 2*sizeof(int4) )
153+
#define HDRSIZEQT (VARHDRSZ + sizeof(int4))
154154
#define COMPUTESIZE(size) ( HDRSIZEQT + size * sizeof(ITEM) )
155155
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
156156

contrib/intarray/_int_bool.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ bqarr_in(PG_FUNCTION_ARGS)
465465

466466
commonlen = COMPUTESIZE(state.num);
467467
query = (QUERYTYPE *) palloc(commonlen);
468-
query->len = commonlen;
468+
SET_VARSIZE(query, commonlen);
469469
query->size = state.num;
470470
ptr = GETQUERY(query);
471471

contrib/intarray/_intbig_gist.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ g_intbig_compress(PG_FUNCTION_ARGS)
147147
ArrayType *in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
148148
int4 *ptr;
149149
int num;
150-
GISTTYPE *res = (GISTTYPE *) palloc(CALCGTSIZE(0));
150+
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
151151

152152
CHECKARRVALID(in);
153153
if (ARRISVOID(in))
@@ -160,8 +160,7 @@ g_intbig_compress(PG_FUNCTION_ARGS)
160160
ptr = ARRPTR(in);
161161
num = ARRNELEMS(in);
162162
}
163-
memset(res, 0, CALCGTSIZE(0));
164-
res->len = CALCGTSIZE(0);
163+
SET_VARSIZE(res, CALCGTSIZE(0));
165164

166165
while (num--)
167166
{
@@ -192,7 +191,7 @@ g_intbig_compress(PG_FUNCTION_ARGS)
192191
);
193192

194193
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
195-
res->len = CALCGTSIZE(ALLISTRUE);
194+
SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
196195
res->flag = ALLISTRUE;
197196

198197
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
@@ -292,10 +291,11 @@ g_intbig_union(PG_FUNCTION_ARGS)
292291

293292
len = CALCGTSIZE(flag);
294293
result = (GISTTYPE *) palloc(len);
295-
*size = result->len = len;
294+
SET_VARSIZE(result, len);
296295
result->flag = flag;
297296
if (!ISALLTRUE(result))
298297
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
298+
*size = len;
299299

300300
PG_RETURN_POINTER(result);
301301
}
@@ -389,26 +389,26 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
389389
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
390390
{
391391
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
392-
datum_l->len = GTHDRSIZE;
392+
SET_VARSIZE(datum_l, GTHDRSIZE);
393393
datum_l->flag = ALLISTRUE;
394394
}
395395
else
396396
{
397397
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
398-
datum_l->len = GTHDRSIZE + SIGLEN;
398+
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
399399
datum_l->flag = 0;
400400
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC));
401401
}
402402
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
403403
{
404404
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
405-
datum_r->len = GTHDRSIZE;
405+
SET_VARSIZE(datum_r, GTHDRSIZE);
406406
datum_r->flag = ALLISTRUE;
407407
}
408408
else
409409
{
410410
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
411-
datum_r->len = GTHDRSIZE + SIGLEN;
411+
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
412412
datum_r->flag = 0;
413413
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
414414
}

0 commit comments

Comments
 (0)