On Mon, Mar 25, 2019 at 01:18:05PM +0000, Daniel Gustafsson wrote: > When reading another codepath, I happened to notice a few codepaths where we > do > pg_malloc() immediately followed by a memset( .. 0, ..), without there being > a > justification (that I can see) for not using pg_malloc0() instead. The > attached > patch changes to pg_malloc0(), and passes make check.
If we simplify all of them (and that's not really a big deal), I have spotted two extra places on top of what you noticed, one in gist.c where ROTATEDIST is defined and a second one in tablefunc.c. Thoughts? -- Michael
diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c
index 02e4921410..cd02495c45 100644
--- a/contrib/tablefunc/tablefunc.c
+++ b/contrib/tablefunc/tablefunc.c
@@ -867,11 +867,8 @@ get_crosstab_tuplestore(char *sql,
"tuple has %d columns but crosstab " \
"returns %d.", tupdesc->natts, result_ncols)));
- /* allocate space */
- values = (char **) palloc(result_ncols * sizeof(char *));
-
- /* and make sure it's clear */
- memset(values, '\0', result_ncols * sizeof(char *));
+ /* allocate space and make sure it's clear */
+ values = (char **) palloc0(result_ncols * sizeof(char *));
for (i = 0; i < proc; i++)
{
diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index a746e911f3..2fddb23496 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -43,8 +43,7 @@ static void gistprunepage(Relation rel, Page page, Buffer buffer,
#define ROTATEDIST(d) do { \
- SplitedPageLayout *tmp=(SplitedPageLayout*)palloc(sizeof(SplitedPageLayout)); \
- memset(tmp,0,sizeof(SplitedPageLayout)); \
+ SplitedPageLayout *tmp=(SplitedPageLayout*)palloc0(sizeof(SplitedPageLayout)); \
tmp->block.blkno = InvalidBlockNumber; \
tmp->buffer = InvalidBuffer; \
tmp->next = (d); \
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 2266c99b33..6131cdda96 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -1382,8 +1382,7 @@ SortTocFromFile(Archive *AHX)
bool incomplete_line;
/* Allocate space for the 'wanted' array, and init it */
- ropt->idWanted = (bool *) pg_malloc(sizeof(bool) * AH->maxDumpId);
- memset(ropt->idWanted, 0, sizeof(bool) * AH->maxDumpId);
+ ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId);
/* Setup the file */
fh = fopen(ropt->tocFile, PG_BINARY_R);
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index bb128c89f3..a7a5b1e7f7 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -320,8 +320,7 @@ TopoSort(DumpableObject **objs,
* We also make a map showing the input-order index of the item with
* dumpId j.
*/
- beforeConstraints = (int *) pg_malloc((maxDumpId + 1) * sizeof(int));
- memset(beforeConstraints, 0, (maxDumpId + 1) * sizeof(int));
+ beforeConstraints = (int *) pg_malloc0((maxDumpId + 1) * sizeof(int));
idMap = (int *) pg_malloc((maxDumpId + 1) * sizeof(int));
for (i = 0; i < numObjs; i++)
{
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index b1afe44817..cdd21d7469 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -5301,8 +5301,7 @@ main(int argc, char **argv)
else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
login = env;
- state = (CState *) pg_malloc(sizeof(CState));
- memset(state, 0, sizeof(CState));
+ state = (CState *) pg_malloc0(sizeof(CState));
/* set random seed early, because it may be used while parsing scripts. */
if (!set_random_seed(getenv("PGBENCH_RANDOM_SEED")))
signature.asc
Description: PGP signature
