From 07353d57a7b680a8acf2b845a9dd9efdabbde77f Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Tue, 20 Oct 2026 20:01:11 +0900 Subject: [PATCH v3 4/4] Use pg_parse_lsn() for reading backup label read_backup_label() parsed several WAL positions on the label file with fscanf("%X/%08X"). This commit convert to use the common LSN parser. Note: It assumes that the maximul length of LSN string is 17. We can avoid by exporting the fixed value MAXPG_LSNLEN. --- src/backend/access/transam/xlogrecovery.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c index 20be788da78..57a208060b8 100644 --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -44,6 +44,7 @@ #include "catalog/pg_control.h" #include "commands/tablespace.h" #include "common/file_utils.h" +#include "common/pg_parse_lsn.h" #include "miscadmin.h" #include "nodes/miscnodes.h" #include "pgstat.h" @@ -1190,8 +1191,8 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI, char backupfrom[20]; char backuplabel[MAXPGPATH]; char backuptime[128]; - uint32 hi, - lo; + char lsn[18]; + XLogRecPtr istartpoint = InvalidXLogRecPtr; /* suppress possible uninitialized-variable warnings */ *checkPointLoc = InvalidXLogRecPtr; @@ -1218,19 +1219,20 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI, * is pretty crude, but we are not expecting any variability in the file * format). */ - if (fscanf(lfp, "START WAL LOCATION: %X/%08X (file %08X%16s)%c", - &hi, &lo, &tli_from_walseg, startxlogfilename, &ch) != 5 || ch != '\n') + if (fscanf(lfp, "START WAL LOCATION: %17s (file %08X%16s)%c", + lsn, &tli_from_walseg, startxlogfilename, &ch) != 4 || + ch != '\n' || + !pg_parse_lsn(lsn, &RedoStartLSN)) ereport(FATAL, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE))); - RedoStartLSN = ((uint64) hi) << 32 | lo; RedoStartTLI = tli_from_walseg; - if (fscanf(lfp, "CHECKPOINT LOCATION: %X/%08X%c", - &hi, &lo, &ch) != 3 || ch != '\n') + if (fscanf(lfp, "CHECKPOINT LOCATION: %17s%c", lsn, &ch) != 2 || + ch != '\n' || + !pg_parse_lsn(lsn, checkPointLoc)) ereport(FATAL, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE))); - *checkPointLoc = ((uint64) hi) << 32 | lo; *backupLabelTLI = tli_from_walseg; /* @@ -1297,7 +1299,8 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI, tli_from_file, BACKUP_LABEL_FILE))); } - if (fscanf(lfp, "INCREMENTAL FROM LSN: %X/%08X\n", &hi, &lo) > 0) + if (fscanf(lfp, "INCREMENTAL FROM LSN: %17s\n", lsn) > 0 || + !pg_parse_lsn(lsn, &istartpoint)) ereport(FATAL, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("this is an incremental backup, not a data directory"), -- 2.52.0