On Sat, Aug 27, 2016 at 10:24 PM, Michael Paquier
<michael.paqu...@gmail.com> wrote:
> ./src/backend/postmaster/postmaster.c:              userDoption =
> strdup(optarg);
> [...]
> ./src/backend/bootstrap/bootstrap.c:                userDoption =
> strdup(optarg);
> [...]
> ./src/backend/tcop/postgres.c:                  userDoption = strdup(optarg);
> We cannot use pstrdup here because memory contexts are not set up
> here, still it would be better than crashing, but I am not sure if
> that's worth complicating this code.. Other opinions are welcome.

Those are not changed. We could have some NULL-checks but I am not
sure that's worth the complication here.

> ./contrib/vacuumlo/vacuumlo.c:              param.pg_user = strdup(optarg);
> [...]
> ./contrib/pg_standby/pg_standby.c:              triggerPath = strdup(optarg);
> [...]
> ./src/bin/pg_archivecleanup/pg_archivecleanup.c:
> additional_ext = strdup(optarg);        /* Extension to remove
> Right we can do something here with pstrdup().

Those are updated with pg_strdup().

> ./contrib/spi/refint.c:     plan->splan = (SPIPlanPtr *)
> malloc(sizeof(SPIPlanPtr));
> Regarding refint.c, you can see upthread. Instead of reworking the
> code it would be better to just drop it from the tree.

I'd rather see this nuked out of the surface of the code tree.

> ./src/backend/utils/adt/pg_locale.c:    grouping = strdup(extlconv->grouping);
> Here that would be a failure with an elog() as this is getting used by
> things like NUM_TOCHAR_finish and PGLC_localeconv.

Done.

> ./src/pl/tcl/pltcl.c:       prodesc->user_proname =
> strdup(NameStr(procStruct->proname));
> ./src/pl/tcl/pltcl.c:       prodesc->internal_proname =
> strdup(internal_proname);
> ./src/pl/tcl/pltcl.c-       if (prodesc->user_proname == NULL ||
> prodesc->internal_proname == NULL)
> ./src/pl/tcl/pltcl.c-           ereport(ERROR,
> ./src/pl/tcl/pltcl.c-                   (errcode(ERRCODE_OUT_OF_MEMORY),
> ./src/pl/tcl/pltcl.c-                    errmsg("out of memory")));
> Ah, yes. Here we need to do a free(prodesc) first.

Done.

> ./src/common/exec.c:        putenv(strdup(env_path));
> set_pglocale_pgservice() is used at the beginning of each process run,
> meaning that a failure here would be printf(stderr), followed by
> exit() for frontend, even ECPG as this compiles with -DFRONTEND.
> Backend can use elog(ERROR) btw.

Done.

Regarding the handling of strdup in libpq the code is careful in its
handling after a review, so we had better do nothing.

After that, there are two calls to realloc and one call to malloc that
deserve some attention, though those happen in pgc.l and I am not
exactly sure how to handle them.

As Alexander's email
(https://www.postgresql.org/message-id/20160826153358.GA29981%40e733)
has broken this thread, I am attaching to this thread the analysis
report that has been generated by Alexander previously. It was
referenced in only an URL.

Attached is an updated patch addressing those extra points.
-- 
Michael
find . -type f -iname '*.c' -exec egrep -C5 '[^a-z_](malloc|realloc|strdup)' {} 
+ | less

/(malloc|realloc|strdup)




./contrib/pg_standby/pg_standby.c-          case 't':           /* Trigger file 
*/
./contrib/pg_standby/pg_standby.c:              triggerPath = strdup(optarg);
./contrib/pg_standby/pg_standby.c-              break;

./contrib/spi/refint.c:     plan->splan = (SPIPlanPtr *) 
malloc(sizeof(SPIPlanPtr));
./contrib/spi/refint.c-     *(plan->splan) = pplan;
./contrib/spi/refint.c-     plan->nplans = 1;

./contrib/spi/refint.c:     plan->splan = (SPIPlanPtr *) malloc(nrefs * 
sizeof(SPIPlanPtr));
./contrib/spi/refint.c-
./contrib/spi/refint.c-     for (r = 0; r < nrefs; r++)
./contrib/spi/refint.c-     {
./contrib/spi/refint.c-         relname = args2[0];
./contrib/spi/refint.c-

./contrib/spi/refint.c-         if (strcmp((*eplan)[i].ident, ident) == 0)
./contrib/spi/refint.c-             break;
./contrib/spi/refint.c-     }
./contrib/spi/refint.c-     if (i != *nplans)
./contrib/spi/refint.c-         return (*eplan + i);
./contrib/spi/refint.c:     *eplan = (EPlan *) realloc(*eplan, (i + 1) * 
sizeof(EPlan));
./contrib/spi/refint.c-     newp = *eplan + i;
./contrib/spi/refint.c- }
./contrib/spi/refint.c- else
./contrib/spi/refint.c- {
./contrib/spi/refint.c:     newp = *eplan = (EPlan *) malloc(sizeof(EPlan));
./contrib/spi/refint.c-     (*nplans) = i = 0;
./contrib/spi/refint.c- }
./contrib/spi/refint.c-
./contrib/spi/refint.c- newp->ident = strdup(ident);
./contrib/spi/refint.c- newp->nplans = 0;

./contrib/spi/timetravel.c-     if (i != *nplans)
./contrib/spi/timetravel.c-         return (*eplan + i);
./contrib/spi/timetravel.c:     *eplan = (EPlan *) realloc(*eplan, (i + 1) * 
sizeof(EPlan));
./contrib/spi/timetravel.c-     newp = *eplan + i;
./contrib/spi/timetravel.c- }
./contrib/spi/timetravel.c- else
./contrib/spi/timetravel.c- {
./contrib/spi/timetravel.c:     newp = *eplan = (EPlan *) malloc(sizeof(EPlan));
./contrib/spi/timetravel.c-     (*nplans) = i = 0;
./contrib/spi/timetravel.c- }
./contrib/spi/timetravel.c-
./contrib/spi/timetravel.c- newp->ident = strdup(ident);
./contrib/spi/timetravel.c- newp->splan = NULL;

