1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-06 07:49:08 +03:00

Fix a bunch of places that called malloc and friends with no NULL check.

Where possible, use palloc or pg_malloc instead; otherwise, insert
explicit NULL checks.

Generally speaking, these are places where an actual OOM is quite
unlikely, either because they're in client programs that don't
allocate all that much, or they're very early in process startup
so that we'd likely have had a fork() failure instead.  Hence,
no back-patch, even though this is nominally a bug fix.

Michael Paquier, with some adjustments by me

Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
This commit is contained in:
Tom Lane
2016-08-30 18:22:43 -04:00
parent 9daec77e16
commit 052cc223d5
12 changed files with 108 additions and 69 deletions

View File

@@ -113,6 +113,9 @@ static char **save_argv;
* overwritten during init_ps_display. Also, the physical location of the
* environment strings may be moved, so this should be called before any code
* that might try to hang onto a getenv() result.)
*
* Note that in case of failure this cannot call elog() as that is not
* initialized yet. We rely on write_stderr() instead.
*/
char **
save_ps_display_args(int argc, char **argv)
@@ -163,8 +166,20 @@ 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 +204,20 @@ 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__)