mirror of
https://github.com/postgres/postgres.git
synced 2025-11-18 02:02:55 +03:00
Avoid leakage of zero-length arrays in partition_bounds_copy().
If ndatums is zero, the code would allocate zero-length boundKinds and boundDatums chunks, which would have nothing pointing to them, leading to Valgrind complaints. Rearrange the code to avoid the useless pallocs, and also to not bother computing byval/typlen when they aren't used. I'm unsure why I didn't see this in my Valgrind testing back in May. This code hasn't changed since then, but maybe we added a regression test that reaches this edge case. Or possibly I just failed to notice the reports, which do say "0 bytes lost". Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/285483.1746756246@sss.pgh.pa.us
This commit is contained in:
@@ -1007,9 +1007,6 @@ partition_bounds_copy(PartitionBoundInfo src,
|
||||
int ndatums;
|
||||
int nindexes;
|
||||
int partnatts;
|
||||
bool hash_part;
|
||||
int natts;
|
||||
Datum *boundDatums;
|
||||
|
||||
dest = (PartitionBoundInfo) palloc(sizeof(PartitionBoundInfoData));
|
||||
|
||||
@@ -1023,7 +1020,7 @@ partition_bounds_copy(PartitionBoundInfo src,
|
||||
|
||||
dest->datums = (Datum **) palloc(sizeof(Datum *) * ndatums);
|
||||
|
||||
if (src->kind != NULL)
|
||||
if (src->kind != NULL && ndatums > 0)
|
||||
{
|
||||
PartitionRangeDatumKind *boundKinds;
|
||||
|
||||
@@ -1058,9 +1055,11 @@ partition_bounds_copy(PartitionBoundInfo src,
|
||||
* For hash partitioning, datums array will have two elements - modulus
|
||||
* and remainder.
|
||||
*/
|
||||
hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
|
||||
natts = hash_part ? 2 : partnatts;
|
||||
boundDatums = palloc(ndatums * natts * sizeof(Datum));
|
||||
if (ndatums > 0)
|
||||
{
|
||||
bool hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
|
||||
int natts = hash_part ? 2 : partnatts;
|
||||
Datum *boundDatums = palloc(ndatums * natts * sizeof(Datum));
|
||||
|
||||
for (i = 0; i < ndatums; i++)
|
||||
{
|
||||
@@ -1069,6 +1068,9 @@ partition_bounds_copy(PartitionBoundInfo src,
|
||||
dest->datums[i] = &boundDatums[i * natts];
|
||||
|
||||
for (j = 0; j < natts; j++)
|
||||
{
|
||||
if (dest->kind == NULL ||
|
||||
dest->kind[i][j] == PARTITION_RANGE_DATUM_VALUE)
|
||||
{
|
||||
bool byval;
|
||||
int typlen;
|
||||
@@ -1083,13 +1085,12 @@ partition_bounds_copy(PartitionBoundInfo src,
|
||||
byval = key->parttypbyval[j];
|
||||
typlen = key->parttyplen[j];
|
||||
}
|
||||
|
||||
if (dest->kind == NULL ||
|
||||
dest->kind[i][j] == PARTITION_RANGE_DATUM_VALUE)
|
||||
dest->datums[i][j] = datumCopy(src->datums[i][j],
|
||||
byval, typlen);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dest->indexes = (int *) palloc(sizeof(int) * nindexes);
|
||||
memcpy(dest->indexes, src->indexes, sizeof(int) * nindexes);
|
||||
|
||||
Reference in New Issue
Block a user