./contrib/vacuumlo/vacuumlo.c-          case 'U':
./contrib/vacuumlo/vacuumlo.c:              param.pg_user = strdup(optarg);
./contrib/vacuumlo/vacuumlo.c-              break;
./contrib/vacuumlo/vacuumlo.c-          case 'w':
./contrib/vacuumlo/vacuumlo.c-              param.pg_prompt = TRI_NO;
./contrib/vacuumlo/vacuumlo.c-              break;
./contrib/vacuumlo/vacuumlo.c-          case 'W':
--
./contrib/vacuumlo/vacuumlo.c-              if ((port < 1) || (port > 65535))
./contrib/vacuumlo/vacuumlo.c-              {
./contrib/vacuumlo/vacuumlo.c-                  fprintf(stderr, "%s: invalid 
port number: %s\n", progname, optarg);
./contrib/vacuumlo/vacuumlo.c-                  exit(1);
./contrib/vacuumlo/vacuumlo.c-              }
./contrib/vacuumlo/vacuumlo.c:              param.pg_port = strdup(optarg);
./contrib/vacuumlo/vacuumlo.c-              break;
./contrib/vacuumlo/vacuumlo.c-          case 'h':
./contrib/vacuumlo/vacuumlo.c:              param.pg_host = strdup(optarg);
./contrib/vacuumlo/vacuumlo.c-              break;
./contrib/vacuumlo/vacuumlo.c-      }
./contrib/vacuumlo/vacuumlo.c-  }
./contrib/vacuumlo/vacuumlo.c-
./contrib/vacuumlo/vacuumlo.c-  /* No database given? Show usage */

