Skip to content

Commit

Permalink
r.neighbors, r.resamp.filter, r.resamp.interp: fix integer overflow (#…
Browse files Browse the repository at this point in the history
…2853)

* r.neighbors, r.resamp.filter, r.resamp.interp: fix integer overflow
  • Loading branch information
metzm authored Feb 24, 2023
1 parent 50815f9 commit 2ad3235
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions raster/r.neighbors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ int main(int argc, char *argv[])
* (eq. floor(neighborhood/2)) Thus original data start
* is shifted by ncb.dist! */
for (i = 0; i < num_outputs; i++)
outputs[i].buf[brow_idx * ncols + col] =
outputs[i].buf[(size_t)brow_idx * ncols + col] =
ncb.buf[t][ncb.dist][col + ncb.dist];
continue;
}
Expand All @@ -544,7 +544,7 @@ int main(int argc, char *argv[])

for (i = 0; i < num_outputs; i++) {
struct output *out = &outputs[i];
DCELL *rp = &out->buf[brow_idx * ncols + col];
DCELL *rp = &out->buf[(size_t)brow_idx * ncols + col];

if (n == 0) {
Rast_set_d_null_value(rp, 1);
Expand Down
4 changes: 2 additions & 2 deletions raster/r.resamp.filter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ static void filter(void)

num_rows = rows;

v_filter(&outbuf[(row - start) * dst_w.cols], bufs[t_id], row,
rows);
v_filter(&outbuf[(size_t)(row - start) * dst_w.cols],
bufs[t_id], row, rows);
#pragma omp atomic update
computed_row++;
}
Expand Down
24 changes: 16 additions & 8 deletions raster/r.resamp.interp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ int main(int argc, char *argv[])

if (Rast_is_d_null_value(&c)) {
Rast_set_d_null_value(
&outbuf[(row - start) * dst_w.cols + col], 1);
&outbuf[(size_t)(row - start) * dst_w.cols +
col],
1);
}
else {
outbuf[(row - start) * dst_w.cols + col] = c;
outbuf[(size_t)(row - start) * dst_w.cols + col] =
c;
}
}

Expand Down Expand Up @@ -288,10 +291,12 @@ int main(int argc, char *argv[])
Rast_is_d_null_value(&c10) ||
Rast_is_d_null_value(&c11)) {
Rast_set_d_null_value(
&outbuf[(row - start) * dst_w.cols + col], 1);
&outbuf[(size_t)(row - start) * dst_w.cols +
col],
1);
}
else {
outbuf[(row - start) * dst_w.cols + col] =
outbuf[(size_t)(row - start) * dst_w.cols + col] =
Rast_interp_bilinear(u, v, c00, c01, c10, c11);
}
}
Expand Down Expand Up @@ -361,10 +366,12 @@ int main(int argc, char *argv[])
Rast_is_d_null_value(&c32) ||
Rast_is_d_null_value(&c33)) {
Rast_set_d_null_value(
&outbuf[(row - start) * dst_w.cols + col], 1);
&outbuf[(size_t)(row - start) * dst_w.cols +
col],
1);
}
else {
outbuf[(row - start) * dst_w.cols + col] =
outbuf[(size_t)(row - start) * dst_w.cols + col] =
Rast_interp_bicubic(u, v, c00, c01, c02, c03,
c10, c11, c12, c13, c20,
c21, c22, c23, c30, c31,
Expand Down Expand Up @@ -419,7 +426,7 @@ int main(int argc, char *argv[])
}

if (do_lanczos) {
outbuf[(row - start) * dst_w.cols + col] =
outbuf[(size_t)(row - start) * dst_w.cols + col] =
Rast_interp_lanczos(u, v, c);
}
}
Expand All @@ -433,7 +440,8 @@ int main(int argc, char *argv[])

/* write to output map */
for (row = start; row < end; row++) {
Rast_put_d_row(outfile, &outbuf[(row - start) * dst_w.cols]);
Rast_put_d_row(outfile,
&outbuf[(size_t)(row - start) * dst_w.cols]);
}
written = end;
}
Expand Down

0 comments on commit 2ad3235

Please sign in to comment.