From 4c6295aa31fbf325f9b88c942d82a26f2f4aa87e Mon Sep 17 00:00:00 2001 From: interma 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 | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index 7af1ccb6b5..99bbec9f72 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -3249,15 +3249,21 @@ void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree) { + long total_cells; #ifndef ENABLE_NLS (void) translate; /* unused parameter */ #endif - if (content->cellsadded >= content->ncolumns * content->nrows) + /* + * total_cells is the product of ncolumns and nrows + * Using long type here to prevent int overflow + */ + total_cells = (long)content->ncolumns * (long)content->nrows; + if (content->cellsadded >= total_cells) { 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"), + total_cells, content->cellsadded); exit(EXIT_FAILURE); } @@ -3273,7 +3279,7 @@ printTableAddCell(printTableContent *const content, char *cell, { if (content->cellmustfree == NULL) content->cellmustfree = - pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool)); + pg_malloc0((total_cells + 1) * sizeof(bool)); content->cellmustfree[content->cellsadded] = true; } -- 2.39.2 (Apple Git-143)