From 1e3368895e72cc1ab59efecc6b5e93a7752608c7 Mon Sep 17 00:00:00 2001
From: interma <interma@outlook.com>
Date: Mon, 11 Sep 2023 14:42:14 +0800
Subject: [PATCH] Using long type in printTableAddCell() to prevent int
 overflow

---
 src/fe_utils/print.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c
index 7af1ccb6b5..4d6a9e1d4f 100644
--- a/src/fe_utils/print.c
+++ b/src/fe_utils/print.c
@@ -3249,15 +3249,17 @@ void
 printTableAddCell(printTableContent *const content, char *cell,
 				  const bool translate, const bool mustfree)
 {
+	long product;
 #ifndef ENABLE_NLS
 	(void) translate;			/* unused parameter */
 #endif
-
-	if (content->cellsadded >= content->ncolumns * content->nrows)
+	/* product of cols and rows, using long type to prevent int overflow */
+	product = (long)content->ncolumns * (long)content->nrows;
+	if (content->cellsadded >= product)
 	{
 		fprintf(stderr, _("Cannot add cell to table content: "
-						  "total cell count of %d exceeded.\n"),
-				content->ncolumns * content->nrows);
+						  "total cell count of %ld exceeded, cells added: %ld.\n"),
+				product, content->cellsadded);
 		exit(EXIT_FAILURE);
 	}
 
@@ -3273,7 +3275,7 @@ printTableAddCell(printTableContent *const content, char *cell,
 	{
 		if (content->cellmustfree == NULL)
 			content->cellmustfree =
-				pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
+				pg_malloc0((product + 1) * sizeof(bool));
 
 		content->cellmustfree[content->cellsadded] = true;
 	}
-- 
2.39.2 (Apple Git-143)

