pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedSTART_REPLICATION silently truncates an overlong LSN component
20+ messages / 5 participants
[nested] [flat]
Commits from this thread:
2026-10-29 76a55702d8 Use pg_parse_lsn() to parse LSN fields in backup_label
2026-10-22 5ffb8c7409 Use pg_parse_lsn() for timeline history switchpoints
2026-10-22 ba6d9ee259 Use pg_parse_lsn() for IDENTIFY_SYSTEM responses in libpqwalreceiver
2026-10-22 4a842a1d65 Reject overlong LSN components in START_REPLICATION
* START_REPLICATION silently truncates an overlong LSN component
@ 2026-09-08 09:58 ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
0 siblings, 1 reply; 20+ messages in thread
From: ♂π≌26218 @ 2026-09-08 09:58 UTC (permalink / raw)
To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi, I found a potential bug in PostgreSQL's replication command parser where `START_REPLICATION` accepts an LSN whose high component contains more than eight hexadecimal digits and silently truncates it to a different WAL location, rather than rejecting it. Description: The replication command `START_REPLICATION` accepts an LSN in the format `X/Y`, where `X` and `Y` are hexadecimal numbers. The parser accepts an overlong high component (e.g., `123456789/0`) and silently maps it to `23456789/00000000` by truncating the excess digit. This means the server starts replication from a different WAL location than the user requested, without any error or warning. PostgreSQL version: - PostgreSQL 19beta3 (Docker-based runtime) - Source commit: not recorded in the original report - Build relationship: the tested image was not an exact-HEAD build Environment: - Docker-based PostgreSQL 19beta3 runtime - Replication connection privileges required - No special server configuration beyond standard replication setup Steps to Reproduce: Run the following command using a replication connection: ```bash psql "user=postgres dbname=postgres replication=database" \ -c 'START_REPLICATION 123456789/0;'
Then inspect the PostgreSQL server log. For a filing-quality rerun, enable log_error_verbosity=verbose and capture the complete relevant log section.
Actual Result:
The server log identifies a different starting point. For the command above, the log shows:
text
requested starting point 23456789/00000000 ... STATEMENT: START_REPLICATION 123456789/0;
The original 123456789/0 was interpreted as 23456789/00000000, with the high component truncated from nine digits to eight.
Expected Result:
The backend should reject an LSN component longer than eight hexadecimal digits with a syntax error or a specific "invalid LSN" error. It should not silently convert it to another WAL location.
Reproduction Frequency:
Positive reproduction: 2/2 on PostgreSQL 19beta3
Negative control: START_REPLICATION 0/0 remained 0/0
Additional Observations:
The issue likely originates from the replication scanner accepting an unrestricted hexadecimal token and parsing it into a 32-bit destination without checking the component width or whether the conversion was lossless. This could lead to replication starting from an unexpected LSN, potentially causing data inconsistencies or confusion during recovery.
I searched the public PostgreSQL bug archives and did not find any report specifically addressing LSN component overflow or truncation in START_REPLICATION. Please confirm whether this is considered a bug.
♂π≌26218
1991230470@qq.com
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
@ 2026-09-10 13:41 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Ayush Tiwari @ 2026-09-10 13:41 UTC (permalink / raw)
To: ♂π≌26218 <1991230470@qq.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; Fujii Masao <masao.fujii@gmail.com>
Hi,
Thanks for the report.
We introduced pg_parse_lsn() function for such issues.
It seems we should add it here to, attached patch does that.
[Added a TAP test in patch, but I dont think it needs to be committed]
I wonder if we should make the same change in libpqwalreceiver too.
Regards,
Ayush
>
Attachments:
v1-0001-Use-pg_parse_lsn-for-START_REPLICATION.patch (application/octet-stream, 2.5K) download
From 6ae651e19232ede52fe417001c8f8906984cafd6 Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Date: Thu, 10 Sep 2026 19:02:10 +0530
Subject: [PATCH v1] Use pg_parse_lsn() for START_REPLICATION
The replication command scanner parses LSNs with sscanf() into uint32
variables. This can silently change an LSN with an overlong component:
the high component wraps around, while the low component is truncated to
the first eight digits because of the field width in "%08X".
Use pg_parse_lsn() instead. This enforces the same limit of one to eight
hexadecimal digits per component as pg_lsn input and rejects invalid LSNs
rather than starting replication at a different location.
Test overlong high and low components in the existing logical decoding
TAP test.
---
src/backend/replication/repl_scanner.l | 6 ++----
src/test/recovery/t/006_logical_decoding.pl | 12 ++++++++++++
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/backend/replication/repl_scanner.l b/src/backend/replication/repl_scanner.l
index 70b137acedc..af1615f9dbf 100644
--- a/src/backend/replication/repl_scanner.l
+++ b/src/backend/replication/repl_scanner.l
@@ -15,6 +15,7 @@
*/
#include "postgres.h"
+#include "common/pg_parse_lsn.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "parser/scansup.h"
@@ -153,11 +154,8 @@ UPLOAD_MANIFEST { return K_UPLOAD_MANIFEST; }
}
{hexdigit}+\/{hexdigit}+ {
- uint32 hi,
- lo;
- if (sscanf(yytext, "%X/%08X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(yytext, &yylval->recptr))
replication_yyerror(NULL, yyscanner, "invalid streaming start location");
- yylval->recptr = ((uint64) hi) << 32 | lo;
return RECPTR;
}
diff --git a/src/test/recovery/t/006_logical_decoding.pl b/src/test/recovery/t/006_logical_decoding.pl
index 97d11f98b59..490e7a80c64 100644
--- a/src/test/recovery/t/006_logical_decoding.pl
+++ b/src/test/recovery/t/006_logical_decoding.pl
@@ -39,6 +39,18 @@ like(
qr/replication slot "test_slot" was not created in this database/,
"Logical decoding correctly fails to start");
+for my $lsn ('123456789/0', '0/123456789')
+{
+ ($result, $stdout, $stderr) = $node_primary->psql(
+ 'template1',
+ qq[START_REPLICATION $lsn],
+ replication => 'database');
+ like(
+ $stderr,
+ qr/invalid streaming start location/,
+ "START_REPLICATION rejects overlong LSN $lsn");
+}
+
($result, $stdout, $stderr) = $node_primary->psql(
'template1',
qq[READ_REPLICATION_SLOT test_slot;],
--
2.34.1
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-09-11 02:35 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-09-11 02:35 UTC (permalink / raw)
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; Fujii Masao <masao.fujii@gmail.com>
Dear Ayush,
> It seems we should add it here to, attached patch does that.
> [Added a TAP test in patch, but I dont think it needs to be committed]
LGTM. I also think the test is not needed.
> I wonder if we should make the same change in libpqwalreceiver too.
I feel it should be, see attached. It could be combined with your patch.
I grepped the source with the keyword "scanf\(.*%X/%08X", and I could
not find other places where pg_parse_lsn() is usable as-is.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments:
0001-Use-pg_paser_lsn-in-libpqwalreceiver.patch (application/octet-stream, 1.4K) download
From 75472f9c5a2806b3c0d9ef18126b83dc255c3482 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 11 Sep 2026 11:31:51 +0900
Subject: [PATCH] Use pg_paser_lsn in libpqwalreceiver
---
.../replication/libpqwalreceiver/libpqwalreceiver.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 029990d9fce..df47ae535ce 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -23,6 +23,7 @@
#include <sys/time.h>
#include "common/connect.h"
+#include "common/pg_parse_lsn.h"
#include "funcapi.h"
#include "libpq-fe.h"
#include "libpq/libpq-be-fe-helpers.h"
@@ -458,15 +459,11 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli,
/* Column 2 is the server's current WAL flush position */
if (server_lsn)
{
- uint32 hi,
- lo;
-
- if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(PQgetvalue(res, 0, 2), server_lsn))
ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("could not parse WAL location \"%s\"",
PQgetvalue(res, 0, 2))));
- *server_lsn = ((uint64) hi) << 32 | lo;
}
PQclear(res);
--
2.52.0
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-16 13:03 ` Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Jan Nidzwetzki @ 2026-10-16 13:03 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; Fujii Masao <masao.fujii@gmail.com>
Hi Ayush, Hayato,
Thanks for the patches. I looked into them, and both apply cleanly to
master, and check-world passes. So, these changes look good to me.
On 11.09.26 04:35, Hayato Kuroda (Fujitsu) wrote:
>> I wonder if we should make the same change in libpqwalreceiver too.
>
> I feel it should be, see attached. It could be combined with your patch.
> I grepped the source with the keyword "scanf\(.*%X/%08X", and I could
> not find other places where pg_parse_lsn() is usable as-is.
While looking for other places that need the same change, I found that
readTimeLineHistory() in src/backend/access/transam/timeline.c still
parses the switchpoint with sscanf("%X/%08X"). f31d6fbc31d fixed the
frontend copy of this code in pg_rewind, but not the backend one. The
attached 0003 patch converts it the same way and adds a TAP test to
004_timeline_switch.pl, including a case for the trailing characters
that are now rejected. As with the test in 0001, I'm not sure this one
needs to be committed.
In the attached v2 series, your patches are unchanged; I only added the
new 0003.
Best regards,
Jan
--
Jan Nidzwetzki
PlanetScale Postgres Core Team
Attachments: download all (3 files, .tar.gz)
v2-0001-Use-pg_parse_lsn-for-START_REPLICATION.patch (text/plain, 2.5K) download
From 071838f48cdc12ce4ac6e4ac4a5eec4294276cbe Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Date: Thu, 10 Sep 2026 19:02:10 +0530
Subject: [PATCH v2 1/3] Use pg_parse_lsn() for START_REPLICATION
The replication command scanner parses LSNs with sscanf() into uint32
variables. This can silently change an LSN with an overlong component:
the high component wraps around, while the low component is truncated to
the first eight digits because of the field width in "%08X".
Use pg_parse_lsn() instead. This enforces the same limit of one to eight
hexadecimal digits per component as pg_lsn input and rejects invalid LSNs
rather than starting replication at a different location.
Test overlong high and low components in the existing logical decoding
TAP test.
---
src/backend/replication/repl_scanner.l | 6 ++----
src/test/recovery/t/006_logical_decoding.pl | 12 ++++++++++++
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/backend/replication/repl_scanner.l b/src/backend/replication/repl_scanner.l
index 70b137acedc..af1615f9dbf 100644
--- a/src/backend/replication/repl_scanner.l
+++ b/src/backend/replication/repl_scanner.l
@@ -15,6 +15,7 @@
*/
#include "postgres.h"
+#include "common/pg_parse_lsn.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "parser/scansup.h"
@@ -153,11 +154,8 @@ UPLOAD_MANIFEST { return K_UPLOAD_MANIFEST; }
}
{hexdigit}+\/{hexdigit}+ {
- uint32 hi,
- lo;
- if (sscanf(yytext, "%X/%08X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(yytext, &yylval->recptr))
replication_yyerror(NULL, yyscanner, "invalid streaming start location");
- yylval->recptr = ((uint64) hi) << 32 | lo;
return RECPTR;
}
diff --git a/src/test/recovery/t/006_logical_decoding.pl b/src/test/recovery/t/006_logical_decoding.pl
index ab8364fee7a..d8adb4c3050 100644
--- a/src/test/recovery/t/006_logical_decoding.pl
+++ b/src/test/recovery/t/006_logical_decoding.pl
@@ -45,6 +45,18 @@ like(
qr/replication slot "test_slot" was not created in this database/,
"Logical decoding correctly fails to start");
+for my $lsn ('123456789/0', '0/123456789')
+{
+ ($result, $stdout, $stderr) = $node_primary->psql(
+ 'template1',
+ qq[START_REPLICATION $lsn],
+ replication => 'database');
+ like(
+ $stderr,
+ qr/invalid streaming start location/,
+ "START_REPLICATION rejects overlong LSN $lsn");
+}
+
($result, $stdout, $stderr) = $node_primary->psql(
'template1',
qq[READ_REPLICATION_SLOT test_slot;],
--
2.47.3
v2-0002-Use-pg_paser_lsn-in-libpqwalreceiver.patch (text/plain, 1.4K) download
From 635293f464a6d77fc287b596eb75be270dc41daf Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 11 Sep 2026 11:31:51 +0900
Subject: [PATCH v2 2/3] Use pg_paser_lsn in libpqwalreceiver
---
.../replication/libpqwalreceiver/libpqwalreceiver.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 029990d9fce..df47ae535ce 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -23,6 +23,7 @@
#include <sys/time.h>
#include "common/connect.h"
+#include "common/pg_parse_lsn.h"
#include "funcapi.h"
#include "libpq-fe.h"
#include "libpq/libpq-be-fe-helpers.h"
@@ -458,15 +459,11 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli,
/* Column 2 is the server's current WAL flush position */
if (server_lsn)
{
- uint32 hi,
- lo;
-
- if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(PQgetvalue(res, 0, 2), server_lsn))
ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("could not parse WAL location \"%s\"",
PQgetvalue(res, 0, 2))));
- *server_lsn = ((uint64) hi) << 32 | lo;
}
PQclear(res);
--
2.47.3
v2-0003-Use-pg_parse_lsn-for-timeline-history-switchpoint.patch (text/plain, 4.6K) download
From ed08aad4ef065d91560238ffb14c7f18976b7e15 Mon Sep 17 00:00:00 2001
From: Jan Nidzwetzki <jan@planetscale.com>
Date: Fri, 16 Oct 2026 14:24:05 +0200
Subject: [PATCH v2 3/3] Use pg_parse_lsn() for timeline history switchpoints
readTimeLineHistory() parsed the switchpoint on each history file line
with sscanf("%X/%08X"). Commit f31d6fbc31d converted the frontend copy
of this code in pg_rewind, but not the backend one. Convert it in the
same way. As in pg_rewind, characters directly after the switchpoint are
now rejected rather than ignored.
Add a TAP test covering overlong components and trailing characters.
---
src/backend/access/transam/timeline.c | 37 +++++++++++++++++-----
src/test/recovery/t/004_timeline_switch.pl | 28 ++++++++++++++++
2 files changed, 57 insertions(+), 8 deletions(-)
diff --git a/src/backend/access/transam/timeline.c b/src/backend/access/transam/timeline.c
index d80c8ffe0a7..cc27587a07c 100644
--- a/src/backend/access/transam/timeline.c
+++ b/src/backend/access/transam/timeline.c
@@ -39,6 +39,7 @@
#include "access/xlog_internal.h"
#include "access/xlogarchive.h"
#include "access/xlogdefs.h"
+#include "common/pg_parse_lsn.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "utils/wait_event.h"
@@ -128,10 +129,13 @@ readTimeLineHistory(TimeLineID targetTLI)
char fline[MAXPGPATH];
char *res;
char *ptr;
+ char *token_end;
+ char save;
TimeLineID tli;
- uint32 switchpoint_hi;
- uint32 switchpoint_lo;
- int nfields;
+ XLogRecPtr switchpoint;
+ bool valid_switchpoint = false;
+ int nchars;
+ size_t nspaces;
pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_READ);
res = fgets(fline, sizeof(fline), fd);
@@ -155,16 +159,33 @@ readTimeLineHistory(TimeLineID targetTLI)
if (*ptr == '\0' || *ptr == '#')
continue;
- nfields = sscanf(fline, "%u\t%X/%08X", &tli, &switchpoint_hi, &switchpoint_lo);
-
- if (nfields < 1)
+ if (sscanf(fline, "%u%n", &tli, &nchars) != 1)
{
/* expect a numeric timeline ID as first field of line */
ereport(FATAL,
(errmsg("syntax error in history file: %s", fline),
errhint("Expected a numeric timeline ID.")));
}
- if (nfields != 3)
+
+ /* the switchpoint location follows, separated by whitespace */
+ ptr = fline + nchars;
+ nspaces = strspn(ptr, " \t\n\r\f\v");
+ if (nspaces > 0)
+ {
+ ptr += nspaces;
+
+ /*
+ * isolate the location from the rest of the line before parsing
+ * it
+ */
+ token_end = ptr + strcspn(ptr, " \t\n\r\f\v");
+ save = *token_end;
+ *token_end = '\0';
+ valid_switchpoint = pg_parse_lsn(ptr, &switchpoint);
+ *token_end = save;
+ }
+
+ if (!valid_switchpoint)
ereport(FATAL,
(errmsg("syntax error in history file: %s", fline),
errhint("Expected a write-ahead log switchpoint location.")));
@@ -179,7 +200,7 @@ readTimeLineHistory(TimeLineID targetTLI)
entry = palloc_object(TimeLineHistoryEntry);
entry->tli = tli;
entry->begin = prevend;
- entry->end = ((uint64) (switchpoint_hi)) << 32 | (uint64) switchpoint_lo;
+ entry->end = switchpoint;
prevend = entry->end;
/* Build list with newest item first */
diff --git a/src/test/recovery/t/004_timeline_switch.pl b/src/test/recovery/t/004_timeline_switch.pl
index 4085d7d04d1..acb479a3b5c 100644
--- a/src/test/recovery/t/004_timeline_switch.pl
+++ b/src/test/recovery/t/004_timeline_switch.pl
@@ -205,4 +205,32 @@ my $temp_slot_name = $node_standby_4->safe_psql('postgres',
like($temp_slot_name, qr/^pg_walreceiver_[0-9]+$/,
'pg_stat_wal_receiver.slot_name remains set across timeline jumps');
+# Check that a history file with a bogus switchpoint is rejected.
+my $node_bad_history = PostgreSQL::Test::Cluster->new('bad_history');
+$node_bad_history->init(allows_streaming => 1);
+# Start and stop the node once before switching it to standby mode. This is
+# required so that the control file records the test settings.
+$node_bad_history->start;
+$node_bad_history->stop;
+$node_bad_history->set_standby_mode;
+$node_bad_history->append_conf('postgresql.conf',
+ "recovery_target_timeline = '2'");
+
+for my $switchpoint ('123456789/0', '0/123456789', '0/1x')
+{
+ my $history_file =
+ $node_bad_history->data_dir . '/pg_wal/00000002.history';
+ my $log_offset = -s $node_bad_history->logfile;
+
+ unlink $history_file;
+ append_to_file($history_file, "1\t$switchpoint\tbogus switchpoint\n");
+
+ ok(!$node_bad_history->start(fail_ok => 1),
+ "server fails to start with history switchpoint $switchpoint");
+ ok( $node_bad_history->log_contains(
+ qr/FATAL: .*syntax error in history file: 1\t\Q$switchpoint\E/,
+ $log_offset),
+ "history switchpoint $switchpoint is rejected");
+}
+
done_testing();
--
2.47.3
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
@ 2026-10-20 07:36 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-20 07:36 UTC (permalink / raw)
To: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; Fujii Masao <masao.fujii@gmail.com>
Hi Jan,
> While looking for other places that need the same change, I found that
> readTimeLineHistory() in src/backend/access/transam/timeline.c still
> parses the switchpoint with sscanf("%X/%08X"). f31d6fbc31d fixed the
> frontend copy of this code in pg_rewind, but not the backend one. The
> attached 0003 patch converts it the same way and adds a TAP test to
> 004_timeline_switch.pl, including a case for the trailing characters
> that are now rejected. As with the test in 0001, I'm not sure this one
> needs to be committed.
I also checked the source, and found that read_backup_label() parses the %X/%08X
in fscanf(). Can it be also replaced?
Best regards,
Hayato Kuroda
FUJITSU LIMITED
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-20 09:31 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Fujii Masao @ 2026-10-20 09:31 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Tue, Oct 20, 2026 at 4:36 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
> > As with the test in 0001, I'm not sure this one
> > needs to be committed.
I think we can omit the test in 0003. We made the similar change in pg_rewind
in f31d6fbc31d without adding a test, and pg_parse_lsn() is already covered
through other callers.
Regarding 0001 and 0002 patches, they look good to me.
So barring any objections, I will commit them.
> I also checked the source, and found that read_backup_label() parses the %X/%08X
> in fscanf(). Can it be also replaced?
I think that's possible.
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
@ 2026-10-20 11:29 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-20 11:29 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Fujii-san,
Thanks for the review.
> I think we can omit the test in 0003. We made the similar change in pg_rewind
> in f31d6fbc31d without adding a test, and pg_parse_lsn() is already covered
> through other callers.
+1.
> > I also checked the source, and found that read_backup_label() parses
> the %X/%08X
> > in fscanf(). Can it be also replaced?
>
> I think that's possible.
OK, PSA 0004. We can reduce the magic number from 0004 if we export
MAXPG_LSNLEN, but not done yet.
All patches are attached to run CI.
0001 and 0002 are not changed, tests were removed from 0003.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (4 files, .tar.gz)
v3-0001-Use-pg_parse_lsn-for-START_REPLICATION.patch (application/octet-stream, 2.5K) download
From a3f01ea7483d99e1f76654a0451b398d06e94045 Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Date: Thu, 10 Sep 2026 19:02:10 +0530
Subject: [PATCH v3 1/4] Use pg_parse_lsn() for START_REPLICATION
The replication command scanner parses LSNs with sscanf() into uint32
variables. This can silently change an LSN with an overlong component:
the high component wraps around, while the low component is truncated to
the first eight digits because of the field width in "%08X".
Use pg_parse_lsn() instead. This enforces the same limit of one to eight
hexadecimal digits per component as pg_lsn input and rejects invalid LSNs
rather than starting replication at a different location.
Test overlong high and low components in the existing logical decoding
TAP test.
---
src/backend/replication/repl_scanner.l | 6 ++----
src/test/recovery/t/006_logical_decoding.pl | 12 ++++++++++++
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/backend/replication/repl_scanner.l b/src/backend/replication/repl_scanner.l
index 70b137acedc..af1615f9dbf 100644
--- a/src/backend/replication/repl_scanner.l
+++ b/src/backend/replication/repl_scanner.l
@@ -15,6 +15,7 @@
*/
#include "postgres.h"
+#include "common/pg_parse_lsn.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "parser/scansup.h"
@@ -153,11 +154,8 @@ UPLOAD_MANIFEST { return K_UPLOAD_MANIFEST; }
}
{hexdigit}+\/{hexdigit}+ {
- uint32 hi,
- lo;
- if (sscanf(yytext, "%X/%08X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(yytext, &yylval->recptr))
replication_yyerror(NULL, yyscanner, "invalid streaming start location");
- yylval->recptr = ((uint64) hi) << 32 | lo;
return RECPTR;
}
diff --git a/src/test/recovery/t/006_logical_decoding.pl b/src/test/recovery/t/006_logical_decoding.pl
index ab8364fee7a..d8adb4c3050 100644
--- a/src/test/recovery/t/006_logical_decoding.pl
+++ b/src/test/recovery/t/006_logical_decoding.pl
@@ -45,6 +45,18 @@ like(
qr/replication slot "test_slot" was not created in this database/,
"Logical decoding correctly fails to start");
+for my $lsn ('123456789/0', '0/123456789')
+{
+ ($result, $stdout, $stderr) = $node_primary->psql(
+ 'template1',
+ qq[START_REPLICATION $lsn],
+ replication => 'database');
+ like(
+ $stderr,
+ qr/invalid streaming start location/,
+ "START_REPLICATION rejects overlong LSN $lsn");
+}
+
($result, $stdout, $stderr) = $node_primary->psql(
'template1',
qq[READ_REPLICATION_SLOT test_slot;],
--
2.52.0
v3-0002-Use-pg_paser_lsn-in-libpqwalreceiver.patch (application/octet-stream, 1.4K) download
From fc3a54e7e28950bfd5e2525aad01878caf8269bb Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 11 Sep 2026 11:31:51 +0900
Subject: [PATCH v3 2/4] Use pg_paser_lsn in libpqwalreceiver
---
.../replication/libpqwalreceiver/libpqwalreceiver.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 029990d9fce..df47ae535ce 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -23,6 +23,7 @@
#include <sys/time.h>
#include "common/connect.h"
+#include "common/pg_parse_lsn.h"
#include "funcapi.h"
#include "libpq-fe.h"
#include "libpq/libpq-be-fe-helpers.h"
@@ -458,15 +459,11 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli,
/* Column 2 is the server's current WAL flush position */
if (server_lsn)
{
- uint32 hi,
- lo;
-
- if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(PQgetvalue(res, 0, 2), server_lsn))
ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("could not parse WAL location \"%s\"",
PQgetvalue(res, 0, 2))));
- *server_lsn = ((uint64) hi) << 32 | lo;
}
PQclear(res);
--
2.52.0
v3-0003-Use-pg_parse_lsn-for-timeline-history-switchpoint.patch (application/octet-stream, 3.1K) download
From aecefc5b1a99767cecf00d3cabf53917d395cdc6 Mon Sep 17 00:00:00 2001
From: Jan Nidzwetzki <jan@planetscale.com>
Date: Fri, 16 Oct 2026 14:24:05 +0200
Subject: [PATCH v3 3/4] Use pg_parse_lsn() for timeline history switchpoints
readTimeLineHistory() parsed the switchpoint on each history file line
with sscanf("%X/%08X"). Commit f31d6fbc31d converted the frontend copy
of this code in pg_rewind, but not the backend one. Convert it in the
same way. As in pg_rewind, characters directly after the switchpoint are
now rejected rather than ignored.
Add a TAP test covering overlong components and trailing characters.
---
src/backend/access/transam/timeline.c | 37 +++++++++++++++++++++------
1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/src/backend/access/transam/timeline.c b/src/backend/access/transam/timeline.c
index d80c8ffe0a7..cc27587a07c 100644
--- a/src/backend/access/transam/timeline.c
+++ b/src/backend/access/transam/timeline.c
@@ -39,6 +39,7 @@
#include "access/xlog_internal.h"
#include "access/xlogarchive.h"
#include "access/xlogdefs.h"
+#include "common/pg_parse_lsn.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "utils/wait_event.h"
@@ -128,10 +129,13 @@ readTimeLineHistory(TimeLineID targetTLI)
char fline[MAXPGPATH];
char *res;
char *ptr;
+ char *token_end;
+ char save;
TimeLineID tli;
- uint32 switchpoint_hi;
- uint32 switchpoint_lo;
- int nfields;
+ XLogRecPtr switchpoint;
+ bool valid_switchpoint = false;
+ int nchars;
+ size_t nspaces;
pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_READ);
res = fgets(fline, sizeof(fline), fd);
@@ -155,16 +159,33 @@ readTimeLineHistory(TimeLineID targetTLI)
if (*ptr == '\0' || *ptr == '#')
continue;
- nfields = sscanf(fline, "%u\t%X/%08X", &tli, &switchpoint_hi, &switchpoint_lo);
-
- if (nfields < 1)
+ if (sscanf(fline, "%u%n", &tli, &nchars) != 1)
{
/* expect a numeric timeline ID as first field of line */
ereport(FATAL,
(errmsg("syntax error in history file: %s", fline),
errhint("Expected a numeric timeline ID.")));
}
- if (nfields != 3)
+
+ /* the switchpoint location follows, separated by whitespace */
+ ptr = fline + nchars;
+ nspaces = strspn(ptr, " \t\n\r\f\v");
+ if (nspaces > 0)
+ {
+ ptr += nspaces;
+
+ /*
+ * isolate the location from the rest of the line before parsing
+ * it
+ */
+ token_end = ptr + strcspn(ptr, " \t\n\r\f\v");
+ save = *token_end;
+ *token_end = '\0';
+ valid_switchpoint = pg_parse_lsn(ptr, &switchpoint);
+ *token_end = save;
+ }
+
+ if (!valid_switchpoint)
ereport(FATAL,
(errmsg("syntax error in history file: %s", fline),
errhint("Expected a write-ahead log switchpoint location.")));
@@ -179,7 +200,7 @@ readTimeLineHistory(TimeLineID targetTLI)
entry = palloc_object(TimeLineHistoryEntry);
entry->tli = tli;
entry->begin = prevend;
- entry->end = ((uint64) (switchpoint_hi)) << 32 | (uint64) switchpoint_lo;
+ entry->end = switchpoint;
prevend = entry->end;
/* Build list with newest item first */
--
2.52.0
v3-0004-Use-pg_parse_lsn-for-reading-backup-label.patch (application/octet-stream, 3.0K) download
From 07353d57a7b680a8acf2b845a9dd9efdabbde77f Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
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
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-21 06:36 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Fujii Masao @ 2026-10-21 06:36 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Tue, Oct 20, 2026 at 8:29 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
> OK, PSA 0004.
Thanks!
+ if (fscanf(lfp, "INCREMENTAL FROM LSN: %17s\n", lsn) > 0 ||
+ !pg_parse_lsn(lsn, &istartpoint))
When fscanf() succeeds, pg_parse_lsn() is skipped. If the line is absent,
it parses the previous CHECKPOINT LOCATION value instead. Isn't this
problematic?
Since we don't use this LSN, how about removing the pg_parse_lsn() check
and istartpoint and simply rejecting the backup if fscanf() returns > 0?
> We can reduce the magic number from 0004 if we export
> MAXPG_LSNLEN
+1
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
@ 2026-10-21 09:57 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-21 09:57 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Fujii-san,
> + if (fscanf(lfp, "INCREMENTAL FROM LSN: %17s\n", lsn) > 0 ||
> + !pg_parse_lsn(lsn, &istartpoint))
>
> When fscanf() succeeds, pg_parse_lsn() is skipped. If the line is absent,
> it parses the previous CHECKPOINT LOCATION value instead. Isn't this
> problematic?
>
> Since we don't use this LSN, how about removing the pg_parse_lsn() check
> and istartpoint and simply rejecting the backup if fscanf() returns > 0?
Thanks for the feedback. PSA the fixed version. MAXPG_LSNLEN was also
exported in the header.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (4 files, .tar.gz)
v4-0001-Use-pg_parse_lsn-for-START_REPLICATION.patch (application/octet-stream, 2.5K) download
From 45b68f8d541ffe201c38fa2ea65ceb2a7e5e69ab Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Date: Thu, 10 Sep 2026 19:02:10 +0530
Subject: [PATCH v4 1/4] Use pg_parse_lsn() for START_REPLICATION
The replication command scanner parses LSNs with sscanf() into uint32
variables. This can silently change an LSN with an overlong component:
the high component wraps around, while the low component is truncated to
the first eight digits because of the field width in "%08X".
Use pg_parse_lsn() instead. This enforces the same limit of one to eight
hexadecimal digits per component as pg_lsn input and rejects invalid LSNs
rather than starting replication at a different location.
Test overlong high and low components in the existing logical decoding
TAP test.
---
src/backend/replication/repl_scanner.l | 6 ++----
src/test/recovery/t/006_logical_decoding.pl | 12 ++++++++++++
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/backend/replication/repl_scanner.l b/src/backend/replication/repl_scanner.l
index 70b137acedc..af1615f9dbf 100644
--- a/src/backend/replication/repl_scanner.l
+++ b/src/backend/replication/repl_scanner.l
@@ -15,6 +15,7 @@
*/
#include "postgres.h"
+#include "common/pg_parse_lsn.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "parser/scansup.h"
@@ -153,11 +154,8 @@ UPLOAD_MANIFEST { return K_UPLOAD_MANIFEST; }
}
{hexdigit}+\/{hexdigit}+ {
- uint32 hi,
- lo;
- if (sscanf(yytext, "%X/%08X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(yytext, &yylval->recptr))
replication_yyerror(NULL, yyscanner, "invalid streaming start location");
- yylval->recptr = ((uint64) hi) << 32 | lo;
return RECPTR;
}
diff --git a/src/test/recovery/t/006_logical_decoding.pl b/src/test/recovery/t/006_logical_decoding.pl
index ab8364fee7a..d8adb4c3050 100644
--- a/src/test/recovery/t/006_logical_decoding.pl
+++ b/src/test/recovery/t/006_logical_decoding.pl
@@ -45,6 +45,18 @@ like(
qr/replication slot "test_slot" was not created in this database/,
"Logical decoding correctly fails to start");
+for my $lsn ('123456789/0', '0/123456789')
+{
+ ($result, $stdout, $stderr) = $node_primary->psql(
+ 'template1',
+ qq[START_REPLICATION $lsn],
+ replication => 'database');
+ like(
+ $stderr,
+ qr/invalid streaming start location/,
+ "START_REPLICATION rejects overlong LSN $lsn");
+}
+
($result, $stdout, $stderr) = $node_primary->psql(
'template1',
qq[READ_REPLICATION_SLOT test_slot;],
--
2.52.0
v4-0002-Use-pg_paser_lsn-in-libpqwalreceiver.patch (application/octet-stream, 1.4K) download
From 37de130aeab081dfd84d8ec2b8c1d1372d472b2f Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 11 Sep 2026 11:31:51 +0900
Subject: [PATCH v4 2/4] Use pg_paser_lsn in libpqwalreceiver
---
.../replication/libpqwalreceiver/libpqwalreceiver.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
index 029990d9fce..df47ae535ce 100644
--- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
+++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
@@ -23,6 +23,7 @@
#include <sys/time.h>
#include "common/connect.h"
+#include "common/pg_parse_lsn.h"
#include "funcapi.h"
#include "libpq-fe.h"
#include "libpq/libpq-be-fe-helpers.h"
@@ -458,15 +459,11 @@ libpqrcv_identify_system(WalReceiverConn *conn, TimeLineID *primary_tli,
/* Column 2 is the server's current WAL flush position */
if (server_lsn)
{
- uint32 hi,
- lo;
-
- if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
+ if (!pg_parse_lsn(PQgetvalue(res, 0, 2), server_lsn))
ereport(ERROR,
(errcode(ERRCODE_PROTOCOL_VIOLATION),
errmsg("could not parse WAL location \"%s\"",
PQgetvalue(res, 0, 2))));
- *server_lsn = ((uint64) hi) << 32 | lo;
}
PQclear(res);
--
2.52.0
v4-0003-Use-pg_parse_lsn-for-timeline-history-switchpoint.patch (application/octet-stream, 3.1K) download
From c628c3f54615f992b35886378f79f4d6371a9232 Mon Sep 17 00:00:00 2001
From: Jan Nidzwetzki <jan@planetscale.com>
Date: Fri, 16 Oct 2026 14:24:05 +0200
Subject: [PATCH v4 3/4] Use pg_parse_lsn() for timeline history switchpoints
readTimeLineHistory() parsed the switchpoint on each history file line
with sscanf("%X/%08X"). Commit f31d6fbc31d converted the frontend copy
of this code in pg_rewind, but not the backend one. Convert it in the
same way. As in pg_rewind, characters directly after the switchpoint are
now rejected rather than ignored.
Add a TAP test covering overlong components and trailing characters.
---
src/backend/access/transam/timeline.c | 37 +++++++++++++++++++++------
1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/src/backend/access/transam/timeline.c b/src/backend/access/transam/timeline.c
index d80c8ffe0a7..cc27587a07c 100644
--- a/src/backend/access/transam/timeline.c
+++ b/src/backend/access/transam/timeline.c
@@ -39,6 +39,7 @@
#include "access/xlog_internal.h"
#include "access/xlogarchive.h"
#include "access/xlogdefs.h"
+#include "common/pg_parse_lsn.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "utils/wait_event.h"
@@ -128,10 +129,13 @@ readTimeLineHistory(TimeLineID targetTLI)
char fline[MAXPGPATH];
char *res;
char *ptr;
+ char *token_end;
+ char save;
TimeLineID tli;
- uint32 switchpoint_hi;
- uint32 switchpoint_lo;
- int nfields;
+ XLogRecPtr switchpoint;
+ bool valid_switchpoint = false;
+ int nchars;
+ size_t nspaces;
pgstat_report_wait_start(WAIT_EVENT_TIMELINE_HISTORY_READ);
res = fgets(fline, sizeof(fline), fd);
@@ -155,16 +159,33 @@ readTimeLineHistory(TimeLineID targetTLI)
if (*ptr == '\0' || *ptr == '#')
continue;
- nfields = sscanf(fline, "%u\t%X/%08X", &tli, &switchpoint_hi, &switchpoint_lo);
-
- if (nfields < 1)
+ if (sscanf(fline, "%u%n", &tli, &nchars) != 1)
{
/* expect a numeric timeline ID as first field of line */
ereport(FATAL,
(errmsg("syntax error in history file: %s", fline),
errhint("Expected a numeric timeline ID.")));
}
- if (nfields != 3)
+
+ /* the switchpoint location follows, separated by whitespace */
+ ptr = fline + nchars;
+ nspaces = strspn(ptr, " \t\n\r\f\v");
+ if (nspaces > 0)
+ {
+ ptr += nspaces;
+
+ /*
+ * isolate the location from the rest of the line before parsing
+ * it
+ */
+ token_end = ptr + strcspn(ptr, " \t\n\r\f\v");
+ save = *token_end;
+ *token_end = '\0';
+ valid_switchpoint = pg_parse_lsn(ptr, &switchpoint);
+ *token_end = save;
+ }
+
+ if (!valid_switchpoint)
ereport(FATAL,
(errmsg("syntax error in history file: %s", fline),
errhint("Expected a write-ahead log switchpoint location.")));
@@ -179,7 +200,7 @@ readTimeLineHistory(TimeLineID targetTLI)
entry = palloc_object(TimeLineHistoryEntry);
entry->tli = tli;
entry->begin = prevend;
- entry->end = ((uint64) (switchpoint_hi)) << 32 | (uint64) switchpoint_lo;
+ entry->end = switchpoint;
prevend = entry->end;
/* Build list with newest item first */
--
2.52.0
v4-0004-Use-pg_parse_lsn-for-reading-backup-label.patch (application/octet-stream, 3.9K) download
From 49d65d0b063f1bdca9276caa511c177fef5953fd Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Tue, 20 Oct 2026 20:01:11 +0900
Subject: [PATCH v4 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.
---
src/backend/access/transam/xlogrecovery.c | 23 ++++++++++++++---------
src/backend/utils/adt/pg_lsn.c | 2 --
src/include/common/pg_parse_lsn.h | 2 ++
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 20be788da78..7c9c6acf1d2 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,7 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
char backupfrom[20];
char backuplabel[MAXPGPATH];
char backuptime[128];
- uint32 hi,
- lo;
+ char lsn[MAXPG_LSNLEN + 1];
/* suppress possible uninitialized-variable warnings */
*checkPointLoc = InvalidXLogRecPtr;
@@ -1218,19 +1218,23 @@ 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: %" CppAsString2(MAXPG_LSNLEN) "s (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: %" CppAsString2(MAXPG_LSNLEN) "s%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 +1301,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: %" CppAsString2(MAXPG_LSNLEN) "s\n",
+ lsn) > 0)
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("this is an incremental backup, not a data directory"),
diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c
index 7886e9b83e8..3ec2c2b650e 100644
--- a/src/backend/utils/adt/pg_lsn.c
+++ b/src/backend/utils/adt/pg_lsn.c
@@ -19,8 +19,6 @@
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
-#define MAXPG_LSNLEN 17
-
/*----------------------------------------------------------
* Formatting and conversion routines.
*---------------------------------------------------------*/
diff --git a/src/include/common/pg_parse_lsn.h b/src/include/common/pg_parse_lsn.h
index 0a80785a4e9..85cda67bce9 100644
--- a/src/include/common/pg_parse_lsn.h
+++ b/src/include/common/pg_parse_lsn.h
@@ -15,6 +15,8 @@
#include "access/xlogdefs.h"
+#define MAXPG_LSNLEN 17
+
extern bool pg_parse_lsn(const char *str, XLogRecPtr *result);
#endif /* PG_PARSE_LSN_H */
--
2.52.0
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-22 07:06 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-22 08:37 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 2 replies; 20+ messages in thread
From: Fujii Masao @ 2026-10-22 07:06 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Wed, Oct 21, 2026 at 6:57 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
> Thanks for the feedback. PSA the fixed version. MAXPG_LSNLEN was also
> exported in the header.
Thanks for updating the patch!
+ if (fscanf(lfp,
+ "CHECKPOINT LOCATION: %" CppAsString2(MAXPG_LSNLEN) "s%c", lsn,
+ &ch) != 2 ||
+ ch != '\n' ||
+ !pg_parse_lsn(lsn, checkPointLoc))
This seems to accept malformed LSN fields such as:
CHECKPOINT LOCATION: 0/123<NUL>junk
Here, <NUL> represents an actual NUL byte. fscanf("%17s") reads past it,
but pg_parse_lsn() sees only 0/123. The old code rejected this because %c
read the NUL instead of a newline. START WAL LOCATION seems to
have the same issue.
BTW, I'm thinking of committing 0001 and 0002 first, as I've reviewed them.
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
@ 2026-10-22 08:37 ` Fujii Masao <masao.fujii@gmail.com>
1 sibling, 0 replies; 20+ messages in thread
From: Fujii Masao @ 2026-10-22 08:37 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Thu, Oct 22, 2026 at 4:06 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> BTW, I'm thinking of committing 0001 and 0002 first, as I've reviewed them.
Firstly I've pushed 0001, 0002, and 0003 patches. Thanks!
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
@ 2026-10-23 03:24 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
1 sibling, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-23 03:24 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Dear Fujii-san,
Thanks for pushing the first half!
> + if (fscanf(lfp,
> + "CHECKPOINT LOCATION: %" CppAsString2(MAXPG_LSNLEN) "s%c", lsn,
> + &ch) != 2 ||
> + ch != '\n' ||
> + !pg_parse_lsn(lsn, checkPointLoc))
>
> This seems to accept malformed LSN fields such as:
>
> CHECKPOINT LOCATION: 0/123<NUL>junk
>
> Here, <NUL> represents an actual NUL byte. fscanf("%17s") reads past it,
> but pg_parse_lsn() sees only 0/123. The old code rejected this because %c
> read the NUL instead of a newline. START WAL LOCATION seems to
> have the same issue.
Right, it should be rejected. One my primitive fix idea is to directly specify the
acceptable strings, like [0-9A-Fa-f/]. PSA the patch.
I also attached the test code to check invalid strings can be rejected, see 0002.
I'm not intended to be pushed.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (2 files, .tar.gz)
v5-0001-Use-pg_parse_lsn-for-reading-backup-label.patch (application/octet-stream, 3.9K) download
From d2706357b711f53fa29bb3e6cc9e529d7e9b4bfa Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Tue, 20 Oct 2026 20:01:11 +0900
Subject: [PATCH v5 1/2] 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.
---
src/backend/access/transam/xlogrecovery.c | 23 ++++++++++++++---------
src/backend/utils/adt/pg_lsn.c | 2 --
src/include/common/pg_parse_lsn.h | 2 ++
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 20be788da78..f4e0cdc8dda 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,7 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
char backupfrom[20];
char backuplabel[MAXPGPATH];
char backuptime[128];
- uint32 hi,
- lo;
+ char lsn[MAXPG_LSNLEN + 1];
/* suppress possible uninitialized-variable warnings */
*checkPointLoc = InvalidXLogRecPtr;
@@ -1218,19 +1218,23 @@ 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: %" CppAsString2(MAXPG_LSNLEN) "[0-9A-Fa-f/] (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: %" CppAsString2(MAXPG_LSNLEN) "[0-9A-Fa-f/]%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 +1301,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: %" CppAsString2(MAXPG_LSNLEN) "[0-9A-Fa-f/]\n",
+ lsn) > 0)
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("this is an incremental backup, not a data directory"),
diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c
index 7886e9b83e8..3ec2c2b650e 100644
--- a/src/backend/utils/adt/pg_lsn.c
+++ b/src/backend/utils/adt/pg_lsn.c
@@ -19,8 +19,6 @@
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
-#define MAXPG_LSNLEN 17
-
/*----------------------------------------------------------
* Formatting and conversion routines.
*---------------------------------------------------------*/
diff --git a/src/include/common/pg_parse_lsn.h b/src/include/common/pg_parse_lsn.h
index 0a80785a4e9..85cda67bce9 100644
--- a/src/include/common/pg_parse_lsn.h
+++ b/src/include/common/pg_parse_lsn.h
@@ -15,6 +15,8 @@
#include "access/xlogdefs.h"
+#define MAXPG_LSNLEN 17
+
extern bool pg_parse_lsn(const char *str, XLogRecPtr *result);
#endif /* PG_PARSE_LSN_H */
--
2.52.0
v5-0002-Add-fuzzy-test.patch (application/octet-stream, 4.0K) download
From 0300e01870c6dd6663c0a02f839f7ad7ccd85563 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 23 Oct 2026 12:13:38 +0900
Subject: [PATCH v5 2/2] Add fuzzy test
---
src/test/recovery/meson.build | 1 +
src/test/recovery/t/061_backup_label.pl | 126 ++++++++++++++++++++++++
2 files changed, 127 insertions(+)
create mode 100644 src/test/recovery/t/061_backup_label.pl
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index b6ac9dd07a7..c022b7d87dd 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -69,6 +69,7 @@ tests += {
't/058_shutdown_crash_restart.pl',
't/059_remote_apply_status_interval.pl',
't/060_multixact_crashed_updater.pl',
+ 't/061_backup_label.pl',
],
},
}
diff --git a/src/test/recovery/t/061_backup_label.pl b/src/test/recovery/t/061_backup_label.pl
new file mode 100644
index 00000000000..90c62ca0da3
--- /dev/null
+++ b/src/test/recovery/t/061_backup_label.pl
@@ -0,0 +1,126 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test parsing of LSN fields in backup_label.
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my @tests = (
+ {
+ name => 'nul_in_start_lsn',
+ start_lsn => "0/123\0junk",
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'nul_after_start_lsn',
+ start_lsn => "0/123\0",
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'nul_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => "0/123\0junk",
+ },
+ {
+ name => 'nul_after_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => "0/123\0",
+ },
+ {
+ name => 'non_hexadecimal_start_lsn',
+ start_lsn => '0/12G',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'non_hexadecimal_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/12G',
+ },
+ {
+ name => 'non_hexadecimal_start_lsn_prefix',
+ start_lsn => 'G/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'non_hexadecimal_checkpoint_lsn_prefix',
+ start_lsn => '0/1',
+ checkpoint_lsn => 'G/1',
+ },
+ {
+ name => 'extra_slash_in_start_lsn',
+ start_lsn => '0/1/2',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'missing_slash_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '01',
+ },
+ {
+ name => 'missing_high_component_in_start_lsn',
+ start_lsn => '/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'missing_low_component_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/',
+ },
+ {
+ name => 'whitespace_in_start_lsn',
+ start_lsn => '0 /1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'overlong_start_lsn',
+ start_lsn => '123456789/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'overlong_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '123456789/1',
+ },
+ {
+ name => 'incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/1',
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'nul_in_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => "0/123\0junk",
+ error => 'this is an incremental backup, not a data directory',
+ },
+);
+
+for my $test (@tests)
+{
+ my $node = PostgreSQL::Test::Cluster->new($test->{name});
+ $node->init;
+
+ open my $fh, '>', $node->data_dir . '/backup_label'
+ or BAIL_OUT("could not create backup_label: $!");
+ binmode $fh;
+ print $fh
+ "START WAL LOCATION: $test->{start_lsn} "
+ . "(file 000000010000000000000000)\n";
+ print $fh "CHECKPOINT LOCATION: $test->{checkpoint_lsn}\n";
+ print $fh "INCREMENTAL FROM LSN: $test->{incremental_lsn}\n"
+ if defined $test->{incremental_lsn};
+ close $fh or BAIL_OUT("could not close backup_label: $!");
+
+ is($node->start(fail_ok => 1), 0, "$test->{name} prevents startup");
+ my $error = $test->{error} // 'invalid data in file "backup_label"';
+ ok($node->log_contains(qr/FATAL: .*\Q$error\E/),
+ "$test->{name} is rejected");
+}
+
+done_testing();
--
2.52.0
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-23 13:45 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Fujii Masao @ 2026-10-23 13:45 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Fri, Oct 23, 2026 at 12:24 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
> Right, it should be rejected. One my primitive fix idea is to directly specify the
> acceptable strings, like [0-9A-Fa-f/]. PSA the patch.
Thanks for updating the patch!
+ if (fscanf(lfp,
+ "START WAL LOCATION: %" CppAsString2(MAXPG_LSNLEN) "[0-9A-Fa-f/]
(file %08X%16s)%c",
For portability, isn't it better to use [0123456789ABCDEFabcdef/] instead of
[0-9A-Fa-f/]? pg_parse_lsn() also lists the hexadecimal digits explicitly.
- if (fscanf(lfp, "INCREMENTAL FROM LSN: %X/%08X\n", &hi, &lo) > 0)
+ if (fscanf(lfp, "INCREMENTAL FROM LSN: %" CppAsString2(MAXPG_LSNLEN)
"[0-9A-Fa-f/]\n",
+ lsn) > 0)
This change seems to allow a backup label containing "INCREMENTAL FROM
LSN: +0/1"
to bypass the check. Previously, %X accepted the leading sign, causing fscanf()
to return a positive value and the backup to be rejected. With this scanset,
fscanf() returns zero instead.
Since we only need to detect an incremental backup here and don't use
the LSN value, how about using "%s\n" instead of "[0-9A-Fa-f/]\n"? This would
preserve the rejection of the above input while also rejecting malformed
values such as "INCREMENTAL FROM LSN: junk".
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
@ 2026-10-26 07:26 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-26 07:26 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Fujii-san,
Thanks for reviewing!
>
> + if (fscanf(lfp,
> + "START WAL LOCATION: %" CppAsString2(MAXPG_LSNLEN)
> "[0-9A-Fa-f/]
> (file %08X%16s)%c",
>
> For portability, isn't it better to use [0123456789ABCDEFabcdef/] instead of
> [0-9A-Fa-f/]? pg_parse_lsn() also lists the hexadecimal digits explicitly.
Done. I checked the C standard and the behavior when - is in the bracket seemed
to be the implementation-defined.
> Since we only need to detect an incremental backup here and don't use
> the LSN value, how about using "%s\n" instead of "[0-9A-Fa-f/]\n"? This would
> preserve the rejection of the above input while also rejecting malformed
> values such as "INCREMENTAL FROM LSN: junk".
Indeed, but %s cannot be used as-is because we need to pass the fixed-length char
array. If we do not have to take care the format of string, isn't it enough to
only check the prefix? I implemented like that in v6.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (2 files, .tar.gz)
v6-0001-Use-pg_parse_lsn-for-reading-backup-label.patch (application/octet-stream, 4.0K) download
From 2427cfcc170e5f0c949cb39590c6fb985e55bf4e Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 26 Oct 2026 16:19:25 +0900
Subject: [PATCH v6 1/2] 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.
---
src/backend/access/transam/xlogrecovery.c | 27 +++++++++++++++--------
src/backend/utils/adt/pg_lsn.c | 2 --
src/include/common/pg_parse_lsn.h | 2 ++
3 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 20be788da78..7a1de0567a8 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,7 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
char backupfrom[20];
char backuplabel[MAXPGPATH];
char backuptime[128];
- uint32 hi,
- lo;
+ char lsn[MAXPG_LSNLEN + 1];
/* suppress possible uninitialized-variable warnings */
*checkPointLoc = InvalidXLogRecPtr;
@@ -1218,19 +1218,23 @@ 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: %" CppAsString2(MAXPG_LSNLEN) "[0123456789ABCDEFabcdef/] (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: %" CppAsString2(MAXPG_LSNLEN) "[0123456789ABCDEFabcdef/]%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 +1301,12 @@ 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)
+ /*
+ * INCREMENTAL FROM LSN indicates that this is an incremental backup. The
+ * LSN itself need not be parsed, so reading its first character is
+ * sufficient.
+ */
+ if (fscanf(lfp, "INCREMENTAL FROM LSN: %c", &ch) == 1)
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("this is an incremental backup, not a data directory"),
diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c
index 7886e9b83e8..3ec2c2b650e 100644
--- a/src/backend/utils/adt/pg_lsn.c
+++ b/src/backend/utils/adt/pg_lsn.c
@@ -19,8 +19,6 @@
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
-#define MAXPG_LSNLEN 17
-
/*----------------------------------------------------------
* Formatting and conversion routines.
*---------------------------------------------------------*/
diff --git a/src/include/common/pg_parse_lsn.h b/src/include/common/pg_parse_lsn.h
index 0a80785a4e9..85cda67bce9 100644
--- a/src/include/common/pg_parse_lsn.h
+++ b/src/include/common/pg_parse_lsn.h
@@ -15,6 +15,8 @@
#include "access/xlogdefs.h"
+#define MAXPG_LSNLEN 17
+
extern bool pg_parse_lsn(const char *str, XLogRecPtr *result);
#endif /* PG_PARSE_LSN_H */
--
2.52.0
v6-0002-Add-fuzzy-test.patch (application/octet-stream, 4.4K) download
From 8424d833b1fad3e097c1b1eda1ddacc0a78c89fb Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 23 Oct 2026 12:13:38 +0900
Subject: [PATCH v6 2/2] Add fuzzy test
---
src/test/recovery/meson.build | 1 +
src/test/recovery/t/061_backup_label.pl | 140 ++++++++++++++++++++++++
2 files changed, 141 insertions(+)
create mode 100644 src/test/recovery/t/061_backup_label.pl
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index b6ac9dd07a7..c022b7d87dd 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -69,6 +69,7 @@ tests += {
't/058_shutdown_crash_restart.pl',
't/059_remote_apply_status_interval.pl',
't/060_multixact_crashed_updater.pl',
+ 't/061_backup_label.pl',
],
},
}
diff --git a/src/test/recovery/t/061_backup_label.pl b/src/test/recovery/t/061_backup_label.pl
new file mode 100644
index 00000000000..d88f7a45e9b
--- /dev/null
+++ b/src/test/recovery/t/061_backup_label.pl
@@ -0,0 +1,140 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test parsing of LSN fields in backup_label.
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my @tests = (
+ {
+ name => 'nul_in_start_lsn',
+ start_lsn => "0/123\0junk",
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'nul_after_start_lsn',
+ start_lsn => "0/123\0",
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'nul_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => "0/123\0junk",
+ },
+ {
+ name => 'nul_after_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => "0/123\0",
+ },
+ {
+ name => 'non_hexadecimal_start_lsn',
+ start_lsn => '0/12G',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'non_hexadecimal_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/12G',
+ },
+ {
+ name => 'non_hexadecimal_start_lsn_prefix',
+ start_lsn => 'G/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'non_hexadecimal_checkpoint_lsn_prefix',
+ start_lsn => '0/1',
+ checkpoint_lsn => 'G/1',
+ },
+ {
+ name => 'extra_slash_in_start_lsn',
+ start_lsn => '0/1/2',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'missing_slash_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '01',
+ },
+ {
+ name => 'missing_high_component_in_start_lsn',
+ start_lsn => '/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'missing_low_component_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/',
+ },
+ {
+ name => 'whitespace_in_start_lsn',
+ start_lsn => '0 /1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'overlong_start_lsn',
+ start_lsn => '123456789/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'overlong_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '123456789/1',
+ },
+ {
+ name => 'incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/1',
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'invalid_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/0/1',
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'nul_in_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => "0/123\0junk",
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'too_long_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/1111111111111111111111111111111111111',
+ error => 'this is an incremental backup, not a data directory',
+ },
+);
+
+for my $test (@tests)
+{
+ my $node = PostgreSQL::Test::Cluster->new($test->{name});
+ $node->init;
+
+ open my $fh, '>', $node->data_dir . '/backup_label'
+ or BAIL_OUT("could not create backup_label: $!");
+ binmode $fh;
+ print $fh
+ "START WAL LOCATION: $test->{start_lsn} "
+ . "(file 000000010000000000000000)\n";
+ print $fh "CHECKPOINT LOCATION: $test->{checkpoint_lsn}\n";
+ print $fh "INCREMENTAL FROM LSN: $test->{incremental_lsn}\n"
+ if defined $test->{incremental_lsn};
+ close $fh or BAIL_OUT("could not close backup_label: $!");
+
+ is($node->start(fail_ok => 1), 0, "$test->{name} prevents startup");
+ my $error = $test->{error} // 'invalid data in file "backup_label"';
+ ok($node->log_contains(qr/FATAL: .*\Q$error\E/),
+ "$test->{name} is rejected");
+}
+
+done_testing();
--
2.52.0
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-26 15:26 ` Jan Nidzwetzki <jan@planetscale.com>
2026-10-28 03:18 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Jan Nidzwetzki @ 2026-10-26 15:26 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; Fujii Masao <masao.fujii@gmail.com>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Kuroda-san, Fujii-san,
Thanks for updating the patch set. I tested the two remaining ones on my
system. They apply cleanly and check-world passes.
Patch 0001
==========
I have just a small comment on the moved MAXPG_LSNLEN. It's currently a
hard-coded 17. It could make sense to export MAXPG_LSNCOMPONENT as well
and define MAXPG_LSNLEN as (2 * MAXPG_LSNCOMPONENT + 1), which would
document where the value comes from.
Patch 0002
==========
Kuroda-san wrote in the v5 mail:
> I also attached the test code to check invalid strings can be
rejected, see 0002.
> I'm not intended to be pushed.
I think this test is worth committing. As far as I can see, nothing else
covers the "this is an incremental backup, not a data directory" error,
and the new LSN checks would otherwise remain untested. However, we test
only an overlong high component. It could make sense to add this test
for the low component as well (e.g., using 0/123456789).
Best regards
Jan
--
Jan Nidzwetzki
PlanetScale Postgres Core Team
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
@ 2026-10-28 03:18 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 10:50 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-28 03:18 UTC (permalink / raw)
To: Jan Nidzwetzki <jan@planetscale.com>; Fujii Masao <masao.fujii@gmail.com>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Jan,
> Patch 0001
> ==========
>
> I have just a small comment on the moved MAXPG_LSNLEN. It's currently a
> hard-coded 17. It could make sense to export MAXPG_LSNCOMPONENT as well
> and define MAXPG_LSNLEN as (2 * MAXPG_LSNCOMPONENT + 1), which
> would
> document where the value comes from.
I feel the notation cannot be used. CppAsString2() converts the argument to the
string, but it does not evaluate arithmetic at that time. So your suggestion
would be "%(2 * 8 + 1)s" and it won't work well. You can find that the compiler
raises warnings.
I feel we can add a static assert to ensure they remain consistent. I put in
pg_parse_lsn.c and it will be checked at compilation time. See new 0002 patch.
I separated them because it may be the independent improvement.
> Patch 0002
> ==========
>
> Kuroda-san wrote in the v5 mail:
>
> > I also attached the test code to check invalid strings can be
> rejected, see 0002.
> > I'm not intended to be pushed.
>
> I think this test is worth committing. As far as I can see, nothing else
> covers the "this is an incremental backup, not a data directory" error,
> and the new LSN checks would otherwise remain untested. However, we test
> only an overlong high component. It could make sense to add this test
> for the low component as well (e.g., using 0/123456789).
Hmm. But 5ffb8c74 also did not have any tests and the error "syntax error in history file:"
is nor verified, right? I still feel we can follow the manner.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (3 files, .tar.gz)
v7-0001-Use-pg_parse_lsn-for-reading-backup-label.patch (application/octet-stream, 4.5K) download
From 61c6ee7eb652ebf3b5c09ae603b3322d4b90370a Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 26 Oct 2026 16:19:25 +0900
Subject: [PATCH v7 1/3] Use pg_parse_lsn() for reading backup label
read_backup_label() previously parsed the START WAL LOCATION and
CHECKPOINT LOCATION fields using fscanf("%X/%08X") into uint32 values.
An overlong high component could overflow, while an overlong low
component was truncated to its first eight digits. Other malformed
forms accepted by scanf(), such as signs and "0x" prefixes, could also
be interpreted differently from pg_lsn input.
Use pg_parse_lsn() instead, requiring one to eight hexadecimal digits
per component and no trailing characters, as for pg_lsn input. This
follows similar conversions in commits 4a842a1d657, ba6d9ee259c, and
5ffb8c74093.
---
src/backend/access/transam/xlogrecovery.c | 27 +++++++++++++++--------
src/backend/utils/adt/pg_lsn.c | 2 --
src/include/common/pg_parse_lsn.h | 2 ++
3 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 20be788da78..7a1de0567a8 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,7 @@ read_backup_label(XLogRecPtr *checkPointLoc, TimeLineID *backupLabelTLI,
char backupfrom[20];
char backuplabel[MAXPGPATH];
char backuptime[128];
- uint32 hi,
- lo;
+ char lsn[MAXPG_LSNLEN + 1];
/* suppress possible uninitialized-variable warnings */
*checkPointLoc = InvalidXLogRecPtr;
@@ -1218,19 +1218,23 @@ 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: %" CppAsString2(MAXPG_LSNLEN) "[0123456789ABCDEFabcdef/] (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: %" CppAsString2(MAXPG_LSNLEN) "[0123456789ABCDEFabcdef/]%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 +1301,12 @@ 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)
+ /*
+ * INCREMENTAL FROM LSN indicates that this is an incremental backup. The
+ * LSN itself need not be parsed, so reading its first character is
+ * sufficient.
+ */
+ if (fscanf(lfp, "INCREMENTAL FROM LSN: %c", &ch) == 1)
ereport(FATAL,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("this is an incremental backup, not a data directory"),
diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c
index 7886e9b83e8..3ec2c2b650e 100644
--- a/src/backend/utils/adt/pg_lsn.c
+++ b/src/backend/utils/adt/pg_lsn.c
@@ -19,8 +19,6 @@
#include "utils/numeric.h"
#include "utils/pg_lsn.h"
-#define MAXPG_LSNLEN 17
-
/*----------------------------------------------------------
* Formatting and conversion routines.
*---------------------------------------------------------*/
diff --git a/src/include/common/pg_parse_lsn.h b/src/include/common/pg_parse_lsn.h
index 0a80785a4e9..94d776f89b6 100644
--- a/src/include/common/pg_parse_lsn.h
+++ b/src/include/common/pg_parse_lsn.h
@@ -15,6 +15,8 @@
#include "access/xlogdefs.h"
+#define MAXPG_LSNLEN 17
+
extern bool pg_parse_lsn(const char *str, XLogRecPtr *result);
#endif /* PG_PARSE_LSN_H */
--
2.52.0
v7-0002-Add-static_assert-for-MAXPG_LSNLEN-and-MAXPG_LSNC.patch (application/octet-stream, 692B) download
From f75754cec54ed0d767676b37908f1e79fe72bf13 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Wed, 28 Oct 2026 12:08:08 +0900
Subject: [PATCH v7 2/3] Add static_assert() for MAXPG_LSNLEN and
MAXPG_LSNCOMPONENT
---
src/common/pg_parse_lsn.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/common/pg_parse_lsn.c b/src/common/pg_parse_lsn.c
index b6a83b57216..9223f46bd44 100644
--- a/src/common/pg_parse_lsn.c
+++ b/src/common/pg_parse_lsn.c
@@ -22,6 +22,9 @@
#define MAXPG_LSNCOMPONENT 8
+static_assert(MAXPG_LSNLEN == MAXPG_LSNCOMPONENT * 2 + 1,
+ "MAXPG_LSNLEN does not match MAXPG_LSNCOMPONENT");
+
/*
* pg_parse_lsn
*
--
2.52.0
v7-0003-Add-fuzzy-test.patch (application/octet-stream, 4.4K) download
From aee815c8cf4f520e59ded8b6044df4dcec6ccb47 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 23 Oct 2026 12:13:38 +0900
Subject: [PATCH v7 3/3] Add fuzzy test
---
src/test/recovery/meson.build | 1 +
src/test/recovery/t/061_backup_label.pl | 140 ++++++++++++++++++++++++
2 files changed, 141 insertions(+)
create mode 100644 src/test/recovery/t/061_backup_label.pl
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index b6ac9dd07a7..c022b7d87dd 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -69,6 +69,7 @@ tests += {
't/058_shutdown_crash_restart.pl',
't/059_remote_apply_status_interval.pl',
't/060_multixact_crashed_updater.pl',
+ 't/061_backup_label.pl',
],
},
}
diff --git a/src/test/recovery/t/061_backup_label.pl b/src/test/recovery/t/061_backup_label.pl
new file mode 100644
index 00000000000..d88f7a45e9b
--- /dev/null
+++ b/src/test/recovery/t/061_backup_label.pl
@@ -0,0 +1,140 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test parsing of LSN fields in backup_label.
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my @tests = (
+ {
+ name => 'nul_in_start_lsn',
+ start_lsn => "0/123\0junk",
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'nul_after_start_lsn',
+ start_lsn => "0/123\0",
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'nul_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => "0/123\0junk",
+ },
+ {
+ name => 'nul_after_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => "0/123\0",
+ },
+ {
+ name => 'non_hexadecimal_start_lsn',
+ start_lsn => '0/12G',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'non_hexadecimal_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/12G',
+ },
+ {
+ name => 'non_hexadecimal_start_lsn_prefix',
+ start_lsn => 'G/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'non_hexadecimal_checkpoint_lsn_prefix',
+ start_lsn => '0/1',
+ checkpoint_lsn => 'G/1',
+ },
+ {
+ name => 'extra_slash_in_start_lsn',
+ start_lsn => '0/1/2',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'missing_slash_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '01',
+ },
+ {
+ name => 'missing_high_component_in_start_lsn',
+ start_lsn => '/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'missing_low_component_in_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/',
+ },
+ {
+ name => 'whitespace_in_start_lsn',
+ start_lsn => '0 /1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'overlong_start_lsn',
+ start_lsn => '123456789/1',
+ checkpoint_lsn => '0/1',
+ },
+ {
+ name => 'overlong_checkpoint_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '123456789/1',
+ },
+ {
+ name => 'incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/1',
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'invalid_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/0/1',
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'nul_in_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => "0/123\0junk",
+ error => 'this is an incremental backup, not a data directory',
+ },
+ {
+ name => 'too_long_incremental_lsn',
+ start_lsn => '0/1',
+ checkpoint_lsn => '0/1',
+ incremental_lsn => '0/1111111111111111111111111111111111111',
+ error => 'this is an incremental backup, not a data directory',
+ },
+);
+
+for my $test (@tests)
+{
+ my $node = PostgreSQL::Test::Cluster->new($test->{name});
+ $node->init;
+
+ open my $fh, '>', $node->data_dir . '/backup_label'
+ or BAIL_OUT("could not create backup_label: $!");
+ binmode $fh;
+ print $fh
+ "START WAL LOCATION: $test->{start_lsn} "
+ . "(file 000000010000000000000000)\n";
+ print $fh "CHECKPOINT LOCATION: $test->{checkpoint_lsn}\n";
+ print $fh "INCREMENTAL FROM LSN: $test->{incremental_lsn}\n"
+ if defined $test->{incremental_lsn};
+ close $fh or BAIL_OUT("could not close backup_label: $!");
+
+ is($node->start(fail_ok => 1), 0, "$test->{name} prevents startup");
+ my $error = $test->{error} // 'invalid data in file "backup_label"';
+ ok($node->log_contains(qr/FATAL: .*\Q$error\E/),
+ "$test->{name} is rejected");
+}
+
+done_testing();
--
2.52.0
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-28 03:18 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-28 10:50 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-28 11:33 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
0 siblings, 1 reply; 20+ messages in thread
From: Fujii Masao @ 2026-10-28 10:50 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Wed, Oct 28, 2026 at 12:18 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
> Hmm. But 5ffb8c74 also did not have any tests and the error "syntax error in history file:"
> is nor verified, right? I still feel we can follow the manner.
I'm not convinced that 0002 and 0003 are necessary. OTOH, there seems to be
general agreement on 0001, so barring any objections, I'll commit that patch.
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-28 03:18 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 10:50 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
@ 2026-10-28 11:33 ` Jan Nidzwetzki <jan@planetscale.com>
2026-10-29 02:12 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 20+ messages in thread
From: Jan Nidzwetzki @ 2026-10-28 11:33 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Kuroda-san, Fujii-san,
On 28.10.26 11:50, Fujii Masao wrote:
> On Wed, Oct 28, 2026 at 12:18 PM Hayato Kuroda (Fujitsu)
> <kuroda.hayato@fujitsu.com> wrote:
>> Hmm. But 5ffb8c74 also did not have any tests and the error "syntax error in history file:"
>> is nor verified, right? I still feel we can follow the manner.
>
> I'm not convinced that 0002 and 0003 are necessary. OTOH, there seems to be
> general agreement on 0001, so barring any objections, I'll commit that patch.
No objections from my side.
Kuroda-san, you are right about CppAsString2(). I missed that the value
is stringified for scanf. Thanks for checking. So, I am fine with 0001.
The static assert and the test can be left out.
Best regards
Jan
--
Jan Nidzwetzki
PlanetScale Postgres Core Team
^ permalink raw reply [nested|flat] 20+ messages in thread
* RE: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-28 03:18 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 10:50 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-28 11:33 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
@ 2026-10-29 02:12 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-29 11:47 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
0 siblings, 1 reply; 20+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-29 02:12 UTC (permalink / raw)
To: Jan Nidzwetzki <jan@planetscale.com>; Fujii Masao <masao.fujii@gmail.com>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Jan. Fujii-san,
Thanks for the review. I think 0002 is not a mandatory, can retain for now.
Someone can propose later in another thread.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
^ permalink raw reply [nested|flat] 20+ messages in thread
* Re: START_REPLICATION silently truncates an overlong LSN component
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Re: START_REPLICATION silently truncates an overlong LSN component Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-28 03:18 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 10:50 ` Re: START_REPLICATION silently truncates an overlong LSN component Fujii Masao <masao.fujii@gmail.com>
2026-10-28 11:33 ` Re: START_REPLICATION silently truncates an overlong LSN component Jan Nidzwetzki <jan@planetscale.com>
2026-10-29 02:12 ` RE: START_REPLICATION silently truncates an overlong LSN component Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-29 11:47 ` Fujii Masao <masao.fujii@gmail.com>
0 siblings, 0 replies; 20+ messages in thread
From: Fujii Masao @ 2026-10-29 11:47 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jan Nidzwetzki <jan@planetscale.com>; Ayush Tiwari <ayushtiwari.slg01@gmail.com>; ♂π≌26218 <1991230470@qq.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Thu, Oct 29, 2026 at 11:12 AM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Hi Jan. Fujii-san,
>
> Thanks for the review. I think 0002 is not a mandatory, can retain for now.
> Someone can propose later in another thread.
I've pushed the 0001 patch. Thanks!
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 20+ messages in thread
end of thread, other threads:[~2026-10-29 11:47 UTC | newest]
Thread overview: 20+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-08 09:58 START_REPLICATION silently truncates an overlong LSN component ♂π≌26218 <1991230470@qq.com>
2026-09-10 13:41 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-11 02:35 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-16 13:03 ` Jan Nidzwetzki <jan@planetscale.com>
2026-10-20 07:36 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-20 09:31 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-20 11:29 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-21 06:36 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-21 09:57 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-22 07:06 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-22 08:37 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-23 03:24 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-23 13:45 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-26 07:26 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 15:26 ` Jan Nidzwetzki <jan@planetscale.com>
2026-10-28 03:18 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 10:50 ` Fujii Masao <masao.fujii@gmail.com>
2026-10-28 11:33 ` Jan Nidzwetzki <jan@planetscale.com>
2026-10-29 02:12 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-29 11:47 ` Fujii Masao <masao.fujii@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox