1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2025-09-16 09:26:30 +02:00

[PBCKP-528] Fixed memory leaks and some minor bugs.

This commit is contained in:
Victor Spirin
2023-03-16 12:47:03 +03:00
parent 2e6d20dba0
commit 91ebe718ba
8 changed files with 54 additions and 12 deletions

View File

@@ -891,7 +891,7 @@ catalog_get_instance_list(CatalogState *catalogState)
instanceState = pgut_new(InstanceState);
strncpy(instanceState->instance_name, dent->d_name, MAXPGPATH);
strlcpy(instanceState->instance_name, dent->d_name, MAXPGPATH);
join_path_components(instanceState->instance_backup_subdir_path,
catalogState->backup_subdir_path, instanceState->instance_name);
join_path_components(instanceState->instance_wal_subdir_path,
@@ -2245,6 +2245,12 @@ do_set_backup(InstanceState *instanceState, time_t backup_id,
if (set_backup_params->note)
add_note(target_backup, set_backup_params->note);
/* Cleanup */
if (backup_list)
{
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
}
}
/*
@@ -2310,6 +2316,7 @@ add_note(pgBackup *target_backup, char *note)
{
char *note_string;
char *p;
/* unset note */
if (pg_strcasecmp(note, "none") == 0)
@@ -2326,8 +2333,8 @@ add_note(pgBackup *target_backup, char *note)
* we save only "aaa"
* Example: tests.set_backup.SetBackupTest.test_add_note_newlines
*/
note_string = pgut_malloc(MAX_NOTE_SIZE);
sscanf(note, "%[^\n]", note_string);
p = strchr(note, '\n');
note_string = pgut_strndup(note, p ? (p-note) : MAX_NOTE_SIZE);
target_backup->note = note_string;
elog(INFO, "Adding note to backup %s: '%s'",

View File

@@ -158,7 +158,13 @@ void do_retention(InstanceState *instanceState, bool no_validate, bool no_sync)
/* Retention is disabled but we still can cleanup wal */
elog(WARNING, "Retention policy is not set");
if (!delete_wal)
{
parray_walk(backup_list, pgBackupFree);
parray_free(backup_list);
parray_free(to_keep_list);
parray_free(to_purge_list);
return;
}
}
else
/* At least one retention policy is active */
@@ -1047,6 +1053,8 @@ do_delete_status(InstanceState *instanceState, InstanceConfig *instance_config,
if (parray_num(backup_list) == 0)
{
elog(WARNING, "Instance '%s' has no backups", instanceState->instance_name);
parray_free(delete_list);
parray_free(backup_list);
return;
}

View File

@@ -151,7 +151,7 @@ dir_create_dir(const char *dir, mode_t mode, bool strict)
{
char parent[MAXPGPATH];
strncpy(parent, dir, MAXPGPATH);
strlcpy(parent, dir, MAXPGPATH);
get_parent_directory(parent);
/* Create parent first */
@@ -964,7 +964,7 @@ create_data_directories(parray *dest_files, const char *data_dir, const char *ba
if (links)
{
/* get parent dir of rel_path */
strncpy(parent_dir, dir->rel_path, MAXPGPATH);
strlcpy(parent_dir, dir->rel_path, MAXPGPATH);
get_parent_directory(parent_dir);
/* check if directory is actually link to tablespace */

View File

@@ -887,7 +887,7 @@ merge_rename:
pfree(threads);
}
if (result_filelist && parray_num(result_filelist) > 0)
if (result_filelist)
{
parray_walk(result_filelist, pgFileFree);
parray_free(result_filelist);
@@ -1067,7 +1067,7 @@ merge_files(void *arg)
tmp_file->hdr_crc = file->hdr_crc;
}
else
tmp_file->uncompressed_size = tmp_file->uncompressed_size;
tmp_file->uncompressed_size = file->uncompressed_size;
/* Copy header metadata from old map into a new one */
tmp_file->n_headers = file->n_headers;

View File

@@ -452,7 +452,7 @@ print_backup_json_object(PQExpBuffer buf, pgBackup *backup)
appendPQExpBuffer(buf, INT64_FORMAT, backup->uncompressed_bytes);
}
if (backup->uncompressed_bytes >= 0)
if (backup->pgdata_bytes >= 0)
{
json_add_key(buf, "pgdata-bytes", json_level);
appendPQExpBuffer(buf, INT64_FORMAT, backup->pgdata_bytes);
@@ -514,6 +514,8 @@ show_backup(InstanceState *instanceState, time_t requested_backup_id)
elog(INFO, "Requested backup \"%s\" is not found.",
/* We do not need free base36enc's result, we exit anyway */
base36enc(requested_backup_id));
parray_walk(backups, pgBackupFree);
parray_free(backups);
/* This is not error */
return 0;
}

View File

@@ -648,7 +648,7 @@ start_WAL_streaming(PGconn *backup_conn, char *stream_dst_path, ConnectionOption
//TODO Add a comment about this calculation
stream_stop_timeout = stream_stop_timeout + stream_stop_timeout * 0.1;
strncpy(stream_thread_arg.basedir, stream_dst_path, sizeof(stream_thread_arg.basedir));
strlcpy(stream_thread_arg.basedir, stream_dst_path, sizeof(stream_thread_arg.basedir));
/*
* Connect in replication mode to the server.

View File

@@ -1177,7 +1177,8 @@ parse_time(const char *value, time_t *result, bool utc_default)
char *local_tz = getenv("TZ");
/* tmp = replace( value, !isalnum, ' ' ) */
tmp = pgut_malloc(strlen(value) + + 1);
tmp = pgut_malloc(strlen(value) + 1);
if(!tmp) return false;
len = 0;
fields_num = 1;
@@ -1205,7 +1206,10 @@ parse_time(const char *value, time_t *result, bool utc_default)
errno = 0;
hr = strtol(value + 1, &cp, 10);
if ((value + 1) == cp || errno == ERANGE)
{
pfree(tmp);
return false;
}
/* explicit delimiter? */
if (*cp == ':')
@@ -1213,13 +1217,19 @@ parse_time(const char *value, time_t *result, bool utc_default)
errno = 0;
min = strtol(cp + 1, &cp, 10);
if (errno == ERANGE)
{
pfree(tmp);
return false;
}
if (*cp == ':')
{
errno = 0;
sec = strtol(cp + 1, &cp, 10);
if (errno == ERANGE)
{
pfree(tmp);
return false;
}
}
}
/* otherwise, might have run things together... */
@@ -1234,11 +1244,20 @@ parse_time(const char *value, time_t *result, bool utc_default)
/* Range-check the values; see notes in datatype/timestamp.h */
if (hr < 0 || hr > MAX_TZDISP_HOUR)
{
pfree(tmp);
return false;
}
if (min < 0 || min >= MINS_PER_HOUR)
{
pfree(tmp);
return false;
}
if (sec < 0 || sec >= SECS_PER_MINUTE)
{
pfree(tmp);
return false;
}
tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE + sec;
if (*value == '-')
@@ -1251,7 +1270,10 @@ parse_time(const char *value, time_t *result, bool utc_default)
}
/* wrong format */
else if (!IsSpace(*value))
{
pfree(tmp);
return false;
}
else
value++;
}
@@ -1268,7 +1290,7 @@ parse_time(const char *value, time_t *result, bool utc_default)
i = sscanf(tmp, "%04d %02d %02d %02d %02d %02d%1s",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec, junk);
free(tmp);
pfree(tmp);
if (i < 3 || i > 6)
return false;

View File

@@ -1215,13 +1215,16 @@ pgut_pgfnames(const char *path, bool strict)
}
}
filenames[numnames] = NULL;
if (errno)
{
elog(strict ? ERROR : WARNING, "could not read directory \"%s\": %m", path);
pgut_pgfnames_cleanup(filenames);
closedir(dir);
return NULL;
}
filenames[numnames] = NULL;
if (closedir(dir))
{