Skip to content

Commit

Permalink
r.viewshed: fix CodeQL warnings
Browse files Browse the repository at this point in the history
Warnings on comparison of narrow type with wide type in loop condition.
  • Loading branch information
nilason committed Nov 14, 2022
1 parent 0e0d23f commit 4bf5958
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions raster/r.viewshed/grass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ init_event_list_in_memory(AEvent * eventList, char *rastName,
G_SURFACE_T **inrast;
int nrows = Rast_window_rows();
int ncols = Rast_window_cols();
if (nrows > maxDimension || ncols > maxDimension)
G_fatal_error(_("Grid size exceeds max dimension: %d"), maxDimension);

inrast = (G_SURFACE_T **)G_malloc(3 * sizeof(G_SURFACE_T *));
assert(inrast);
Expand Down Expand Up @@ -247,7 +249,7 @@ init_event_list_in_memory(AEvent * eventList, char *rastName,
Rast_get_row(infd, inrast[2], 0, data_type);

e.angle = -1;
for (i = 0; i < nrows; i++) {
for (i = 0; i < (dimensionType)nrows; i++) {
/*read in the raster row */

G_SURFACE_T *tmprast = inrast[0];
Expand All @@ -263,7 +265,7 @@ init_event_list_in_memory(AEvent * eventList, char *rastName,
G_percent(i, nrows, 2);

/*fill event list with events from this row */
for (j = 0; j < Rast_window_cols(); j++) {
for (j = 0; j < (dimensionType)ncols; j++) {
e.row = i;
e.col = j;

Expand Down Expand Up @@ -443,6 +445,8 @@ AMI_STREAM < AEvent > *init_event_list(char *rastName, Viewpoint * vp,
G_SURFACE_T **inrast;
int nrows = Rast_window_rows();
int ncols = Rast_window_cols();
if (nrows > maxDimension || ncols > maxDimension)
G_fatal_error(_("Grid size exceeds max dimension: %d"), maxDimension);

inrast = (G_SURFACE_T **)G_malloc(3 * sizeof(G_SURFACE_T *));
assert(inrast);
Expand All @@ -469,7 +473,7 @@ AMI_STREAM < AEvent > *init_event_list(char *rastName, Viewpoint * vp,
e.angle = -1;

/*start scanning through the grid */
for (i = 0; i < nrows; i++) {
for (i = 0; i < (dimensionType)nrows; i++) {

G_percent(i, nrows, 2);

Expand All @@ -486,7 +490,7 @@ AMI_STREAM < AEvent > *init_event_list(char *rastName, Viewpoint * vp,
Rast_set_null_value(inrast[2], ncols, data_type);

/*fill event list with events from this row */
for (j = 0; j < ncols; j++) {
for (j = 0; j < (dimensionType)ncols; j++) {

e.row = i;
e.col = j;
Expand Down Expand Up @@ -642,11 +646,16 @@ save_grid_to_GRASS(Grid * grid, char *filename, RASTER_MAP_TYPE type,
outrast = Rast_allocate_buf(type);
assert(outrast);

int nrows = Rast_window_rows();
int ncols = Rast_window_cols();
if (nrows > maxDimension || ncols > maxDimension)
G_fatal_error(_("Grid size exceeds max dimension: %d"), maxDimension);

dimensionType i, j;

for (i = 0; i < Rast_window_rows(); i++) {
G_percent(i, Rast_window_rows(), 5);
for (j = 0; j < Rast_window_cols(); j++) {
for (i = 0; i < (dimensionType)nrows; i++) {
G_percent(i, nrows, 5);
for (j = 0; j < (dimensionType)ncols; j++) {
if (is_invisible_nodata(grid->grid_data[i][j])) {
writeNodataValue(outrast, j, type);
}
Expand Down Expand Up @@ -723,14 +732,19 @@ save_vis_elev_to_GRASS(Grid * visgrid, char *elevfname, char *visfname,
visrast = Rast_allocate_buf(elev_data_type);
assert(visrast);

int nrows = Rast_window_rows();
int ncols = Rast_window_cols();
if (nrows > maxDimension || ncols > maxDimension)
G_fatal_error(_("Grid size exceeds max dimension: %d"), maxDimension);

dimensionType i, j;
double elev = 0, viewshed_value;

for (i = 0; i < Rast_window_rows(); i++) {
for (i = 0; i < (dimensionType)nrows; i++) {
/* get the row from elevation */
Rast_get_row(elevfd, elevrast, i, elev_data_type);

for (j = 0; j < Rast_window_cols(); j++) {
for (j = 0; j < (dimensionType)ncols; j++) {

/* read the current elevation value */
int isNull = 0;
Expand Down Expand Up @@ -865,10 +879,15 @@ save_io_visibilitygrid_to_GRASS(IOVisibilityGrid * visgrid,
counter++;
}

int nrows = Rast_window_rows();
int ncols = Rast_window_cols();
if (nrows > maxDimension || ncols > maxDimension)
G_fatal_error(_("Grid size exceeds max dimension: %d"), maxDimension);

dimensionType i, j;

for (i = 0; i < Rast_window_rows(); i++) {
for (j = 0; j < Rast_window_cols(); j++) {
for (i = 0; i < (dimensionType)nrows; i++) {
for (j = 0; j < (dimensionType)ncols; j++) {

if (curResult->row == i && curResult->col == j) {
/*cell is recodred in the visibility stream: it must be
Expand Down Expand Up @@ -972,12 +991,17 @@ save_io_vis_and_elev_to_GRASS(IOVisibilityGrid * visgrid, char *elevfname,

dimensionType i, j;
double elev = 0;
int nrows = Rast_window_rows();
int ncols = Rast_window_cols();
if (nrows > maxDimension || ncols > maxDimension)
G_fatal_error(_("Grid size exceeds max dimension: %d"), maxDimension);


for (i = 0; i < Rast_window_rows(); i++) {
for (i = 0; i < (dimensionType)nrows; i++) {

Rast_get_row(elevfd, elevrast, i, elev_data_type);

for (j = 0; j < Rast_window_cols(); j++) {
for (j = 0; j < (dimensionType)ncols; j++) {

/* read the current elevation value */
int isNull = 0;
Expand Down

0 comments on commit 4bf5958

Please sign in to comment.