printTableInit(printTableContent *const content, const printTableOpt *opt,
const char *title, const int ncolumns, const int nrows)
{
+ uint64 total_cells;
+
content->opt = opt;
content->title = title;
content->ncolumns = ncolumns;
content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
- content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
+ total_cells = (uint64) ncolumns * nrows;
+ /* Catch possible overflow. Using >= here allows adding 1 below */
+ if (total_cells >= SIZE_MAX / sizeof(*content->cells))
+ {
+ fprintf(stderr, _("Cannot print table contents: number of cells %lld is equal to or exceeds maximum %lld.\n"),
+ (long long int) total_cells,
+ (long long int) (SIZE_MAX / sizeof(*content->cells)));
+ exit(EXIT_FAILURE);
+ }
+ content->cells = pg_malloc0((total_cells + 1) * sizeof(*content->cells));
content->cellmustfree = NULL;
content->footers = NULL;
printTableAddCell(printTableContent *const content, char *cell,
const bool translate, const bool mustfree)
{
+ uint64 total_cells;
+
#ifndef ENABLE_NLS
(void) translate; /* unused parameter */
#endif
- if (content->cellsadded >= content->ncolumns * content->nrows)
+ total_cells = (uint64) content->ncolumns * 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);
+ fprintf(stderr, _("Cannot add cell to table content: total cell count of %lld exceeded.\n"),
+ (long long int) total_cells);
exit(EXIT_FAILURE);
}
{
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;
}
{
if (content->cellmustfree)
{
- int i;
+ uint64 total_cells;
- for (i = 0; i < content->nrows * content->ncolumns; i++)
+ total_cells = (uint64) content->ncolumns * content->nrows;
+ for (uint64 i = 0; i < total_cells; i++)
{
if (content->cellmustfree[i])
free(unconstify(char *, content->cells[i]));