Skip to content

raster: Fix multiplications converted to a larger type - #7706

Open
cwhite911 wants to merge 4 commits into
OSGeo:mainfrom
cwhite911:raster-int-mult
Open

cwhite911 wants to merge 4 commits into
OSGeo:mainfrom
cwhite911:raster-int-mult

Conversation

@cwhite911

Copy link
Copy Markdown
Contributor

Fixes 36 CodeQL cpp/integer-multiplication-cast-to-long alerts across 15 raster tools (all remaining raster-tool alerts for this rule except the r.sunmask cluster, which is a larger float-precision type change handled separately).

Two classes, both fixed by casting the first operand so the product is computed in the consuming type (idiom from ed986f8):

  • Integer products used as allocation sizes, file seek offsets, and pointer offsets (G_malloc, G_fseek, n_array + row * cols * Rast_cell_size(...)), which could overflow int for very large regions: r.basins.fill, r.distance, r.geomorphon, r.in.mat, r.in.xyz, r.param.scale, r.regression.multi, r.resamp.filter, r.resamp.stats, r.surf.fractal, r.watershed (flag_create, ramseg), r.viewshed (totalsize, memory report).
  • float products consumed as double (arguments to exp, log2, sin, or double variables), now computed in double: r.ros, r.texture, r.viewshed (eventlist.cpp), r.sunhours (one line in solpos00.c). These can shift results by at most one float ulp; the r.texture testsuite, which compares numeric outputs, passes unchanged.

Verification: all 15 tools compile cleanly, and the six existing testsuites among them pass against nc_spm (r.watershed, r.viewshed, r.surf.fractal, r.geomorphon, r.resamp.filter, r.texture).

This is part of a larger effort to work through the open code scanning alerts, grouped into small PRs by issue type.

Written with the assistance of Claude Code.

Cast the first operand so that integer products used as allocation
sizes, file offsets, and pointer offsets are computed in size_t,
off_t, or the destination integer type instead of int, and so that
float products consumed as double (exp, log2, sin arguments and
double variables) are computed in double.

Fixes 36 CodeQL cpp/integer-multiplication-cast-to-long alerts in 15
raster tools.
Comment thread raster/r.param.scale/process.c Fixed
Comment thread raster/r.param.scale/process.c Fixed
@github-actions github-actions Bot added raster Related to raster data processing C Related code is in C C++ Related code is in C++ module labels Jul 14, 2026
@cwhite911 cwhite911 self-assigned this Jul 15, 2026
CodeQL flagged process.c:99,102: SQR() uses the float sqrarg, so
SQR(wsize) does a float multiplication that the macro's ternary then
widens to double, and the (size_t) cast applied afterwards does not
prevent the float overflow. wsize is an int window size, so compute
the window area as (size_t)wsize * wsize instead, keeping the whole
allocation size in integer/size_t math.

Fixes the 2 CodeQL cpp/integer-multiplication-cast-to-long alerts
this PR previously reintroduced.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C C++ Related code is in C++ module raster Related to raster data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants