mirror of
https://github.com/postgres/postgres.git
synced 2025-08-14 02:22:38 +03:00
Modify pg_dump to use error-free memory allocation macros. This avoids
ignoring errors and call-site error checking.
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include "pg_backup_db.h"
|
||||
#include "common.h"
|
||||
#include "dumputils.h"
|
||||
|
||||
#include <ctype.h>
|
||||
@@ -541,7 +542,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
|
||||
{
|
||||
ahlog(AH, 1, "connecting to new database \"%s\"\n", te->tag);
|
||||
_reconnectToDB(AH, te->tag);
|
||||
ropt->dbname = strdup(te->tag);
|
||||
ropt->dbname = pg_strdup(te->tag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -660,7 +661,7 @@ NewRestoreOptions(void)
|
||||
{
|
||||
RestoreOptions *opts;
|
||||
|
||||
opts = (RestoreOptions *) calloc(1, sizeof(RestoreOptions));
|
||||
opts = (RestoreOptions *) pg_calloc(1, sizeof(RestoreOptions));
|
||||
|
||||
/* set any fields that shouldn't default to zeroes */
|
||||
opts->format = archUnknown;
|
||||
@@ -759,9 +760,7 @@ ArchiveEntry(Archive *AHX,
|
||||
ArchiveHandle *AH = (ArchiveHandle *) AHX;
|
||||
TocEntry *newToc;
|
||||
|
||||
newToc = (TocEntry *) calloc(1, sizeof(TocEntry));
|
||||
if (!newToc)
|
||||
die_horribly(AH, modulename, "out of memory\n");
|
||||
newToc = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
|
||||
|
||||
AH->tocCount++;
|
||||
if (dumpId > AH->maxDumpId)
|
||||
@@ -776,19 +775,19 @@ ArchiveEntry(Archive *AHX,
|
||||
newToc->dumpId = dumpId;
|
||||
newToc->section = section;
|
||||
|
||||
newToc->tag = strdup(tag);
|
||||
newToc->namespace = namespace ? strdup(namespace) : NULL;
|
||||
newToc->tablespace = tablespace ? strdup(tablespace) : NULL;
|
||||
newToc->owner = strdup(owner);
|
||||
newToc->tag = pg_strdup(tag);
|
||||
newToc->namespace = namespace ? pg_strdup(namespace) : NULL;
|
||||
newToc->tablespace = tablespace ? pg_strdup(tablespace) : NULL;
|
||||
newToc->owner = pg_strdup(owner);
|
||||
newToc->withOids = withOids;
|
||||
newToc->desc = strdup(desc);
|
||||
newToc->defn = strdup(defn);
|
||||
newToc->dropStmt = strdup(dropStmt);
|
||||
newToc->copyStmt = copyStmt ? strdup(copyStmt) : NULL;
|
||||
newToc->desc = pg_strdup(desc);
|
||||
newToc->defn = pg_strdup(defn);
|
||||
newToc->dropStmt = pg_strdup(dropStmt);
|
||||
newToc->copyStmt = copyStmt ? pg_strdup(copyStmt) : NULL;
|
||||
|
||||
if (nDeps > 0)
|
||||
{
|
||||
newToc->dependencies = (DumpId *) malloc(nDeps * sizeof(DumpId));
|
||||
newToc->dependencies = (DumpId *) pg_malloc(nDeps * sizeof(DumpId));
|
||||
memcpy(newToc->dependencies, deps, nDeps * sizeof(DumpId));
|
||||
newToc->nDeps = nDeps;
|
||||
}
|
||||
@@ -1032,7 +1031,7 @@ SortTocFromFile(Archive *AHX, RestoreOptions *ropt)
|
||||
bool incomplete_line;
|
||||
|
||||
/* Allocate space for the 'wanted' array, and init it */
|
||||
ropt->idWanted = (bool *) malloc(sizeof(bool) * AH->maxDumpId);
|
||||
ropt->idWanted = (bool *) pg_malloc(sizeof(bool) * AH->maxDumpId);
|
||||
memset(ropt->idWanted, 0, sizeof(bool) * AH->maxDumpId);
|
||||
|
||||
/* Setup the file */
|
||||
@@ -1120,7 +1119,7 @@ InitDummyWantedList(Archive *AHX, RestoreOptions *ropt)
|
||||
ArchiveHandle *AH = (ArchiveHandle *) AHX;
|
||||
|
||||
/* Allocate space for the 'wanted' array, and init it to 1's */
|
||||
ropt->idWanted = (bool *) malloc(sizeof(bool) * AH->maxDumpId);
|
||||
ropt->idWanted = (bool *) pg_malloc(sizeof(bool) * AH->maxDumpId);
|
||||
memset(ropt->idWanted, 1, sizeof(bool) * AH->maxDumpId);
|
||||
}
|
||||
|
||||
@@ -1155,9 +1154,7 @@ archprintf(Archive *AH, const char *fmt,...)
|
||||
if (p != NULL)
|
||||
free(p);
|
||||
bSize *= 2;
|
||||
p = (char *) malloc(bSize);
|
||||
if (p == NULL)
|
||||
exit_horribly(AH, modulename, "out of memory\n");
|
||||
p = (char *) pg_malloc(bSize);
|
||||
va_start(ap, fmt);
|
||||
cnt = vsnprintf(p, bSize, fmt, ap);
|
||||
va_end(ap);
|
||||
@@ -1286,9 +1283,7 @@ ahprintf(ArchiveHandle *AH, const char *fmt,...)
|
||||
if (p != NULL)
|
||||
free(p);
|
||||
bSize *= 2;
|
||||
p = (char *) malloc(bSize);
|
||||
if (p == NULL)
|
||||
die_horribly(AH, modulename, "out of memory\n");
|
||||
p = (char *) pg_malloc(bSize);
|
||||
va_start(ap, fmt);
|
||||
cnt = vsnprintf(p, bSize, fmt, ap);
|
||||
va_end(ap);
|
||||
@@ -1756,10 +1751,7 @@ ReadStr(ArchiveHandle *AH)
|
||||
buf = NULL;
|
||||
else
|
||||
{
|
||||
buf = (char *) malloc(l + 1);
|
||||
if (!buf)
|
||||
die_horribly(AH, modulename, "out of memory\n");
|
||||
|
||||
buf = (char *) pg_malloc(l + 1);
|
||||
if ((*AH->ReadBufPtr) (AH, (void *) buf, l) != l)
|
||||
die_horribly(AH, modulename, "unexpected end of file\n");
|
||||
|
||||
@@ -1785,7 +1777,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
|
||||
free(AH->lookahead);
|
||||
|
||||
AH->lookaheadSize = 512;
|
||||
AH->lookahead = calloc(1, 512);
|
||||
AH->lookahead = pg_calloc(1, 512);
|
||||
AH->lookaheadLen = 0;
|
||||
AH->lookaheadPos = 0;
|
||||
|
||||
@@ -1950,9 +1942,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
|
||||
write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt);
|
||||
#endif
|
||||
|
||||
AH = (ArchiveHandle *) calloc(1, sizeof(ArchiveHandle));
|
||||
if (!AH)
|
||||
die_horribly(AH, modulename, "out of memory\n");
|
||||
AH = (ArchiveHandle *) pg_calloc(1, sizeof(ArchiveHandle));
|
||||
|
||||
/* AH->debugLevel = 100; */
|
||||
|
||||
@@ -1979,12 +1969,12 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
|
||||
AH->offSize = sizeof(pgoff_t);
|
||||
if (FileSpec)
|
||||
{
|
||||
AH->fSpec = strdup(FileSpec);
|
||||
AH->fSpec = pg_strdup(FileSpec);
|
||||
|
||||
/*
|
||||
* Not used; maybe later....
|
||||
*
|
||||
* AH->workDir = strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ;
|
||||
* AH->workDir = pg_strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ;
|
||||
* i--) if (AH->workDir[i-1] == '/')
|
||||
*/
|
||||
}
|
||||
@@ -1996,9 +1986,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
|
||||
AH->currTablespace = NULL; /* ditto */
|
||||
AH->currWithOids = -1; /* force SET */
|
||||
|
||||
AH->toc = (TocEntry *) calloc(1, sizeof(TocEntry));
|
||||
if (!AH->toc)
|
||||
die_horribly(AH, modulename, "out of memory\n");
|
||||
AH->toc = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
|
||||
|
||||
AH->toc->next = AH->toc;
|
||||
AH->toc->prev = AH->toc;
|
||||
@@ -2169,7 +2157,7 @@ ReadToc(ArchiveHandle *AH)
|
||||
|
||||
for (i = 0; i < AH->tocCount; i++)
|
||||
{
|
||||
te = (TocEntry *) calloc(1, sizeof(TocEntry));
|
||||
te = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
|
||||
te->dumpId = ReadInt(AH);
|
||||
|
||||
if (te->dumpId > AH->maxDumpId)
|
||||
@@ -2255,7 +2243,7 @@ ReadToc(ArchiveHandle *AH)
|
||||
if (AH->version >= K_VERS_1_5)
|
||||
{
|
||||
depSize = 100;
|
||||
deps = (DumpId *) malloc(sizeof(DumpId) * depSize);
|
||||
deps = (DumpId *) pg_malloc(sizeof(DumpId) * depSize);
|
||||
depIdx = 0;
|
||||
for (;;)
|
||||
{
|
||||
@@ -2315,7 +2303,7 @@ static void
|
||||
processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
|
||||
{
|
||||
/* te->defn should have the form SET client_encoding = 'foo'; */
|
||||
char *defn = strdup(te->defn);
|
||||
char *defn = pg_strdup(te->defn);
|
||||
char *ptr1;
|
||||
char *ptr2 = NULL;
|
||||
int encoding;
|
||||
@@ -2660,7 +2648,7 @@ _becomeUser(ArchiveHandle *AH, const char *user)
|
||||
*/
|
||||
if (AH->currUser)
|
||||
free(AH->currUser);
|
||||
AH->currUser = strdup(user);
|
||||
AH->currUser = pg_strdup(user);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -2729,7 +2717,7 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
|
||||
|
||||
if (AH->currSchema)
|
||||
free(AH->currSchema);
|
||||
AH->currSchema = strdup(schemaName);
|
||||
AH->currSchema = pg_strdup(schemaName);
|
||||
|
||||
destroyPQExpBuffer(qry);
|
||||
}
|
||||
@@ -2790,7 +2778,7 @@ _selectTablespace(ArchiveHandle *AH, const char *tablespace)
|
||||
|
||||
if (AH->currTablespace)
|
||||
free(AH->currTablespace);
|
||||
AH->currTablespace = strdup(want);
|
||||
AH->currTablespace = pg_strdup(want);
|
||||
|
||||
destroyPQExpBuffer(qry);
|
||||
}
|
||||
@@ -2872,7 +2860,7 @@ _getObjectDescription(PQExpBuffer buf, TocEntry *te, ArchiveHandle *AH)
|
||||
strcmp(type, "OPERATOR FAMILY") == 0)
|
||||
{
|
||||
/* Chop "DROP " off the front and make a modifiable copy */
|
||||
char *first = strdup(te->dropStmt + 5);
|
||||
char *first = pg_strdup(te->dropStmt + 5);
|
||||
char *last;
|
||||
|
||||
/* point to last character in string */
|
||||
@@ -3279,7 +3267,7 @@ restore_toc_entries_parallel(ArchiveHandle *AH)
|
||||
|
||||
ahlog(AH, 2, "entering restore_toc_entries_parallel\n");
|
||||
|
||||
slots = (ParallelSlot *) calloc(sizeof(ParallelSlot), n_slots);
|
||||
slots = (ParallelSlot *) pg_calloc(sizeof(ParallelSlot), n_slots);
|
||||
|
||||
/* Adjust dependency information */
|
||||
fix_dependencies(AH);
|
||||
@@ -3431,7 +3419,7 @@ restore_toc_entries_parallel(ArchiveHandle *AH)
|
||||
par_list_remove(next_work_item);
|
||||
|
||||
/* this memory is dealloced in mark_work_done() */
|
||||
args = malloc(sizeof(RestoreArgs));
|
||||
args = pg_malloc(sizeof(RestoreArgs));
|
||||
args->AH = CloneArchive(AH);
|
||||
args->te = next_work_item;
|
||||
|
||||
@@ -3550,7 +3538,7 @@ reap_child(ParallelSlot *slots, int n_slots, int *work_status)
|
||||
|
||||
/* first time around only, make space for handles to listen on */
|
||||
if (handles == NULL)
|
||||
handles = (HANDLE *) calloc(sizeof(HANDLE), n_slots);
|
||||
handles = (HANDLE *) pg_calloc(sizeof(HANDLE), n_slots);
|
||||
|
||||
/* set up list of handles to listen to */
|
||||
for (snum = 0, tnum = 0; snum < n_slots; snum++)
|
||||
@@ -3898,7 +3886,7 @@ fix_dependencies(ArchiveHandle *AH)
|
||||
* the TOC items are marked as not being in any parallel-processing list.
|
||||
*/
|
||||
maxDumpId = AH->maxDumpId;
|
||||
tocsByDumpId = (TocEntry **) calloc(maxDumpId, sizeof(TocEntry *));
|
||||
tocsByDumpId = (TocEntry **) pg_calloc(maxDumpId, sizeof(TocEntry *));
|
||||
for (te = AH->toc->next; te != AH->toc; te = te->next)
|
||||
{
|
||||
tocsByDumpId[te->dumpId - 1] = te;
|
||||
@@ -3958,7 +3946,7 @@ fix_dependencies(ArchiveHandle *AH)
|
||||
{
|
||||
if (strcmp(te2->desc, "BLOBS") == 0)
|
||||
{
|
||||
te->dependencies = (DumpId *) malloc(sizeof(DumpId));
|
||||
te->dependencies = (DumpId *) pg_malloc(sizeof(DumpId));
|
||||
te->dependencies[0] = te2->dumpId;
|
||||
te->nDeps++;
|
||||
te->depCount++;
|
||||
@@ -4000,7 +3988,7 @@ fix_dependencies(ArchiveHandle *AH)
|
||||
for (te = AH->toc->next; te != AH->toc; te = te->next)
|
||||
{
|
||||
if (te->nRevDeps > 0)
|
||||
te->revDeps = (DumpId *) malloc(te->nRevDeps * sizeof(DumpId));
|
||||
te->revDeps = (DumpId *) pg_malloc(te->nRevDeps * sizeof(DumpId));
|
||||
te->nRevDeps = 0;
|
||||
}
|
||||
|
||||
@@ -4092,7 +4080,7 @@ identify_locking_dependencies(TocEntry *te)
|
||||
* that all the entry types we are interested in here are POST_DATA, so
|
||||
* they will all have been changed this way.)
|
||||
*/
|
||||
lockids = (DumpId *) malloc(te->nDeps * sizeof(DumpId));
|
||||
lockids = (DumpId *) pg_malloc(te->nDeps * sizeof(DumpId));
|
||||
nlockids = 0;
|
||||
for (i = 0; i < te->nDeps; i++)
|
||||
{
|
||||
@@ -4204,9 +4192,7 @@ CloneArchive(ArchiveHandle *AH)
|
||||
ArchiveHandle *clone;
|
||||
|
||||
/* Make a "flat" copy */
|
||||
clone = (ArchiveHandle *) malloc(sizeof(ArchiveHandle));
|
||||
if (clone == NULL)
|
||||
die_horribly(AH, modulename, "out of memory\n");
|
||||
clone = (ArchiveHandle *) pg_malloc(sizeof(ArchiveHandle));
|
||||
memcpy(clone, AH, sizeof(ArchiveHandle));
|
||||
|
||||
/* Handle format-independent fields ... none at the moment */
|
||||
@@ -4220,7 +4206,7 @@ CloneArchive(ArchiveHandle *AH)
|
||||
|
||||
/* savedPassword must be local in case we change it while connecting */
|
||||
if (clone->savedPassword)
|
||||
clone->savedPassword = strdup(clone->savedPassword);
|
||||
clone->savedPassword = pg_strdup(clone->savedPassword);
|
||||
|
||||
/* clone has its own error count, too */
|
||||
clone->public.n_errors = 0;
|
||||
|
Reference in New Issue
Block a user