From 74cd9c54dfc79179f95350c14fcbe6b1f268e478 Mon Sep 17 00:00:00 2001 From: Grigory Smolkin Date: Mon, 15 Feb 2021 15:49:30 +0300 Subject: [PATCH] [Issue #326] Handle empty history files correctly --- src/catalog.c | 4 ++++ src/restore.c | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/catalog.c b/src/catalog.c index 423e83f7..15386d42 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -1581,6 +1581,10 @@ catalog_get_timelines(InstanceConfig *instance) sscanf(file->name, "%08X.history", &tli); timelines = read_timeline_history(arclog_path, tli, true); + /* History file is empty or corrupted, disregard it */ + if (!timelines) + continue; + if (!tlinfo || tlinfo->tli != tli) { tlinfo = timelineInfoNew(tli); diff --git a/src/restore.c b/src/restore.c index adc20bb6..f28265cc 100644 --- a/src/restore.c +++ b/src/restore.c @@ -289,6 +289,9 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt, /* Read timeline history files from archives */ timelines = read_timeline_history(arclog_path, rt->target_tli, true); + if (!timelines) + elog(WARNING, "Failed to get history file for target timeline %i", rt->target_tli); + if (!satisfy_timeline(timelines, current_backup)) { if (target_backup_id != INVALID_BACKUP_ID) @@ -1778,6 +1781,14 @@ read_timeline_history(const char *arclog_path, TimeLineID targetTLI, bool strict if (last_timeline && targetTLI <= last_timeline->tli) elog(ERROR, "Timeline IDs must be less than child timeline's ID."); + /* History file is empty or corrupted */ + if (parray_num(result) != 1) + { + elog(WARNING, "History file is corrupted: \"%s\"", path); + pg_free(result); + return NULL; + } + /* append target timeline */ entry = pgut_new(TimeLineHistoryEntry); entry->tli = targetTLI;