From bed9d016fbeee5df08105662b92f4e1396b4e8d8 Mon Sep 17 00:00:00 2001
From: s1ko <s1ko@riseup.net>
Date: Sun, 8 Nov 2026 18:35:22 +0100
Subject: [PATCH v1 2/2] pg_basebackup: fail on malformed TIMELINE_HISTORY
 response

ReceiveXlogStream() checked that the TIMELINE_HISTORY reply had one row
and two columns, but only logged a warning and then went on to call
writeTimeLineHistoryFile() with PQgetvalue() results that are NULL
whenever the check failed, crashing in strlen().

Make the check fatal: log an error, clear the result and return false,
as the surrounding error paths already do.  This has been reachable
since cc8d4151172 introduced the warning.
---
 src/bin/pg_basebackup/receivelog.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index 77a2b4458b..9b03fdfd24 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -552,8 +552,10 @@ ReceiveXlogStream(PGconn *conn, StreamCtl *stream)
 			 */
 			if (PQnfields(res) != 2 || PQntuples(res) != 1)
 			{
-				pg_log_warning("unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields",
-							   PQntuples(res), PQnfields(res), 1, 2);
+				pg_log_error("unexpected response to TIMELINE_HISTORY command: got %d rows and %d fields, expected %d rows and %d fields",
+							 PQntuples(res), PQnfields(res), 1, 2);
+				PQclear(res);
+				return false;
 			}
 
 			/* Write the history file to disk */
-- 
2.53.0