./src/backend/bootstrap/bootstrap.c-            case 'D':
./src/backend/bootstrap/bootstrap.c:                userDoption = 
strdup(optarg);
./src/backend/bootstrap/bootstrap.c-                break;
./src/backend/bootstrap/bootstrap.c-            case 'd':
./src/backend/bootstrap/bootstrap.c-                {


./src/backend/main/main.c-  else if (argc > 1 && strcmp(argv[1], "--single") == 
0)
./src/backend/main/main.c-      PostgresMain(argc, argv,
./src/backend/main/main.c-                   NULL,      /* no dbname */
./src/backend/main/main.c:                   
strdup(get_user_name_or_exit(progname)));  /* does not return */


./src/backend/postmaster/postmaster.c-          case 'C':
./src/backend/postmaster/postmaster.c:              output_config_variable = 
strdup(optarg);
./src/backend/postmaster/postmaster.c-              break;
./src/backend/postmaster/postmaster.c-
./src/backend/postmaster/postmaster.c-          case 'D':
./src/backend/postmaster/postmaster.c:              userDoption = 
strdup(optarg);
./src/backend/postmaster/postmaster.c-              break;
./src/backend/postmaster/postmaster.c-

./src/backend/postmaster/postmaster.c-
./src/backend/postmaster/postmaster.c-  /*
./src/backend/postmaster/postmaster.c-   * Save remote_host and remote_port in 
port structure (after this, they
./src/backend/postmaster/postmaster.c-   * will appear in log_line_prefix data 
for log messages).
./src/backend/postmaster/postmaster.c-   */
./src/backend/postmaster/postmaster.c:  port->remote_host = strdup(remote_host);
./src/backend/postmaster/postmaster.c:  port->remote_port = strdup(remote_port);
./src/backend/postmaster/postmaster.c-


./src/backend/postmaster/postmaster.c-   */
./src/backend/postmaster/postmaster.c-  if (log_hostname &&
./src/backend/postmaster/postmaster.c-      ret == 0 &&
./src/backend/postmaster/postmaster.c-      strspn(remote_host, "0123456789.") 
< strlen(remote_host) &&
./src/backend/postmaster/postmaster.c-      strspn(remote_host, 
"0123456789ABCDEFabcdef:") < strlen(remote_host))
./src/backend/postmaster/postmaster.c:      port->remote_hostname = 
strdup(remote_host);
./src/backend/postmaster/postmaster.c-


./src/backend/tcop/postgres.c-          case 'D':
./src/backend/tcop/postgres.c-              if (secure)
./src/backend/tcop/postgres.c:                  userDoption = strdup(optarg);
./src/backend/tcop/postgres.c-              break;
./src/backend/tcop/postgres.c-
./src/backend/tcop/postgres.c-          case 'd':
./src/backend/tcop/postgres.c-              set_debug_options(atoi(optarg), 
ctx, gucsource);
./src/backend/tcop/postgres.c-              break;

./src/backend/tcop/postgres.c-  if (!errs && dbname && *dbname == NULL && argc 
- optind >= 1)
./src/backend/tcop/postgres.c:      *dbname = strdup(argv[optind++]);
./src/backend/tcop/postgres.c-
./src/backend/tcop/postgres.c-  if (errs || argc != optind)
./src/backend/tcop/postgres.c-  {
./src/backend/tcop/postgres.c-      if (errs)
./src/backend/tcop/postgres.c-          optind--;           /* complain about 
the previous argument */


./src/backend/utils/adt/pg_locale.c:    grouping = strdup(extlconv->grouping);
./src/backend/utils/adt/pg_locale.c-
./src/backend/utils/adt/pg_locale.c-#ifdef WIN32
./src/backend/utils/adt/pg_locale.c-    /* use monetary to set the ctype */
./src/backend/utils/adt/pg_locale.c-    setlocale(LC_CTYPE, locale_monetary);
./src/backend/utils/adt/pg_locale.c-#endif

./src/backend/utils/adt/pg_locale.c:    CurrentLocaleConv.mon_grouping = 
strdup(extlconv->mon_grouping);



++ pg_locale.c (result is never checked by caller):

static char *
db_encoding_strdup(int encoding, const char *str)
{
    char       *pstr;
    char       *mstr;

    /* convert the string to the database encoding */
    pstr = pg_any_to_server(str, strlen(str), encoding);
    mstr = strdup(pstr);
    if (pstr != str)
        pfree(pstr);

    return mstr;
}

./src/backend/utils/error/elog.c:       syslog_ident = strdup(ident);

./src/backend/utils/mmgr/mcxt.c-    {
./src/backend/utils/mmgr/mcxt.c:        /* Special case for startup: use good 
ol' malloc */
./src/backend/utils/mmgr/mcxt.c:        node = (MemoryContext) malloc(needed);
./src/backend/utils/mmgr/mcxt.c-        Assert(node != NULL);
./src/backend/utils/mmgr/mcxt.c-    }

./src/bin/pg_archivecleanup/pg_archivecleanup.c-            case 'x':
./src/bin/pg_archivecleanup/pg_archivecleanup.c:                additional_ext 
= strdup(optarg);        /* Extension to remove
./src/bin/pg_archivecleanup/pg_archivecleanup.c-                                
                         * from xlogfile names */
./src/bin/pg_archivecleanup/pg_archivecleanup.c-                break;

./src/common/exec.c-    if (getenv("PGLOCALEDIR") == NULL)
./src/common/exec.c-    {
./src/common/exec.c-        /* set for libpq to use */
./src/common/exec.c-        snprintf(env_path, sizeof(env_path), 
"PGLOCALEDIR=%s", path);
./src/common/exec.c-        canonicalize_path(env_path + 12);
./src/common/exec.c:        putenv(strdup(env_path));
./src/common/exec.c-    }
./src/common/exec.c-#endif
./src/common/exec.c-
./src/common/exec.c-    if (getenv("PGSYSCONFDIR") == NULL)
./src/common/exec.c-    {
./src/common/exec.c-        get_etc_path(my_exec_path, path);
./src/common/exec.c-
./src/common/exec.c-        /* set for libpq to use */
./src/common/exec.c-        snprintf(env_path, sizeof(env_path), 
"PGSYSCONFDIR=%s", path);
./src/common/exec.c-        canonicalize_path(env_path + 13);
./src/common/exec.c:        putenv(strdup(env_path));
./src/common/exec.c-    }
./src/common/exec.c-}


./src/interfaces/ecpg/preproc/pgc.c-    /* initialize literal buffer to a 
reasonable but expansible size */
./src/interfaces/ecpg/preproc/pgc.c-    if (literalbuf == NULL)
./src/interfaces/ecpg/preproc/pgc.c-    {
./src/interfaces/ecpg/preproc/pgc.c-        literalalloc = 1024;
./src/interfaces/ecpg/preproc/pgc.c:        literalbuf = (char *) 
malloc(literalalloc);
./src/interfaces/ecpg/preproc/pgc.c-    }


./src/interfaces/ecpg/preproc/pgc.c-    if ((literallen+yleng) >= literalalloc)
./src/interfaces/ecpg/preproc/pgc.c-    {
./src/interfaces/ecpg/preproc/pgc.c-        do
./src/interfaces/ecpg/preproc/pgc.c-            literalalloc *= 2;
./src/interfaces/ecpg/preproc/pgc.c-        while ((literallen+yleng) >= 
literalalloc);
./src/interfaces/ecpg/preproc/pgc.c:        literalbuf = (char *) 
realloc(literalbuf, literalalloc);
./src/interfaces/ecpg/preproc/pgc.c-    }
./src/interfaces/ecpg/preproc/pgc.c-    /* append new data, add trailing null */
./src/interfaces/ecpg/preproc/pgc.c-    memcpy(literalbuf+literallen, ytext, 
yleng);
./src/interfaces/ecpg/preproc/pgc.c-    literallen += yleng;
./src/interfaces/ecpg/preproc/pgc.c-    literalbuf[literallen] = '\0';
--
./src/interfaces/ecpg/preproc/pgc.c-{
./src/interfaces/ecpg/preproc/pgc.c-    /* enlarge buffer if needed */
./src/interfaces/ecpg/preproc/pgc.c-    if ((literallen+1) >= literalalloc)
./src/interfaces/ecpg/preproc/pgc.c-    {
./src/interfaces/ecpg/preproc/pgc.c-        literalalloc *= 2;
./src/interfaces/ecpg/preproc/pgc.c:        literalbuf = (char *) 
realloc(literalbuf, literalalloc);
./src/interfaces/ecpg/preproc/pgc.c-    }
./src/interfaces/ecpg/preproc/pgc.c-    /* append new data, add trailing null */
./src/interfaces/ecpg/preproc/pgc.c-    literalbuf[literallen] = ychar;
./src/interfaces/ecpg/preproc/pgc.c-    literallen += 1;
./src/interfaces/ecpg/preproc/pgc.c-    literalbuf[literallen] = '\0';


./src/interfaces/libpq/fe-exec.c-
./src/interfaces/libpq/fe-exec.c-   /* and remember the query text too, if 
possible */
./src/interfaces/libpq/fe-exec.c-   /* if insufficient memory, last_query just 
winds up NULL */
./src/interfaces/libpq/fe-exec.c-   if (conn->last_query)
./src/interfaces/libpq/fe-exec.c-       free(conn->last_query);
./src/interfaces/libpq/fe-exec.c:   conn->last_query = strdup(query);
./src/interfaces/libpq/fe-exec.c-
./src/interfaces/libpq/fe-exec.c-   /*
./src/interfaces/libpq/fe-exec.c-    * Give the data a push.  In nonblock mode, 
don't complain if we're unable
./src/interfaces/libpq/fe-exec.c-    * to send it all; PQgetResult() will do 
any additional flushing needed.
./src/interfaces/libpq/fe-exec.c-    */
--
./src/interfaces/libpq/fe-exec.c-
./src/interfaces/libpq/fe-exec.c-   /* and remember the query text too, if 
possible */
./src/interfaces/libpq/fe-exec.c-   /* if insufficient memory, last_query just 
winds up NULL */
./src/interfaces/libpq/fe-exec.c-   if (conn->last_query)
./src/interfaces/libpq/fe-exec.c-       free(conn->last_query);
./src/interfaces/libpq/fe-exec.c:   conn->last_query = strdup(query);
./src/interfaces/libpq/fe-exec.c-
./src/interfaces/libpq/fe-exec.c-   /*
./src/interfaces/libpq/fe-exec.c-    * Give the data a push.  In nonblock mode, 
don't complain if we're unable
./src/interfaces/libpq/fe-exec.c-    * to send it all; PQgetResult() will do 
any additional flushing needed.
./src/interfaces/libpq/fe-exec.c-    */


./src/interfaces/libpq/fe-exec.c-   /* and remember the query text too, if 
possible */
./src/interfaces/libpq/fe-exec.c-   /* if insufficient memory, last_query just 
winds up NULL */
./src/interfaces/libpq/fe-exec.c-   if (conn->last_query)
./src/interfaces/libpq/fe-exec.c-       free(conn->last_query);
./src/interfaces/libpq/fe-exec.c-   if (command)
./src/interfaces/libpq/fe-exec.c:       conn->last_query = strdup(command);
./src/interfaces/libpq/fe-exec.c-   else
./src/interfaces/libpq/fe-exec.c-       conn->last_query = NULL;
./src/interfaces/libpq/fe-exec.c-
./src/interfaces/libpq/fe-exec.c-   /*
./src/interfaces/libpq/fe-exec.c-    * Give the data a push.  In nonblock mode, 
don't complain if we're unable

./src/interfaces/libpq/fe-exec.c-   if (!res ||
./src/interfaces/libpq/fe-exec.c-       (res->resultStatus != PGRES_FATAL_ERROR 
&&
./src/interfaces/libpq/fe-exec.c-        res->resultStatus != 
PGRES_NONFATAL_ERROR))
./src/interfaces/libpq/fe-exec.c:       return strdup(libpq_gettext("PGresult 
is not an error result\n"));
./src/interfaces/libpq/fe-exec.c-

./src/interfaces/libpq/fe-exec.c-
./src/interfaces/libpq/fe-exec.c-   /* If insufficient memory to format the 
message, fail cleanly */
./src/interfaces/libpq/fe-exec.c-   if (PQExpBufferDataBroken(workBuf))
./src/interfaces/libpq/fe-exec.c-   {
./src/interfaces/libpq/fe-exec.c-       termPQExpBuffer(&workBuf);
./src/interfaces/libpq/fe-exec.c:       return strdup(libpq_gettext("out of 
memory\n"));
./src/interfaces/libpq/fe-exec.c-   }
./src/interfaces/libpq/fe-exec.c-


Possible resource leak:

./src/pl/tcl/pltcl.c:       prodesc->user_proname = 
strdup(NameStr(procStruct->proname));
./src/pl/tcl/pltcl.c:       prodesc->internal_proname = 
strdup(internal_proname);
./src/pl/tcl/pltcl.c-       if (prodesc->user_proname == NULL || 
prodesc->internal_proname == NULL)
./src/pl/tcl/pltcl.c-           ereport(ERROR,
./src/pl/tcl/pltcl.c-                   (errcode(ERRCODE_OUT_OF_MEMORY),
./src/pl/tcl/pltcl.c-                    errmsg("out of memory")));


./src/timezone/zic.c:                   directory = strdup(optarg);
./src/timezone/zic.c-               else
./src/timezone/zic.c-               {
./src/timezone/zic.c-                   fprintf(stderr,
./src/timezone/zic.c-                           _("%s: More than one -d option 
specified\n"),
./src/timezone/zic.c-                           progname);
./src/timezone/zic.c-                   return EXIT_FAILURE;
./src/timezone/zic.c-               }
./src/timezone/zic.c-               break;
./src/timezone/zic.c-           case 'l':
./src/timezone/zic.c-               if (lcltime == NULL)
./src/timezone/zic.c:                   lcltime = strdup(optarg);
./src/timezone/zic.c-               else
./src/timezone/zic.c-               {
./src/timezone/zic.c-                   fprintf(stderr,
./src/timezone/zic.c-                           _("%s: More than one -l option 
specified\n"),
./src/timezone/zic.c-                           progname);
./src/timezone/zic.c-                   return EXIT_FAILURE;
./src/timezone/zic.c-               }
./src/timezone/zic.c-               break;
./src/timezone/zic.c-           case 'p':
./src/timezone/zic.c-               if (psxrules == NULL)
./src/timezone/zic.c:                   psxrules = strdup(optarg);
./src/timezone/zic.c-               else
./src/timezone/zic.c-               {
./src/timezone/zic.c-                   fprintf(stderr,
./src/timezone/zic.c-                           _("%s: More than one -p option 
specified\n"),
./src/timezone/zic.c-                           progname);
./src/timezone/zic.c-                   return EXIT_FAILURE;
./src/timezone/zic.c-               }
./src/timezone/zic.c-               break;
./src/timezone/zic.c-           case 'y':
./src/timezone/zic.c-               if (yitcommand == NULL)
./src/timezone/zic.c:                   yitcommand = strdup(optarg);
./src/timezone/zic.c-               else
./src/timezone/zic.c-               {
./src/timezone/zic.c-                   fprintf(stderr,
./src/timezone/zic.c-                           _("%s: More than one -y option 
specified\n"),
./src/timezone/zic.c-                           progname);
./src/timezone/zic.c-                   return EXIT_FAILURE;
./src/timezone/zic.c-               }
./src/timezone/zic.c-               break;
./src/timezone/zic.c-           case 'L':
./src/timezone/zic.c-               if (leapsec == NULL)
./src/timezone/zic.c:                   leapsec = strdup(optarg);
diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c
index e5eeec2..5dd0046 100644
--- a/contrib/oid2name/oid2name.c
+++ b/contrib/oid2name/oid2name.c
@@ -306,6 +306,11 @@ sql_conn(struct options * my_opts)
 		{
 			PQfinish(conn);
 			password = simple_prompt("Password: ", 100, false);
+			if (!password)
+			{
+				fprintf(stderr, "%s: out of memory\n", "oid2name");
+				exit(1);
+			}
 			new_pass = true;
 		}
 	} while (new_pass);
diff --git a/contrib/pg_standby/pg_standby.c b/contrib/pg_standby/pg_standby.c
index 5eac2b1..e4136f9 100644
--- a/contrib/pg_standby/pg_standby.c
+++ b/contrib/pg_standby/pg_standby.c
@@ -632,7 +632,7 @@ main(int argc, char **argv)
 				}
 				break;
 			case 't':			/* Trigger file */
-				triggerPath = strdup(optarg);
+				triggerPath = pg_strdup(optarg);
 				break;
 			case 'w':			/* Max wait time */
 				maxwaittime = atoi(optarg);
diff --git a/contrib/vacuumlo/vacuumlo.c b/contrib/vacuumlo/vacuumlo.c
index 769c805..2b0faf0 100644
--- a/contrib/vacuumlo/vacuumlo.c
+++ b/contrib/vacuumlo/vacuumlo.c
@@ -71,7 +71,14 @@ vacuumlo(const char *database, const struct _param * param)
 
 	/* Note: password can be carried over from a previous call */
 	if (param->pg_prompt == TRI_YES && password == NULL)
+	{
 		password = simple_prompt("Password: ", 100, false);
+		if (!password)
+		{
+			fprintf(stderr, "out of memory\n");
+			return -1;
+		}
+	}
 
 	/*
 	 * Start the connection.  Loop until we have a password if requested by
@@ -115,6 +122,11 @@ vacuumlo(const char *database, const struct _param * param)
 		{
 			PQfinish(conn);
 			password = simple_prompt("Password: ", 100, false);
+			if (!password)
+			{
+				fprintf(stderr, "out of memory\n");
+				return -1;
+			}
 			new_pass = true;
 		}
 	} while (new_pass);
@@ -514,7 +526,7 @@ main(int argc, char **argv)
 				}
 				break;
 			case 'U':
-				param.pg_user = strdup(optarg);
+				param.pg_user = pg_strdup(optarg);
 				break;
 			case 'w':
 				param.pg_prompt = TRI_NO;
@@ -529,10 +541,10 @@ main(int argc, char **argv)
 					fprintf(stderr, "%s: invalid port number: %s\n", progname, optarg);
 					exit(1);
 				}
-				param.pg_port = strdup(optarg);
+				param.pg_port = pg_strdup(optarg);
 				break;
 			case 'h':
-				param.pg_host = strdup(optarg);
+				param.pg_host = pg_strdup(optarg);
 				break;
 		}
 	}
diff --git a/src/backend/port/dynloader/darwin.c b/src/backend/port/dynloader/darwin.c
index ccd92c3..a83c614 100644
--- a/src/backend/port/dynloader/darwin.c
+++ b/src/backend/port/dynloader/darwin.c
@@ -78,6 +78,9 @@ pg_dlsym(void *handle, char *funcname)
 	NSSymbol symbol;
 	char	   *symname = (char *) malloc(strlen(funcname) + 2);
 
+	if (!symname)
+		return NULL;
+
 	sprintf(symname, "_%s", funcname);
 	if (NSIsSymbolNameDefined(symname))
 	{
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index a818023..47f990c 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -512,6 +512,9 @@ PGLC_localeconv(void)
 	thousands_sep = db_encoding_strdup(encoding, extlconv->thousands_sep);
 	grouping = strdup(extlconv->grouping);
 
+	if (!grouping)
+		elog(ERROR, "out of memory");
+
 #ifdef WIN32
 	/* use monetary to set the ctype */
 	setlocale(LC_CTYPE, locale_monetary);
diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 892a810..09ef2df 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -109,6 +109,9 @@ static char **save_argv;
  * If needed, we make a copy of the original argv[] array to preserve it
  * from being clobbered by subsequent ps_display actions.
  *
+ * Note that in case of failure this cannot call elog() as it is not
+ * initialized yet.
+ *
  * (The original argv[] will not be overwritten by this routine, but may be
  * overwritten during init_ps_display.  Also, the physical location of the
  * environment strings may be moved, so this should be called before any code
@@ -163,8 +166,21 @@ save_ps_display_args(int argc, char **argv)
 		 * move the environment out of the way
 		 */
 		new_environ = (char **) malloc((i + 1) * sizeof(char *));
+		if (!new_environ)
+		{
+			write_stderr("out of memory\n");
+			exit(1);
+		}
 		for (i = 0; environ[i] != NULL; i++)
+		{
 			new_environ[i] = strdup(environ[i]);
+
+			if (!new_environ[i])
+			{
+				write_stderr("out of memory\n");
+				exit(1);
+			}
+		}
 		new_environ[i] = NULL;
 		environ = new_environ;
 	}
@@ -189,8 +205,21 @@ save_ps_display_args(int argc, char **argv)
 		int			i;
 
 		new_argv = (char **) malloc((argc + 1) * sizeof(char *));
+		if (!new_argv)
+		{
+			write_stderr("out of memory\n");
+			exit(1);
+		}
 		for (i = 0; i < argc; i++)
+		{
 			new_argv[i] = strdup(argv[i]);
+
+			if (!new_argv[i])
+			{
+				write_stderr("out of memory\n");
+				exit(1);
+			}
+		}
 		new_argv[argc] = NULL;
 
 #if defined(__darwin__)
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index aad6ba5..d00c889 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1562,6 +1562,11 @@ get_set_pwd(FILE *cmdfd)
 		 */
 		pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
 		pwd2 = simple_prompt("Enter it again: ", 100, false);
+		if (!pwd1 || !pwd2)
+		{
+			fprintf(stderr, _("out of memory\n"));
+			exit_nicely();
+		}
 		if (strcmp(pwd1, pwd2) != 0)
 		{
 			fprintf(stderr, _("Passwords didn't match.\n"));
diff --git a/src/bin/pg_archivecleanup/pg_archivecleanup.c b/src/bin/pg_archivecleanup/pg_archivecleanup.c
index 2b3d15d..319038f 100644
--- a/src/bin/pg_archivecleanup/pg_archivecleanup.c
+++ b/src/bin/pg_archivecleanup/pg_archivecleanup.c
@@ -314,7 +314,7 @@ main(int argc, char **argv)
 				dryrun = true;
 				break;
 			case 'x':
-				additional_ext = strdup(optarg);		/* Extension to remove
+				additional_ext = pg_strdup(optarg);		/* Extension to remove
 														 * from xlogfile names */
 				break;
 			default:
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index 72d8657..b0ac16b 100644
--- a/src/bin/pg_basebackup/streamutil.c
+++ b/src/bin/pg_basebackup/streamutil.c
@@ -151,6 +151,12 @@ GetConnection(void)
 			if (dbpassword)
 				free(dbpassword);
 			dbpassword = simple_prompt(_("Password: "), 100, false);
+			if (!dbpassword)
+			{
+				fprintf(stderr, _("%s: out of memory\n"),
+						progname);
+				exit(1);
+			}
 			need_password = false;
 		}
 
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 54a9f48..5e4a9be 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1890,7 +1890,14 @@ connectDatabase(const char *dbname, const char *connection_string,
 	PQconninfoOption *conn_opts = NULL;
 
 	if (prompt_password == TRI_YES && !password)
+	{
 		password = simple_prompt("Password: ", 100, false);
+		if (password == NULL)
+		{
+			fprintf(stderr, "%s: out of memory\n", progname);
+			exit_nicely(1);
+		}
+	}
 
 	/*
 	 * Start the connection.  Loop until we have a password if requested by
@@ -2003,6 +2010,11 @@ connectDatabase(const char *dbname, const char *connection_string,
 		{
 			PQfinish(conn);
 			password = simple_prompt("Password: ", 100, false);
+			if (password == NULL)
+			{
+				fprintf(stderr, "%s: out of memory\n", progname);
+				exit_nicely(1);
+			}
 			new_pass = true;
 		}
 	} while (new_pass);
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 8027955..dbd5388 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -819,6 +819,11 @@ doConnect(void)
 		{
 			PQfinish(conn);
 			password = simple_prompt("Password: ", 100, false);
+			if (!password)
+			{
+				fprintf(stderr, "out of memory\n");
+				return NULL;
+			}
 			new_pass = true;
 		}
 	} while (new_pass);
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 4aaf657..6fee807 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1095,6 +1095,12 @@ exec_command(const char *cmd,
 		pw1 = simple_prompt("Enter new password: ", 100, false);
 		pw2 = simple_prompt("Enter it again: ", 100, false);
 
+		if (!pw1 || !pw2)
+		{
+			psql_error("out of memory\n");
+			exit(EXIT_FAILURE);
+		}
+
 		if (strcmp(pw1, pw2) != 0)
 		{
 			psql_error("Passwords didn't match.\n");
@@ -1184,6 +1190,12 @@ exec_command(const char *cmd,
 				result = gets_fromFile(stdin);
 			}
 
+			if (!result)
+			{
+				psql_error("out of memory\n");
+				exit(EXIT_FAILURE);
+			}
+
 			if (!SetVariable(pset.vars, opt, result))
 			{
 				psql_error("\\%s: error while setting variable\n", cmd);
@@ -1760,6 +1772,11 @@ prompt_for_password(const char *username)
 		free(prompt_text);
 	}
 
+	if (!result)
+	{
+		psql_error("out of memory\n");
+		exit(EXIT_FAILURE);
+	}
 	return result;
 }
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 111593c..09cac30 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -210,7 +210,14 @@ main(int argc, char *argv[])
 								   options.username);
 
 	if (pset.getPassword == TRI_YES)
+	{
 		password = simple_prompt(password_prompt, 100, false);
+		if (!password)
+		{
+			fprintf(stderr, _("%s: out of memory\n"), pset.progname);
+			exit(EXIT_FAILURE);
+		}
+	}
 
 	/* loop until we have a password if requested by backend */
 	do
@@ -249,6 +256,11 @@ main(int argc, char *argv[])
 		{
 			PQfinish(pset.db);
 			password = simple_prompt(password_prompt, 100, false);
+			if (!password)
+			{
+				fprintf(stderr, _("%s: out of memory\n"), pset.progname);
+				exit(EXIT_FAILURE);
+			}
 			new_pass = true;
 		}
 	} while (new_pass);
diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c
index 7c1ebe0..16bef1a 100644
--- a/src/bin/scripts/common.c
+++ b/src/bin/scripts/common.c
@@ -79,7 +79,14 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
 	}
 
 	if (password == NULL && prompt_password == TRI_YES)
+	{
 		password = simple_prompt("Password: ", 100, false);
+		if (!password)
+		{
+			fprintf(stderr, _("%s: out of memory\n"), progname);
+			exit(1);
+		}
+	}
 
 	/*
 	 * Start the connection.  Loop until we have a password if requested by
@@ -126,6 +133,11 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
 			if (password)
 				free(password);
 			password = simple_prompt("Password: ", 100, false);
+			if (!password)
+			{
+				fprintf(stderr, _("%s: out of memory\n"), progname);
+				exit(1);
+			}
 			new_pass = true;
 		}
 	} while (new_pass);
@@ -278,6 +290,11 @@ yesno_prompt(const char *question)
 		char	   *resp;
 
 		resp = simple_prompt(prompt, 1, true);
+		if (!resp)
+		{
+			fprintf(stderr, _("out of memory\n"));
+			exit(1);
+		}
 
 		if (strcmp(resp, _(PG_YESLETTER)) == 0)
 		{
diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c
index e88879d..6a80ecc 100644
--- a/src/bin/scripts/createuser.c
+++ b/src/bin/scripts/createuser.c
@@ -188,7 +188,14 @@ main(int argc, char *argv[])
 	if (newuser == NULL)
 	{
 		if (interactive)
+		{
 			newuser = simple_prompt("Enter name of role to add: ", 128, true);
+			if (!newuser)
+			{
+				fprintf(stderr, _("%s: out of memory\n"), progname);
+				exit(1);
+			}
+		}
 		else
 		{
 			if (getenv("PGUSER"))
@@ -205,6 +212,11 @@ main(int argc, char *argv[])
 
 		pw1 = simple_prompt("Enter password for new role: ", 100, false);
 		pw2 = simple_prompt("Enter it again: ", 100, false);
+		if (!pw1 || !pw2)
+		{
+			fprintf(stderr, _("%s: out of memory\n"), progname);
+			exit(1);
+		}
 		if (strcmp(pw1, pw2) != 0)
 		{
 			fprintf(stderr, _("Passwords didn't match.\n"));
diff --git a/src/bin/scripts/dropuser.c b/src/bin/scripts/dropuser.c
index 31fa28f..94d5272 100644
--- a/src/bin/scripts/dropuser.c
+++ b/src/bin/scripts/dropuser.c
@@ -108,7 +108,14 @@ main(int argc, char *argv[])
 	if (dropuser == NULL)
 	{
 		if (interactive)
+		{
 			dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
+			if (!dropuser)
+			{
+				fprintf(stderr, _("%s: out of memory\n"), progname);
+				exit(1);
+			}
+		}
 		else
 		{
 			fprintf(stderr, _("%s: missing required argument role name\n"), progname);
diff --git a/src/common/exec.c b/src/common/exec.c
index d736b02..6dfef8f 100644
--- a/src/common/exec.c
+++ b/src/common/exec.c
@@ -580,21 +580,49 @@ set_pglocale_pgservice(const char *argv0, const char *app)
 
 	if (getenv("PGLOCALEDIR") == NULL)
 	{
+		char *buf;
+
 		/* set for libpq to use */
 		snprintf(env_path, sizeof(env_path), "PGLOCALEDIR=%s", path);
 		canonicalize_path(env_path + 12);
-		putenv(strdup(env_path));
+
+		buf = strdup(env_path);
+		if (buf == NULL)
+		{
+#ifdef FRONTEND
+			fprintf(stderr, _("out of memory"));
+			exit(EXIT_FAILURE)
+#else
+			elog(ERROR, "out of memory");
+#endif
+		}
+
+		putenv(buf);
 	}
 #endif
 
 	if (getenv("PGSYSCONFDIR") == NULL)
 	{
+		char *buf;
+
 		get_etc_path(my_exec_path, path);
 
 		/* set for libpq to use */
 		snprintf(env_path, sizeof(env_path), "PGSYSCONFDIR=%s", path);
 		canonicalize_path(env_path + 13);
-		putenv(strdup(env_path));
+
+		buf = strdup(env_path);
+		if (buf == NULL)
+		{
+#ifdef FRONTEND
+			fprintf(stderr, _("out of memory"));
+			exit(EXIT_FAILURE);
+#else
+			elog(ERROR, "out of memory");
+#endif
+		}
+
+		putenv(buf);
 	}
 }
 
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 2a335aa..fd5e760 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -1340,9 +1340,12 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
 		prodesc->user_proname = strdup(NameStr(procStruct->proname));
 		prodesc->internal_proname = strdup(internal_proname);
 		if (prodesc->user_proname == NULL || prodesc->internal_proname == NULL)
+		{
+			free(prodesc);
 			ereport(ERROR,
 					(errcode(ERRCODE_OUT_OF_MEMORY),
 					 errmsg("out of memory")));
+		}
 		prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
 		prodesc->fn_tid = procTup->t_self;
 
diff --git a/src/port/sprompt.c b/src/port/sprompt.c
index fd6f16e..da07c73 100644
--- a/src/port/sprompt.c
+++ b/src/port/sprompt.c
@@ -119,6 +119,11 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
 	{
 		/* get a new handle to turn echo off */
 		t_orig = (LPDWORD) malloc(sizeof(DWORD));
+		if (!t_orig)
+		{
+			free(destination);
+			return NULL;
+		}
 		t = GetStdHandle(STD_INPUT_HANDLE);
 
 		/* save the old configuration first */
diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index 908a7ce..6452a1e 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -15,6 +15,7 @@
 #include <sys/select.h>
 #endif
 
+#include "common/fe_memutils.h"
 #include "datatype/timestamp.h"
 #include "libpq-fe.h"
 #include "pqexpbuffer.h"
@@ -119,7 +120,7 @@ main(int argc, char **argv)
 	for (i = 0; i < testspec->nsessions; i++)
 		nallsteps += testspec->sessions[i]->nsteps;
 
-	allsteps = malloc(nallsteps * sizeof(Step *));
+	allsteps = pg_malloc(nallsteps * sizeof(Step *));
 
 	n = 0;
 	for (i = 0; i < testspec->nsessions; i++)
@@ -190,7 +191,7 @@ main(int argc, char **argv)
 		if (PQresultStatus(res) == PGRES_TUPLES_OK)
 		{
 			if (PQntuples(res) == 1 && PQnfields(res) == 1)
-				backend_pids[i] = strdup(PQgetvalue(res, 0, 0));
+				backend_pids[i] = pg_strdup(PQgetvalue(res, 0, 0));
 			else
 			{
 				fprintf(stderr, "backend pid query returned %d rows and %d columns, expected 1 row and 1 column",
@@ -286,7 +287,7 @@ run_all_permutations(TestSpec *testspec)
 	for (i = 0; i < testspec->nsessions; i++)
 		nsteps += testspec->sessions[i]->nsteps;
 
-	steps = malloc(sizeof(Step *) * nsteps);
+	steps = pg_malloc(sizeof(Step *) * nsteps);
 
 	/*
 	 * To generate the permutations, we conceptually put the steps of each
@@ -297,7 +298,7 @@ run_all_permutations(TestSpec *testspec)
 	 * A pile is actually just an integer which tells how many steps we've
 	 * already picked from this pile.
 	 */
-	piles = malloc(sizeof(int) * testspec->nsessions);
+	piles = pg_malloc(sizeof(int) * testspec->nsessions);
 	for (i = 0; i < testspec->nsessions; i++)
 		piles[i] = 0;
 
@@ -345,7 +346,7 @@ run_named_permutations(TestSpec *testspec)
 		Permutation *p = testspec->permutations[i];
 		Step	  **steps;
 
-		steps = malloc(p->nsteps * sizeof(Step *));
+		steps = pg_malloc(p->nsteps * sizeof(Step *));
 
 		/* Find all the named steps using the lookup table */
 		for (j = 0; j < p->nsteps; j++)
@@ -476,8 +477,8 @@ run_permutation(TestSpec *testspec, int nsteps, Step **steps)
 		return;
 	}
 
-	waiting = malloc(sizeof(Step *) * testspec->nsessions);
-	errorstep = malloc(sizeof(Step *) * testspec->nsessions);
+	waiting = pg_malloc(sizeof(Step *) * testspec->nsessions);
+	errorstep = pg_malloc(sizeof(Step *) * testspec->nsessions);
 
 	printf("\nstarting permutation:");
 	for (i = 0; i < nsteps; i++)
diff --git a/src/test/isolation/specparse.y b/src/test/isolation/specparse.y
index fce6cc6..59744ce 100644
--- a/src/test/isolation/specparse.y
+++ b/src/test/isolation/specparse.y
@@ -73,8 +73,8 @@ setup_list:
 			}
 			| setup_list setup
 			{
-				$$.elements = realloc($1.elements,
-									  ($1.nelements + 1) * sizeof(void *));
+				$$.elements = pg_realloc($1.elements,
+										 ($1.nelements + 1) * sizeof(void *));
 				$$.elements[$1.nelements] = $2;
 				$$.nelements = $1.nelements + 1;
 			}
@@ -97,15 +97,15 @@ opt_teardown:
 session_list:
 			session_list session
 			{
-				$$.elements = realloc($1.elements,
-									  ($1.nelements + 1) * sizeof(void *));
+				$$.elements = pg_realloc($1.elements,
+										 ($1.nelements + 1) * sizeof(void *));
 				$$.elements[$1.nelements] = $2;
 				$$.nelements = $1.nelements + 1;
 			}
 			| session
 			{
 				$$.nelements = 1;
-				$$.elements = malloc(sizeof(void *));
+				$$.elements = pg_malloc(sizeof(void *));
 				$$.elements[0] = $1;
 			}
 		;
@@ -113,7 +113,7 @@ session_list:
 session:
 			SESSION string_literal opt_setup step_list opt_teardown
 			{
-				$$ = malloc(sizeof(Session));
+				$$ = pg_malloc(sizeof(Session));
 				$$->name = $2;
 				$$->setupsql = $3;
 				$$->steps = (Step **) $4.elements;
@@ -125,15 +125,15 @@ session:
 step_list:
 			step_list step
 			{
-				$$.elements = realloc($1.elements,
-									  ($1.nelements + 1) * sizeof(void *));
+				$$.elements = pg_realloc($1.elements,
+										 ($1.nelements + 1) * sizeof(void *));
 				$$.elements[$1.nelements] = $2;
 				$$.nelements = $1.nelements + 1;
 			}
 			| step
 			{
 				$$.nelements = 1;
-				$$.elements = malloc(sizeof(void *));
+				$$.elements = pg_malloc(sizeof(void *));
 				$$.elements[0] = $1;
 			}
 		;
@@ -142,7 +142,7 @@ step_list:
 step:
 			STEP string_literal sqlblock
 			{
-				$$ = malloc(sizeof(Step));
+				$$ = pg_malloc(sizeof(Step));
 				$$->name = $2;
 				$$->sql = $3;
 				$$->errormsg = NULL;
@@ -164,15 +164,15 @@ opt_permutation_list:
 permutation_list:
 			permutation_list permutation
 			{
-				$$.elements = realloc($1.elements,
-									  ($1.nelements + 1) * sizeof(void *));
+				$$.elements = pg_realloc($1.elements,
+										 ($1.nelements + 1) * sizeof(void *));
 				$$.elements[$1.nelements] = $2;
 				$$.nelements = $1.nelements + 1;
 			}
 			| permutation
 			{
 				$$.nelements = 1;
-				$$.elements = malloc(sizeof(void *));
+				$$.elements = pg_malloc(sizeof(void *));
 				$$.elements[0] = $1;
 			}
 		;
@@ -181,7 +181,7 @@ permutation_list:
 permutation:
 			PERMUTATION string_literal_list
 			{
-				$$ = malloc(sizeof(Permutation));
+				$$ = pg_malloc(sizeof(Permutation));
 				$$->stepnames = (char **) $2.elements;
 				$$->nsteps = $2.nelements;
 			}
@@ -190,15 +190,15 @@ permutation:
 string_literal_list:
 			string_literal_list string_literal
 			{
-				$$.elements = realloc($1.elements,
-									  ($1.nelements + 1) * sizeof(void *));
+				$$.elements = pg_realloc($1.elements,
+										 ($1.nelements + 1) * sizeof(void *));
 				$$.elements[$1.nelements] = $2;
 				$$.nelements = $1.nelements + 1;
 			}
 			| string_literal
 			{
 				$$.nelements = 1;
-				$$.elements = malloc(sizeof(void *));
+				$$.elements = pg_malloc(sizeof(void *));
 				$$.elements[0] = $1;
 			}
 		;
diff --git a/src/test/isolation/specscanner.l b/src/test/isolation/specscanner.l
index 675bd21..6f081d5 100644
--- a/src/test/isolation/specscanner.l
+++ b/src/test/isolation/specscanner.l
@@ -56,7 +56,7 @@ teardown		{ return(TEARDOWN); }
 				}
 <qstr>\"		{
 					litbuf[litbufpos] = '\0';
-					yylval.str = strdup(litbuf);
+					yylval.str = pg_strdup(litbuf);
 					BEGIN(INITIAL);
 					return(string_literal);
 				}
@@ -72,7 +72,7 @@ teardown		{ return(TEARDOWN); }
 				}
 <sql>{space}*"}" {
 					litbuf[litbufpos] = '\0';
-					yylval.str = strdup(litbuf);
+					yylval.str = pg_strdup(litbuf);
 					BEGIN(INITIAL);
 					return(sqlblock);
 				}
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 574f5b8..9dafdaf 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -29,6 +29,7 @@
 #include <sys/resource.h>
 #endif
 
+#include "common/fe_memutils.h"
 #include "common/restricted_token.h"
 #include "common/username.h"
 #include "getopt_long.h"
@@ -151,10 +152,10 @@ unlimit_core_size(void)
 void
 add_stringlist_item(_stringlist **listhead, const char *str)
 {
-	_stringlist *newentry = malloc(sizeof(_stringlist));
+	_stringlist *newentry = pg_malloc(sizeof(_stringlist));
 	_stringlist *oldentry;
 
-	newentry->str = strdup(str);
+	newentry->str = pg_strdup(str);
 	newentry->next = NULL;
 	if (*listhead == NULL)
 		*listhead = newentry;
@@ -187,7 +188,7 @@ free_stringlist(_stringlist **listhead)
 static void
 split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
 {
-	char	   *sc = strdup(s);
+	char	   *sc = pg_strdup(s);
 	char	   *token = strtok(sc, delim);
 
 	while (token)
@@ -327,7 +328,7 @@ signal_remove_temp(int signum)
 static const char *
 make_temp_sockdir(void)
 {
-	char	   *template = strdup("/tmp/pg_regress-XXXXXX");
+	char	   *template = pg_strdup("/tmp/pg_regress-XXXXXX");
 
 	temp_sockdir = mkdtemp(template);
 	if (temp_sockdir == NULL)
@@ -441,7 +442,7 @@ replace_string(char *string, char *replace, char *replacement)
 
 	while ((ptr = strstr(string, replace)) != NULL)
 	{
-		char	   *dup = strdup(string);
+		char	   *dup = pg_strdup(string);
 
 		strlcpy(string, dup, ptr - string + 1);
 		strcat(string, replacement);
@@ -661,11 +662,11 @@ load_resultmap(void)
 		 */
 		if (string_matches_pattern(host_platform, platform))
 		{
-			_resultmap *entry = malloc(sizeof(_resultmap));
+			_resultmap *entry = pg_malloc(sizeof(_resultmap));
 
-			entry->test = strdup(buf);
-			entry->type = strdup(file_type);
-			entry->resultfile = strdup(expected);
+			entry->test = pg_strdup(buf);
+			entry->type = pg_strdup(file_type);
+			entry->resultfile = pg_strdup(expected);
 			entry->next = resultmap;
 			resultmap = entry;
 		}
@@ -908,7 +909,7 @@ current_windows_user(const char **acct, const char **dom)
 				progname, GetLastError());
 		exit(2);
 	}
-	tokenuser = malloc(retlen);
+	tokenuser = pg_malloc(retlen);
 	if (!GetTokenInformation(token, TokenUser, tokenuser, retlen, &retlen))
 	{
 		fprintf(stderr,
@@ -1460,7 +1461,7 @@ wait_for_tests(PID_TYPE * pids, int *statuses, char **names, int num_tests)
 	int			i;
 
 #ifdef WIN32
-	PID_TYPE   *active_pids = malloc(num_tests * sizeof(PID_TYPE));
+	PID_TYPE   *active_pids = pg_malloc(num_tests * sizeof(PID_TYPE));
 
 	memcpy(active_pids, pids, num_tests * sizeof(PID_TYPE));
 #endif
@@ -1848,7 +1849,7 @@ open_result_files(void)
 
 	/* create the log file (copy of running status output) */
 	snprintf(file, sizeof(file), "%s/regression.out", outputdir);
-	logfilename = strdup(file);
+	logfilename = pg_strdup(file);
 	logfile = fopen(logfilename, "w");
 	if (!logfile)
 	{
@@ -1859,7 +1860,7 @@ open_result_files(void)
 
 	/* create the diffs file as empty */
 	snprintf(file, sizeof(file), "%s/regression.diffs", outputdir);
-	difffilename = strdup(file);
+	difffilename = pg_strdup(file);
 	difffile = fopen(difffilename, "w");
 	if (!difffile)
 	{
@@ -2064,13 +2065,13 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 				 * before we add the specified one.
 				 */
 				free_stringlist(&dblist);
-				split_to_stringlist(strdup(optarg), ", ", &dblist);
+				split_to_stringlist(pg_strdup(optarg), ", ", &dblist);
 				break;
 			case 2:
 				debug = true;
 				break;
 			case 3:
-				inputdir = strdup(optarg);
+				inputdir = pg_strdup(optarg);
 				break;
 			case 4:
 				add_stringlist_item(&loadlanguage, optarg);
@@ -2079,10 +2080,10 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 				max_connections = atoi(optarg);
 				break;
 			case 6:
-				encoding = strdup(optarg);
+				encoding = pg_strdup(optarg);
 				break;
 			case 7:
-				outputdir = strdup(optarg);
+				outputdir = pg_strdup(optarg);
 				break;
 			case 8:
 				add_stringlist_item(&schedulelist, optarg);
@@ -2094,27 +2095,27 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 				nolocale = true;
 				break;
 			case 13:
-				hostname = strdup(optarg);
+				hostname = pg_strdup(optarg);
 				break;
 			case 14:
 				port = atoi(optarg);
 				port_specified_by_user = true;
 				break;
 			case 15:
-				user = strdup(optarg);
+				user = pg_strdup(optarg);
 				break;
 			case 16:
 				/* "--bindir=" means to use PATH */
 				if (strlen(optarg))
-					bindir = strdup(optarg);
+					bindir = pg_strdup(optarg);
 				else
 					bindir = NULL;
 				break;
 			case 17:
-				dlpath = strdup(optarg);
+				dlpath = pg_strdup(optarg);
 				break;
 			case 18:
-				split_to_stringlist(strdup(optarg), ", ", &extraroles);
+				split_to_stringlist(pg_strdup(optarg), ", ", &extraroles);
 				break;
 			case 19:
 				add_stringlist_item(&temp_configs, optarg);
@@ -2123,13 +2124,13 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc
 				use_existing = true;
 				break;
 			case 21:
-				launcher = strdup(optarg);
+				launcher = pg_strdup(optarg);
 				break;
 			case 22:
 				add_stringlist_item(&loadextension, optarg);
 				break;
 			case 24:
-				config_auth_datadir = pstrdup(optarg);
+				config_auth_datadir = pg_strdup(optarg);
 				break;
 			default:
 				/* getopt_long already emitted a complaint */
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to