Skip to content

Commit 91ebe71

Browse files
committed
[PBCKP-528] Fixed memory leaks and some minor bugs.
1 parent 2e6d20d commit 91ebe71

File tree

8 files changed

+54
-12
lines changed

8 files changed

+54
-12
lines changed

Diff for: src/catalog.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ catalog_get_instance_list(CatalogState *catalogState)
891891

892892
instanceState = pgut_new(InstanceState);
893893

894-
strncpy(instanceState->instance_name, dent->d_name, MAXPGPATH);
894+
strlcpy(instanceState->instance_name, dent->d_name, MAXPGPATH);
895895
join_path_components(instanceState->instance_backup_subdir_path,
896896
catalogState->backup_subdir_path, instanceState->instance_name);
897897
join_path_components(instanceState->instance_wal_subdir_path,
@@ -2245,6 +2245,12 @@ do_set_backup(InstanceState *instanceState, time_t backup_id,
22452245

22462246
if (set_backup_params->note)
22472247
add_note(target_backup, set_backup_params->note);
2248+
/* Cleanup */
2249+
if (backup_list)
2250+
{
2251+
parray_walk(backup_list, pgBackupFree);
2252+
parray_free(backup_list);
2253+
}
22482254
}
22492255

22502256
/*
@@ -2310,6 +2316,7 @@ add_note(pgBackup *target_backup, char *note)
23102316
{
23112317

23122318
char *note_string;
2319+
char *p;
23132320

23142321
/* unset note */
23152322
if (pg_strcasecmp(note, "none") == 0)
@@ -2326,8 +2333,8 @@ add_note(pgBackup *target_backup, char *note)
23262333
* we save only "aaa"
23272334
* Example: tests.set_backup.SetBackupTest.test_add_note_newlines
23282335
*/
2329-
note_string = pgut_malloc(MAX_NOTE_SIZE);
2330-
sscanf(note, "%[^\n]", note_string);
2336+
p = strchr(note, '\n');
2337+
note_string = pgut_strndup(note, p ? (p-note) : MAX_NOTE_SIZE);
23312338

23322339
target_backup->note = note_string;
23332340
elog(INFO, "Adding note to backup %s: '%s'",

Diff for: src/delete.c

+8
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ void do_retention(InstanceState *instanceState, bool no_validate, bool no_sync)
158158
/* Retention is disabled but we still can cleanup wal */
159159
elog(WARNING, "Retention policy is not set");
160160
if (!delete_wal)
161+
{
162+
parray_walk(backup_list, pgBackupFree);
163+
parray_free(backup_list);
164+
parray_free(to_keep_list);
165+
parray_free(to_purge_list);
161166
return;
167+
}
162168
}
163169
else
164170
/* At least one retention policy is active */
@@ -1047,6 +1053,8 @@ do_delete_status(InstanceState *instanceState, InstanceConfig *instance_config,
10471053
if (parray_num(backup_list) == 0)
10481054
{
10491055
elog(WARNING, "Instance '%s' has no backups", instanceState->instance_name);
1056+
parray_free(delete_list);
1057+
parray_free(backup_list);
10501058
return;
10511059
}
10521060

Diff for: src/dir.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ dir_create_dir(const char *dir, mode_t mode, bool strict)
151151
{
152152
char parent[MAXPGPATH];
153153

154-
strncpy(parent, dir, MAXPGPATH);
154+
strlcpy(parent, dir, MAXPGPATH);
155155
get_parent_directory(parent);
156156

157157
/* Create parent first */
@@ -964,7 +964,7 @@ create_data_directories(parray *dest_files, const char *data_dir, const char *ba
964964
if (links)
965965
{
966966
/* get parent dir of rel_path */
967-
strncpy(parent_dir, dir->rel_path, MAXPGPATH);
967+
strlcpy(parent_dir, dir->rel_path, MAXPGPATH);
968968
get_parent_directory(parent_dir);
969969

970970
/* check if directory is actually link to tablespace */

Diff for: src/merge.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ merge_chain(InstanceState *instanceState,
887887
pfree(threads);
888888
}
889889

890-
if (result_filelist && parray_num(result_filelist) > 0)
890+
if (result_filelist)
891891
{
892892
parray_walk(result_filelist, pgFileFree);
893893
parray_free(result_filelist);
@@ -1067,7 +1067,7 @@ merge_files(void *arg)
10671067
tmp_file->hdr_crc = file->hdr_crc;
10681068
}
10691069
else
1070-
tmp_file->uncompressed_size = tmp_file->uncompressed_size;
1070+
tmp_file->uncompressed_size = file->uncompressed_size;
10711071

10721072
/* Copy header metadata from old map into a new one */
10731073
tmp_file->n_headers = file->n_headers;

Diff for: src/show.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ print_backup_json_object(PQExpBuffer buf, pgBackup *backup)
452452
appendPQExpBuffer(buf, INT64_FORMAT, backup->uncompressed_bytes);
453453
}
454454

455-
if (backup->uncompressed_bytes >= 0)
455+
if (backup->pgdata_bytes >= 0)
456456
{
457457
json_add_key(buf, "pgdata-bytes", json_level);
458458
appendPQExpBuffer(buf, INT64_FORMAT, backup->pgdata_bytes);
@@ -514,6 +514,8 @@ show_backup(InstanceState *instanceState, time_t requested_backup_id)
514514
elog(INFO, "Requested backup \"%s\" is not found.",
515515
/* We do not need free base36enc's result, we exit anyway */
516516
base36enc(requested_backup_id));
517+
parray_walk(backups, pgBackupFree);
518+
parray_free(backups);
517519
/* This is not error */
518520
return 0;
519521
}

Diff for: src/stream.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ start_WAL_streaming(PGconn *backup_conn, char *stream_dst_path, ConnectionOption
648648
//TODO Add a comment about this calculation
649649
stream_stop_timeout = stream_stop_timeout + stream_stop_timeout * 0.1;
650650

651-
strncpy(stream_thread_arg.basedir, stream_dst_path, sizeof(stream_thread_arg.basedir));
651+
strlcpy(stream_thread_arg.basedir, stream_dst_path, sizeof(stream_thread_arg.basedir));
652652

653653
/*
654654
* Connect in replication mode to the server.

Diff for: src/utils/configuration.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,8 @@ parse_time(const char *value, time_t *result, bool utc_default)
11771177
char *local_tz = getenv("TZ");
11781178

11791179
/* tmp = replace( value, !isalnum, ' ' ) */
1180-
tmp = pgut_malloc(strlen(value) + + 1);
1180+
tmp = pgut_malloc(strlen(value) + 1);
1181+
if(!tmp) return false;
11811182
len = 0;
11821183
fields_num = 1;
11831184

@@ -1205,21 +1206,30 @@ parse_time(const char *value, time_t *result, bool utc_default)
12051206
errno = 0;
12061207
hr = strtol(value + 1, &cp, 10);
12071208
if ((value + 1) == cp || errno == ERANGE)
1209+
{
1210+
pfree(tmp);
12081211
return false;
1212+
}
12091213

12101214
/* explicit delimiter? */
12111215
if (*cp == ':')
12121216
{
12131217
errno = 0;
12141218
min = strtol(cp + 1, &cp, 10);
12151219
if (errno == ERANGE)
1220+
{
1221+
pfree(tmp);
12161222
return false;
1223+
}
12171224
if (*cp == ':')
12181225
{
12191226
errno = 0;
12201227
sec = strtol(cp + 1, &cp, 10);
12211228
if (errno == ERANGE)
1229+
{
1230+
pfree(tmp);
12221231
return false;
1232+
}
12231233
}
12241234
}
12251235
/* otherwise, might have run things together... */
@@ -1234,11 +1244,20 @@ parse_time(const char *value, time_t *result, bool utc_default)
12341244

12351245
/* Range-check the values; see notes in datatype/timestamp.h */
12361246
if (hr < 0 || hr > MAX_TZDISP_HOUR)
1247+
{
1248+
pfree(tmp);
12371249
return false;
1250+
}
12381251
if (min < 0 || min >= MINS_PER_HOUR)
1252+
{
1253+
pfree(tmp);
12391254
return false;
1255+
}
12401256
if (sec < 0 || sec >= SECS_PER_MINUTE)
1257+
{
1258+
pfree(tmp);
12411259
return false;
1260+
}
12421261

12431262
tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE + sec;
12441263
if (*value == '-')
@@ -1251,7 +1270,10 @@ parse_time(const char *value, time_t *result, bool utc_default)
12511270
}
12521271
/* wrong format */
12531272
else if (!IsSpace(*value))
1273+
{
1274+
pfree(tmp);
12541275
return false;
1276+
}
12551277
else
12561278
value++;
12571279
}
@@ -1268,7 +1290,7 @@ parse_time(const char *value, time_t *result, bool utc_default)
12681290
i = sscanf(tmp, "%04d %02d %02d %02d %02d %02d%1s",
12691291
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
12701292
&tm.tm_hour, &tm.tm_min, &tm.tm_sec, junk);
1271-
free(tmp);
1293+
pfree(tmp);
12721294

12731295
if (i < 3 || i > 6)
12741296
return false;

Diff for: src/utils/pgut.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1215,13 +1215,16 @@ pgut_pgfnames(const char *path, bool strict)
12151215
}
12161216
}
12171217

1218+
filenames[numnames] = NULL;
1219+
12181220
if (errno)
12191221
{
12201222
elog(strict ? ERROR : WARNING, "could not read directory \"%s\": %m", path);
1223+
pgut_pgfnames_cleanup(filenames);
1224+
closedir(dir);
12211225
return NULL;
12221226
}
12231227

1224-
filenames[numnames] = NULL;
12251228

12261229
if (closedir(dir))
12271230
{

0 commit comments

Comments
 (0)