From fbe42e4056d02f10767fabbe068ac864fa5fbf43 Mon Sep 17 00:00:00 2001 From: cwhite911 Date: Tue, 14 Jul 2026 10:48:18 -0400 Subject: [PATCH 1/2] tools: Fix multiplications converted to a larger type Cast the first operand so that integer products used as allocation sizes, memset lengths, cell counts, and file offsets are computed in size_t, long, or off_t instead of int, and so that int or float products consumed as double (D_text_size, floor, region bounds) are computed in double. Fixes 24 CodeQL cpp/integer-multiplication-cast-to-long alerts across display, general, imagery, raster3d, vector, and visualization tools. --- display/d.graph/do_graph.c | 4 ++-- display/d.legend/draw.c | 2 +- general/g.pnmcomp/main.c | 4 ++-- imagery/i.fft/main.c | 4 ++-- imagery/i.ifft/main.c | 4 ++-- imagery/i.ortho.photo/i.ortho.rectify/report.c | 2 +- imagery/i.rectify/report.c | 2 +- imagery/i.segment/flag.c | 2 +- raster3d/r3.in.v5d/main.c | 6 +++--- raster3d/r3.out.v5d/main.c | 2 +- vector/v.extract/main.c | 3 ++- vector/v.surf.rst/main.c | 4 ++-- vector/v.vol.rst/main.c | 2 +- vector/v.vol.rst/user3.c | 2 +- visualization/ximgview/color.c | 4 ++-- 15 files changed, 24 insertions(+), 23 deletions(-) diff --git a/display/d.graph/do_graph.c b/display/d.graph/do_graph.c index 5b1e13deac0..def7b9b4477 100644 --- a/display/d.graph/do_graph.c +++ b/display/d.graph/do_graph.c @@ -50,7 +50,7 @@ int set_graph_stuff(void) int set_text_size(void) { if (hsize >= 0. && vsize >= 0. && hsize <= 100. && vsize <= 100.) { - D_text_size(hsize * xincr, vsize * yincr); + D_text_size((double)hsize * xincr, (double)vsize * yincr); G_debug(3, "text size initialized to [%.1f,%.1f]", hsize * xincr, vsize * yincr); } @@ -201,7 +201,7 @@ int do_size(const char *str) if (xper < 0. || yper < 0. || xper > 100. || yper > 100.) return (-1); - D_text_size(xper * xincr, yper * yincr); + D_text_size((double)xper * xincr, (double)yper * yincr); G_debug(3, "text size set to [%.1f,%.1f]", xper * xincr, yper * yincr); return (0); diff --git a/display/d.legend/draw.c b/display/d.legend/draw.c index d3353589d4d..47d7df32501 100644 --- a/display/d.legend/draw.c +++ b/display/d.legend/draw.c @@ -1098,7 +1098,7 @@ void draw(const char *map_name, int maptype, int color, int thin, int lines, 0.81)); /* ?? txsiz*.81=actual text width. */ if (ScaleFactor < 1.0) { txsiz = txsiz * ScaleFactor; - dots_per_line = (int)floor(dots_per_line * ScaleFactor); + dots_per_line = (int)floor((double)dots_per_line * ScaleFactor); } } diff --git a/general/g.pnmcomp/main.c b/general/g.pnmcomp/main.c index f0070259ffb..702da3523d6 100644 --- a/general/g.pnmcomp/main.c +++ b/general/g.pnmcomp/main.c @@ -338,7 +338,7 @@ int main(int argc, char *argv[]) if (opt.bg->answer) erase(out_buf, opt.bg->answer); - memset(out_mask_buf, 0, width * height); + memset(out_mask_buf, 0, (size_t)width * height); for (i = 0; opt.in->answers[i]; i++) { char *infile = opt.in->answers[i]; @@ -363,7 +363,7 @@ int main(int argc, char *argv[]) } else { read_pnm(infile, out_buf, 3); - memset(out_mask_buf, 255, width * height); + memset(out_mask_buf, 255, (size_t)width * height); } } diff --git a/imagery/i.fft/main.c b/imagery/i.fft/main.c index 38723e5d248..6fffeefef33 100644 --- a/imagery/i.fft/main.c +++ b/imagery/i.fft/main.c @@ -114,13 +114,13 @@ int main(int argc, char *argv[]) /* get the rows and columns in the current window */ rows = Rast_window_rows(); cols = Rast_window_cols(); - totsize = rows * cols; + totsize = (long)rows * cols; /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. data[...][0] will contain the real, and data[...][1] the complex component. */ - data = G_malloc(rows * cols * 2 * sizeof(double)); + data = G_malloc((size_t)rows * cols * 2 * sizeof(double)); /* allocate the space for one row of cell map data */ cell_real = Rast_allocate_d_buf(); diff --git a/imagery/i.ifft/main.c b/imagery/i.ifft/main.c index e84f0c157bc..10100939a6c 100644 --- a/imagery/i.ifft/main.c +++ b/imagery/i.ifft/main.c @@ -124,13 +124,13 @@ int main(int argc, char *argv[]) /* get the rows and columns in the current window */ rows = Rast_window_rows(); cols = Rast_window_cols(); - totsize = rows * cols; + totsize = (long)rows * cols; /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. DATA[0] will contain the real, and DATA[1] the complex component. */ - data = G_malloc(rows * cols * 2 * sizeof(double)); + data = G_malloc((size_t)rows * cols * 2 * sizeof(double)); /* allocate the space for one row of cell map data */ cell_real = Rast_allocate_d_buf(); diff --git a/imagery/i.ortho.photo/i.ortho.rectify/report.c b/imagery/i.ortho.photo/i.ortho.rectify/report.c index 463ea0e9145..5a1e13eb7f6 100644 --- a/imagery/i.ortho.photo/i.ortho.rectify/report.c +++ b/imagery/i.ortho.photo/i.ortho.rectify/report.c @@ -16,7 +16,7 @@ int report(time_t rectify, int ok) minutes = seconds / 60; hours = minutes / 60; minutes -= hours * 60; - ncells = target_window.rows * target_window.cols; + ncells = (long)target_window.rows * target_window.cols; G_verbose_message(_("%d rows, %d cols (%ld cells) completed in"), target_window.rows, target_window.cols, ncells); if (hours) diff --git a/imagery/i.rectify/report.c b/imagery/i.rectify/report.c index 463ea0e9145..5a1e13eb7f6 100644 --- a/imagery/i.rectify/report.c +++ b/imagery/i.rectify/report.c @@ -16,7 +16,7 @@ int report(time_t rectify, int ok) minutes = seconds / 60; hours = minutes / 60; minutes -= hours * 60; - ncells = target_window.rows * target_window.cols; + ncells = (long)target_window.rows * target_window.cols; G_verbose_message(_("%d rows, %d cols (%ld cells) completed in"), target_window.rows, target_window.cols, ncells); if (hours) diff --git a/imagery/i.segment/flag.c b/imagery/i.segment/flag.c index b638ec9b68e..3cfed9ec2ff 100644 --- a/imagery/i.segment/flag.c +++ b/imagery/i.segment/flag.c @@ -20,7 +20,7 @@ FLAG *flag_create(int nrows, int ncols) if (!new_flag->array) G_fatal_error(_("Out of memory!")); - temp = (unsigned char *)G_malloc(nrows * new_flag->leng * + temp = (unsigned char *)G_malloc((size_t)nrows * new_flag->leng * sizeof(unsigned char)); if (!temp) diff --git a/raster3d/r3.in.v5d/main.c b/raster3d/r3.in.v5d/main.c index 72c8a4a3214..f750892ff7e 100644 --- a/raster3d/r3.in.v5d/main.c +++ b/raster3d/r3.in.v5d/main.c @@ -117,11 +117,11 @@ void convert(char *openFile, RASTER3D_Region *region, int convertNull, res_r = v5d.ProjArgs[2]; res_c = v5d.ProjArgs[3]; res_l = v5d.VertArgs[1]; - region->south = region->north - region->rows * res_r; + region->south = region->north - (double)region->rows * res_r; region->west = v5d.ProjArgs[1]; - region->east = region->west + region->cols * res_c; + region->east = region->west + (double)region->cols * res_c; region->bottom = v5d.VertArgs[0]; - region->top = region->bottom + region->depths * res_l; + region->top = region->bottom + (double)region->depths * res_l; data1 = (float *)G_malloc(nrncnl * sizeof(float)); if (!data1) diff --git a/raster3d/r3.out.v5d/main.c b/raster3d/r3.out.v5d/main.c index a6be34e2469..23d01b8fe7f 100644 --- a/raster3d/r3.out.v5d/main.c +++ b/raster3d/r3.out.v5d/main.c @@ -195,7 +195,7 @@ void convert(char *fileout, int rows, int cols, int depths, int trueCoords) /****************/ - g = (float *)G_malloc(rows * cols * Nl[0] * sizeof(float)); + g = (float *)G_malloc((size_t)rows * cols * Nl[0] * sizeof(float)); d1p = &d1; f1p = (float *)&d1; cnt = 0; diff --git a/vector/v.extract/main.c b/vector/v.extract/main.c index 9e51f205fc3..4db2fe59abe 100644 --- a/vector/v.extract/main.c +++ b/vector/v.extract/main.c @@ -391,7 +391,8 @@ int main(int argc, char **argv) /* Fill cat_array with list of valid random numbers */ while (cat_count < nrandom) { /* Random number in range from 0 to largest CAT value */ - prnd = (int)floor(G_math_rand() * (ucat_array[ucat_count - 1] + 1)); + prnd = (int)floor((double)G_math_rand() * + (ucat_array[ucat_count - 1] + 1)); qsort(cat_array, cat_count, sizeof(int), cmp); /* Check if generated number isn't already in final list and is in list of existing CATs */ diff --git a/vector/v.surf.rst/main.c b/vector/v.surf.rst/main.c index 57f14c888b8..5c643037728 100644 --- a/vector/v.surf.rst/main.c +++ b/vector/v.surf.rst/main.c @@ -374,8 +374,8 @@ int main(int argc, char *argv[]) dmin = ew_res / 2; else dmin = ns_res / 2; - disk = n_rows * n_cols * sizeof(int); - sdisk = n_rows * n_cols * sizeof(short int); + disk = (size_t)n_rows * n_cols * sizeof(int); + sdisk = (size_t)n_rows * n_cols * sizeof(short int); snprintf(dmaxchar, sizeof(dmaxchar), "%f", dmin * 5); snprintf(dminchar, sizeof(dminchar), "%f", dmin); diff --git a/vector/v.vol.rst/main.c b/vector/v.vol.rst/main.c index 33e842f8e16..e1e8ca069f1 100644 --- a/vector/v.vol.rst/main.c +++ b/vector/v.vol.rst/main.c @@ -414,7 +414,7 @@ int main(int argc, char *argv[]) z_orig = current_region.bottom; dmin = amin1(ew_res, ns_res) / 2; - disk = n_rows * n_cols * sizeof(float); + disk = (size_t)n_rows * n_cols * sizeof(float); snprintf(dminchar, sizeof(dminchar), "%lf", dmin); nsizr = n_rows; diff --git a/vector/v.vol.rst/user3.c b/vector/v.vol.rst/user3.c index bc7fd69511f..76c802b5322 100644 --- a/vector/v.vol.rst/user3.c +++ b/vector/v.vol.rst/user3.c @@ -253,7 +253,7 @@ int COGRR1(double x_or, double y_or, double z_or, int n_rows, int n_cols, c normalization c */ - offset1 = nsizr * nsizc; + offset1 = (off_t)nsizr * nsizc; stepix = ew_res / dnorm; stepiy = ns_res / dnorm; diff --git a/visualization/ximgview/color.c b/visualization/ximgview/color.c index f24c59367f6..2a7b91cfc95 100644 --- a/visualization/ximgview/color.c +++ b/visualization/ximgview/color.c @@ -142,8 +142,8 @@ static int try_get_colors(Colormap cmap, int nr, int ng, int nb) int n_pixels; int r, g, b; - xpixels = (unsigned long *)G_realloc(xpixels, - nr * ng * nb * sizeof(unsigned long)); + xpixels = (unsigned long *)G_realloc(xpixels, (size_t)nr * ng * nb * + sizeof(unsigned long)); n_pixels = 0; xcolor.flags = DoRed | DoGreen | DoBlue; From 6f4bfa4c6fc5349280ab9801c6577d60236d88d2 Mon Sep 17 00:00:00 2001 From: Corey White Date: Mon, 24 Aug 2026 22:01:39 -0400 Subject: [PATCH 2/2] removed cases that require further investigation --- general/g.pnmcomp/main.c | 4 ++-- imagery/i.fft/main.c | 4 ++-- imagery/i.ifft/main.c | 4 ++-- imagery/i.ortho.photo/i.ortho.rectify/report.c | 2 +- imagery/i.rectify/report.c | 2 +- vector/v.extract/main.c | 3 +-- vector/v.surf.rst/main.c | 4 ++-- vector/v.vol.rst/main.c | 2 +- 8 files changed, 12 insertions(+), 13 deletions(-) diff --git a/general/g.pnmcomp/main.c b/general/g.pnmcomp/main.c index 702da3523d6..f0070259ffb 100644 --- a/general/g.pnmcomp/main.c +++ b/general/g.pnmcomp/main.c @@ -338,7 +338,7 @@ int main(int argc, char *argv[]) if (opt.bg->answer) erase(out_buf, opt.bg->answer); - memset(out_mask_buf, 0, (size_t)width * height); + memset(out_mask_buf, 0, width * height); for (i = 0; opt.in->answers[i]; i++) { char *infile = opt.in->answers[i]; @@ -363,7 +363,7 @@ int main(int argc, char *argv[]) } else { read_pnm(infile, out_buf, 3); - memset(out_mask_buf, 255, (size_t)width * height); + memset(out_mask_buf, 255, width * height); } } diff --git a/imagery/i.fft/main.c b/imagery/i.fft/main.c index 6fffeefef33..38723e5d248 100644 --- a/imagery/i.fft/main.c +++ b/imagery/i.fft/main.c @@ -114,13 +114,13 @@ int main(int argc, char *argv[]) /* get the rows and columns in the current window */ rows = Rast_window_rows(); cols = Rast_window_cols(); - totsize = (long)rows * cols; + totsize = rows * cols; /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. data[...][0] will contain the real, and data[...][1] the complex component. */ - data = G_malloc((size_t)rows * cols * 2 * sizeof(double)); + data = G_malloc(rows * cols * 2 * sizeof(double)); /* allocate the space for one row of cell map data */ cell_real = Rast_allocate_d_buf(); diff --git a/imagery/i.ifft/main.c b/imagery/i.ifft/main.c index 10100939a6c..e84f0c157bc 100644 --- a/imagery/i.ifft/main.c +++ b/imagery/i.ifft/main.c @@ -124,13 +124,13 @@ int main(int argc, char *argv[]) /* get the rows and columns in the current window */ rows = Rast_window_rows(); cols = Rast_window_cols(); - totsize = (long)rows * cols; + totsize = rows * cols; /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. DATA[0] will contain the real, and DATA[1] the complex component. */ - data = G_malloc((size_t)rows * cols * 2 * sizeof(double)); + data = G_malloc(rows * cols * 2 * sizeof(double)); /* allocate the space for one row of cell map data */ cell_real = Rast_allocate_d_buf(); diff --git a/imagery/i.ortho.photo/i.ortho.rectify/report.c b/imagery/i.ortho.photo/i.ortho.rectify/report.c index 5a1e13eb7f6..463ea0e9145 100644 --- a/imagery/i.ortho.photo/i.ortho.rectify/report.c +++ b/imagery/i.ortho.photo/i.ortho.rectify/report.c @@ -16,7 +16,7 @@ int report(time_t rectify, int ok) minutes = seconds / 60; hours = minutes / 60; minutes -= hours * 60; - ncells = (long)target_window.rows * target_window.cols; + ncells = target_window.rows * target_window.cols; G_verbose_message(_("%d rows, %d cols (%ld cells) completed in"), target_window.rows, target_window.cols, ncells); if (hours) diff --git a/imagery/i.rectify/report.c b/imagery/i.rectify/report.c index 5a1e13eb7f6..463ea0e9145 100644 --- a/imagery/i.rectify/report.c +++ b/imagery/i.rectify/report.c @@ -16,7 +16,7 @@ int report(time_t rectify, int ok) minutes = seconds / 60; hours = minutes / 60; minutes -= hours * 60; - ncells = (long)target_window.rows * target_window.cols; + ncells = target_window.rows * target_window.cols; G_verbose_message(_("%d rows, %d cols (%ld cells) completed in"), target_window.rows, target_window.cols, ncells); if (hours) diff --git a/vector/v.extract/main.c b/vector/v.extract/main.c index 4db2fe59abe..9e51f205fc3 100644 --- a/vector/v.extract/main.c +++ b/vector/v.extract/main.c @@ -391,8 +391,7 @@ int main(int argc, char **argv) /* Fill cat_array with list of valid random numbers */ while (cat_count < nrandom) { /* Random number in range from 0 to largest CAT value */ - prnd = (int)floor((double)G_math_rand() * - (ucat_array[ucat_count - 1] + 1)); + prnd = (int)floor(G_math_rand() * (ucat_array[ucat_count - 1] + 1)); qsort(cat_array, cat_count, sizeof(int), cmp); /* Check if generated number isn't already in final list and is in list of existing CATs */ diff --git a/vector/v.surf.rst/main.c b/vector/v.surf.rst/main.c index 5c643037728..57f14c888b8 100644 --- a/vector/v.surf.rst/main.c +++ b/vector/v.surf.rst/main.c @@ -374,8 +374,8 @@ int main(int argc, char *argv[]) dmin = ew_res / 2; else dmin = ns_res / 2; - disk = (size_t)n_rows * n_cols * sizeof(int); - sdisk = (size_t)n_rows * n_cols * sizeof(short int); + disk = n_rows * n_cols * sizeof(int); + sdisk = n_rows * n_cols * sizeof(short int); snprintf(dmaxchar, sizeof(dmaxchar), "%f", dmin * 5); snprintf(dminchar, sizeof(dminchar), "%f", dmin); diff --git a/vector/v.vol.rst/main.c b/vector/v.vol.rst/main.c index e1e8ca069f1..33e842f8e16 100644 --- a/vector/v.vol.rst/main.c +++ b/vector/v.vol.rst/main.c @@ -414,7 +414,7 @@ int main(int argc, char *argv[]) z_orig = current_region.bottom; dmin = amin1(ew_res, ns_res) / 2; - disk = (size_t)n_rows * n_cols * sizeof(float); + disk = n_rows * n_cols * sizeof(float); snprintf(dminchar, sizeof(dminchar), "%lf", dmin); nsizr = n_rows;