Skip to content

Commit 433853e

Browse files
committed
Fix array- and path-creating functions to ensure padding bytes are zeroes.
Per recent discussion, it's important for all computed datums (not only the results of input functions) to not contain any ill-defined (uninitialized) bits. Failing to ensure that can result in equal() reporting that semantically indistinguishable Consts are not equal, which in turn leads to bizarre and undesirable planner behavior, such as in a recent example from David Johnston. We might eventually try to fix this in a general manner by allowing datatypes to define identity-testing functions, but for now the path of least resistance is to expect datatypes to force all unused bits into consistent states. Per some testing by Noah Misch, array and path functions seem to be the only ones presenting risks at the moment, so I looked through all the functions in adt/array*.c and geo_ops.c and fixed them as necessary. In the array functions, the easiest/safest fix is to allocate result arrays with palloc0 instead of palloc. Possibly in future someone will want to look into whether we can just zero the padding bytes, but that looks too complex for a back-patchable fix. In the path functions, we already had a precedent in path_in for just zeroing the one known pad field, so duplicate that code as needed. Back-patch to all supported branches.
1 parent af9fb26 commit 433853e

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/backend/utils/adt/array_userfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ array_cat(PG_FUNCTION_ARGS)
375375
dataoffset = 0; /* marker for no null bitmap */
376376
nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
377377
}
378-
result = (ArrayType *) palloc(nbytes);
378+
result = (ArrayType *) palloc0(nbytes);
379379
SET_VARSIZE(result, nbytes);
380380
result->ndim = ndims;
381381
result->dataoffset = dataoffset;

src/backend/utils/adt/arrayfuncs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ array_recv(PG_FUNCTION_ARGS)
13141314
dataoffset = 0; /* marker for no null bitmap */
13151315
nbytes += ARR_OVERHEAD_NONULLS(ndim);
13161316
}
1317-
retval = (ArrayType *) palloc(nbytes);
1317+
retval = (ArrayType *) palloc0(nbytes);
13181318
SET_VARSIZE(retval, nbytes);
13191319
retval->ndim = ndim;
13201320
retval->dataoffset = dataoffset;
@@ -1952,7 +1952,7 @@ array_get_slice(ArrayType *array,
19521952
bytes += ARR_OVERHEAD_NONULLS(ndim);
19531953
}
19541954

1955-
newarray = (ArrayType *) palloc(bytes);
1955+
newarray = (ArrayType *) palloc0(bytes);
19561956
SET_VARSIZE(newarray, bytes);
19571957
newarray->ndim = ndim;
19581958
newarray->dataoffset = dataoffset;
@@ -2205,7 +2205,7 @@ array_set(ArrayType *array,
22052205
/*
22062206
* OK, create the new array and fill in header/dimensions
22072207
*/
2208-
newarray = (ArrayType *) palloc(newsize);
2208+
newarray = (ArrayType *) palloc0(newsize);
22092209
SET_VARSIZE(newarray, newsize);
22102210
newarray->ndim = ndim;
22112211
newarray->dataoffset = newhasnulls ? overheadlen : 0;
@@ -2535,7 +2535,7 @@ array_set_slice(ArrayType *array,
25352535

25362536
newsize = overheadlen + olddatasize - olditemsize + newitemsize;
25372537

2538-
newarray = (ArrayType *) palloc(newsize);
2538+
newarray = (ArrayType *) palloc0(newsize);
25392539
SET_VARSIZE(newarray, newsize);
25402540
newarray->ndim = ndim;
25412541
newarray->dataoffset = newhasnulls ? overheadlen : 0;
@@ -2794,7 +2794,7 @@ array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType,
27942794
dataoffset = 0; /* marker for no null bitmap */
27952795
nbytes += ARR_OVERHEAD_NONULLS(ndim);
27962796
}
2797-
result = (ArrayType *) palloc(nbytes);
2797+
result = (ArrayType *) palloc0(nbytes);
27982798
SET_VARSIZE(result, nbytes);
27992799
result->ndim = ndim;
28002800
result->dataoffset = dataoffset;
@@ -2930,7 +2930,7 @@ construct_md_array(Datum *elems,
29302930
dataoffset = 0; /* marker for no null bitmap */
29312931
nbytes += ARR_OVERHEAD_NONULLS(ndims);
29322932
}
2933-
result = (ArrayType *) palloc(nbytes);
2933+
result = (ArrayType *) palloc0(nbytes);
29342934
SET_VARSIZE(result, nbytes);
29352935
result->ndim = ndims;
29362936
result->dataoffset = dataoffset;
@@ -2954,7 +2954,7 @@ construct_empty_array(Oid elmtype)
29542954
{
29552955
ArrayType *result;
29562956

2957-
result = (ArrayType *) palloc(sizeof(ArrayType));
2957+
result = (ArrayType *) palloc0(sizeof(ArrayType));
29582958
SET_VARSIZE(result, sizeof(ArrayType));
29592959
result->ndim = 0;
29602960
result->dataoffset = 0;

src/backend/utils/adt/geo_ops.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,8 @@ path_recv(PG_FUNCTION_ARGS)
14781478
SET_VARSIZE(path, size);
14791479
path->npts = npts;
14801480
path->closed = (closed ? 1 : 0);
1481+
/* prevent instability in unused pad bytes */
1482+
path->dummy = 0;
14811483

14821484
for (i = 0; i < npts; i++)
14831485
{
@@ -4253,6 +4255,8 @@ path_add(PG_FUNCTION_ARGS)
42534255
SET_VARSIZE(result, size);
42544256
result->npts = (p1->npts + p2->npts);
42554257
result->closed = p1->closed;
4258+
/* prevent instability in unused pad bytes */
4259+
result->dummy = 0;
42564260

42574261
for (i = 0; i < p1->npts; i++)
42584262
{
@@ -4486,6 +4490,8 @@ poly_path(PG_FUNCTION_ARGS)
44864490
SET_VARSIZE(path, size);
44874491
path->npts = poly->npts;
44884492
path->closed = TRUE;
4493+
/* prevent instability in unused pad bytes */
4494+
path->dummy = 0;
44894495

44904496
for (i = 0; i < poly->npts; i++)
44914497
{

0 commit comments

Comments
 (0)