diff --git a/contrib/spi/refint.c b/contrib/spi/refint.c
index 01dd717..c8a4e2d 100644
--- a/contrib/spi/refint.c
+++ b/contrib/spi/refint.c
@@ -192,6 +192,8 @@ check_primary_key(PG_FUNCTION_ARGS)
 			/* internal error */
 			elog(ERROR, "check_primary_key: SPI_keepplan failed");
 		plan->splan = (SPIPlanPtr *) malloc(sizeof(SPIPlanPtr));
+		if (!plan->splan)
+			elog(ERROR, "out of memory");
 		*(plan->splan) = pplan;
 		plan->nplans = 1;
 	}
@@ -419,6 +421,9 @@ check_foreign_key(PG_FUNCTION_ARGS)
 
 		plan->splan = (SPIPlanPtr *) malloc(nrefs * sizeof(SPIPlanPtr));
 
+		if (!plan->splan)
+			elog(ERROR, "out of memory");
+
 		for (r = 0; r < nrefs; r++)
 		{
 			relname = args2[0];
@@ -627,6 +632,8 @@ find_plan(char *ident, EPlan **eplan, int *nplans)
 	else
 	{
 		newp = *eplan = (EPlan *) malloc(sizeof(EPlan));
+		if (!newp)
+			elog(ERROR, "out of memory");
 		(*nplans) = i = 0;
 	}
 
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/misc/ps_status.c b/src/backend/utils/misc/ps_status.c
index 892a810..6327b29 100644
--- a/src/backend/utils/misc/ps_status.c
+++ b/src/backend/utils/misc/ps_status.c
@@ -163,6 +163,8 @@ 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)
+			elog(FATAL, "out of memory");
 		for (i = 0; environ[i] != NULL; i++)
 			new_environ[i] = strdup(environ[i]);
 		new_environ[i] = NULL;
@@ -189,6 +191,8 @@ save_ps_display_args(int argc, char **argv)
 		int			i;
 
 		new_argv = (char **) malloc((argc + 1) * sizeof(char *));
+		if (!new_argv)
+			elog(FATAL, "out of memory");
 		for (i = 0; i < argc; i++)
 			new_argv[i] = strdup(argv[i]);
 		new_argv[argc] = NULL;
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..36a7df2 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++)
@@ -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/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 574f5b8..df94dd3 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,7 +152,7 @@ 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);
@@ -661,7 +662,7 @@ 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);
@@ -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
