Skip to content

Commit

Permalink
Trap very large systems risking index overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Stratford committed Jul 23, 2024
1 parent 83b6a90 commit 060889e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*****************************************************************************/

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -97,7 +98,12 @@ int lb_data_create(pe_t * pe, cs_t * cs, const lb_data_options_t * options,
int nz = nlocal[Z] + 2*nhalo;
obj->nsite = nx*ny*nz;
}
{
if (obj->nsite < 1 || INT_MAX/obj->nvel < obj->nsite) {
/* Suggests local system size has overflowed int32_t ... */
/* ... or will overflow indexing */
pe_exit(pe, "Local system size overflows INT_MAX in distributions\n");
}
else {
size_t sz = sizeof(double)*obj->nsite*obj->ndist*obj->nvel;
assert(sz > 0); /* Should not overflow in size_t I hope! */
obj->f = (double *) mem_aligned_malloc(MEM_PAGESIZE, sz);
Expand Down Expand Up @@ -1310,7 +1316,7 @@ __host__ int lb_io_aggr_pack(const lb_t * lb, io_aggregator_t * aggr) {

/* Write data (ic,jc,kc) */
int index = cs_index(lb->cs, ic, jc, kc);
int offset = ib*aggr->szelement;
size_t offset = ib*aggr->szelement;
if (iasc) lb_write_buf_ascii(lb, index, aggr->buf + offset);
if (ibin) lb_write_buf(lb, index, aggr->buf + offset);
}
Expand Down

0 comments on commit 060889e

Please sign in to comment.