Skip to content

Commit

Permalink
safety check for unsigned integer subtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
metzm committed Jun 30, 2023
1 parent fb64871 commit e0b4aa9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion raster/r.neighbors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ int main(int argc, char *argv[])
in_buf_size = (Rast_window_cols() + 2 * ncb.dist) * sizeof(DCELL) *
ncb.nsize * ncb.threads;
/* memory available for output buffer */
out_buf_size = (size_t)atoi(parm.memory->answer) * (1 << 20) - in_buf_size;
out_buf_size = (size_t)atoi(parm.memory->answer) * (1 << 20);
/* size_t is unsigned, check if any memory is left for output buffer */
if (out_buf_size <= in_buf_size)
out_buf_size = 0;
else
out_buf_size -= in_buf_size;
/* number of buffered rows for all output maps */
brows = out_buf_size / (sizeof(DCELL) * ncols * num_outputs);
/* set the output buffer rows to be at most covering the entire map */
Expand Down
7 changes: 6 additions & 1 deletion raster/r.patch/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ int main(int argc, char *argv[])
/* memory reserved for presult and patch */
in_buf_size = out_cell_size * ncols * nprocs * 2;
/* memory available for output buffer */
out_buf_size = (size_t)atoi(memory->answer) * (1 << 20) - in_buf_size;
out_buf_size = (size_t)atoi(memory->answer) * (1 << 20);
/* size_t is unsigned, check if any memory is left for output buffer */
if (out_buf_size <= in_buf_size)
out_buf_size = 0;
else
out_buf_size -= in_buf_size;
/* number of buffered output rows */
bufrows = out_buf_size / (out_cell_size * ncols);
/* set the output buffer rows to be at most covering the entire map */
Expand Down
7 changes: 6 additions & 1 deletion raster/r.resamp.filter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,12 @@ int main(int argc, char *argv[])
/* memory reserved for input */
in_buf_size = dst_w.cols * sizeof(DCELL) * row_scale * nprocs;
/* memory available for output buffer */
out_buf_size = (size_t)atoi(parm.memory->answer) * (1 << 20) - in_buf_size;
out_buf_size = (size_t)atoi(parm.memory->answer) * (1 << 20);
/* size_t is unsigned, check if any memory is left for output buffer */
if (out_buf_size <= in_buf_size)
out_buf_size = 0;
else
out_buf_size -= in_buf_size;
/* number of buffered output rows */
bufrows = out_buf_size / (sizeof(DCELL) * dst_w.cols);
/* set the output buffer rows to be at most covering the entire map */
Expand Down
7 changes: 6 additions & 1 deletion raster/r.series/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,12 @@ int main(int argc, char *argv[])
/* memory reserved for input */
in_buf_size = ncols * sizeof(DCELL) * num_inputs * nprocs;
/* memory available for output buffer */
out_buf_size = (size_t)atoi(parm.memory->answer) * (1 << 20) - in_buf_size;
out_buf_size = (size_t)atoi(parm.memory->answer) * (1 << 20);
/* size_t is unsigned, check if any memory is left for output buffer */
if (out_buf_size <= in_buf_size)
out_buf_size = 0;
else
out_buf_size -= in_buf_size;
/* number of buffered rows for all output maps */
bufrows = out_buf_size / (sizeof(DCELL) * ncols * num_outputs);
/* set the output buffer rows to be at most covering the entire map */
Expand Down

0 comments on commit e0b4aa9

Please sign in to comment.