Avoid useless allocations for information of dumpable objects in pg_dump/
authorMichael Paquier <michael@paquier.xyz>
Mon, 14 Sep 2020 01:44:23 +0000 (10:44 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 14 Sep 2020 01:44:23 +0000 (10:44 +0900)
If there are no objects of a certain type, there is no need to do an
allocation for a set of DumpableObject items.  The previous coding did
an allocation of 1 byte instead as per the fallback of pg_malloc() in
the event of an allocation size of zero.  This assigns NULL instead for
a set of dumpable objects.

A similar rule already applied to findObjectByOid(), so this makes the
code more defensive as we would just fail with a pointer dereference
instead of attempting to use some incorrect data if a non-existing,
positive, OID is given by a caller of this function.

Author: Daniel Gustafsson
Reviewed-by: Julien Rouhaud, Ranier Vilela
Discussion: https://postgr.es/m/26C43E58-BDD0-4F1A-97CC-4A07B52E32C5@yesql.se

src/bin/pg_dump/common.c

index 08239dde4f92f7ccae45c168f1a60eee3e3ad829..634ca86cfb7884788883ba6284ad7494c4cf81f5 100644 (file)
@@ -719,6 +719,9 @@ buildIndexArray(void *objArray, int numObjs, Size objSize)
        DumpableObject **ptrs;
        int                     i;
 
+       if (numObjs <= 0)
+               return NULL;
+
        ptrs = (DumpableObject **) pg_malloc(numObjs * sizeof(DumpableObject *));
        for (i = 0; i < numObjs; i++)
                ptrs[i] = (DumpableObject *) ((char *) objArray + i * objSize);