v2-0001-Prevent-division-by-zero-errors-in-pg_controldata.patch

application/octet-stream

Filename: v2-0001-Prevent-division-by-zero-errors-in-pg_controldata.patch
Type: application/octet-stream
Part: 0
Message: Re: [HACKERS] pg_upgrade to clusters with a different WAL segment size

Patch

Format: format-patch
Series: patch v2-0001
Subject: Prevent division-by-zero errors in pg_controldata.
File+
src/bin/pg_controldata/pg_controldata.c 12 3
From c07e622b8b47d249608d34bd334db5532b051b6b Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Thu, 8 Mar 2018 16:48:47 +0000
Subject: [PATCH v2 1/4] Prevent division-by-zero errors in pg_controldata.

If the control file is corrupted and specifies the WAL segment size
to be 0 bytes, calculating the latest checkpoint's REDO WAL file
will fail with a division-by-zero error.  This change marks the WAL
file name as "unknown" in this case.
---
 src/bin/pg_controldata/pg_controldata.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index cc73b7d..709418e 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -193,10 +193,19 @@ main(int argc, char *argv[])
 	/*
 	 * Calculate name of the WAL file containing the latest checkpoint's REDO
 	 * start point.
+	 *
+	 * If the control file indicates that the WAL segment size is 0 bytes, we
+	 * cannot calculate the latest checkpoint's REDO WAL file, so mark it as
+	 * unknown.
 	 */
-	XLByteToSeg(ControlFile->checkPointCopy.redo, segno, WalSegSz);
-	XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
-				 segno, WalSegSz);
+	if (WalSegSz == 0)
+		strcpy(xlogfilename, "unknown");
+	else
+	{
+		XLByteToSeg(ControlFile->checkPointCopy.redo, segno, WalSegSz);
+		XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
+					 segno, WalSegSz);
+	}
 
 	/*
 	 * Format system_identifier and mock_authentication_nonce separately to
-- 
2.7.3.AMZN