Skip to content

Commit fe8daa2

Browse files
authored
r.surf.idw: fix SHORT overflow (OSGeo#6396)
Co-authored-by: @metzm
1 parent 2aa4836 commit fe8daa2

File tree

4 files changed

+68
-70
lines changed

4 files changed

+68
-70
lines changed

raster/r.surf.idw/dist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ static double t1r, t2r;
2323
#define DIST_PARAMS struct dist_params
2424
DIST_PARAMS
2525
{
26-
short targetrow; /* interpolation row for which params apply */
26+
int targetrow; /* interpolation row for which params apply */
2727
double t1, t2, t3, t4;
2828
};
2929

3030
static DIST_PARAMS *lat_params, *nextcalc;
3131

3232
/* must be called once to establish the ellipsoid */
33-
int G_begin_geodesic_distance_l(short nrows, double a, double e2)
33+
int G_begin_geodesic_distance_l(int nrows, double a, double e2)
3434
{
3535
int i;
3636

raster/r.surf.idw/ll.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* by locating the two data closest to the specified column in */
1010
/* a linked list of row data */
1111

12-
int first_west_LL(EW *ewptr, SHORT col)
12+
int first_west_LL(EW *ewptr, int col)
1313
{
1414
if (ewptr->start == NULL) /* no data in this row */
1515
ewptr->walive = ewptr->ealive = FALSE;
@@ -31,7 +31,7 @@ int first_west_LL(EW *ewptr, SHORT col)
3131
return 0;
3232
}
3333

34-
double offset_distance_LL(SHORT offset)
34+
double offset_distance_LL(int offset)
3535
{
3636
extern double *lat_diff;
3737

@@ -46,13 +46,13 @@ int completed_row_LL(EW *ewptr)
4646
return (!ewptr->walive && !ewptr->ealive);
4747
}
4848

49-
int find_neighbors_LL(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col,
50-
int npoints, SHORT *neighbors)
49+
int find_neighbors_LL(EW *ewptr, NEIGHBOR *nbr_head, int row, int col,
50+
int npoints, int *neighbors)
5151
{
5252
MELEMENT **Mptr; /* double indirection !! */
5353
int westward = 1; /* 1 if west of interpolation point */
5454
double distance;
55-
short *active; /* TRUE if active search in this direction */
55+
int *active; /* TRUE if active search in this direction */
5656

5757
active = &ewptr->walive; /* TRUE if searching west in this row */
5858
Mptr = &ewptr->west; /* process search west first, then east */
@@ -84,7 +84,7 @@ int find_neighbors_LL(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col,
8484
/* This function exhausts all possible nearest neighhbors */
8585
/* within the row indexed by the ew search pointer */
8686

87-
int exhaust_search_LL(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col)
87+
int exhaust_search_LL(EW *ewptr, NEIGHBOR *nbr_head, int row, int col)
8888
{
8989
double distance;
9090

@@ -129,7 +129,7 @@ int extend_east(EW *ewptr)
129129
return 0;
130130
}
131131

132-
double distance_LL(SHORT row, SHORT col, MELEMENT *Mptr)
132+
double distance_LL(int row, int col, MELEMENT *Mptr)
133133
{
134134
extern double *rowlook, *collook;
135135

@@ -142,13 +142,13 @@ double distance_LL(SHORT row, SHORT col, MELEMENT *Mptr)
142142
/* Lookup tables storing pre-processed latitude and longitude data */
143143
/* are created for later use in selecting nearest neighbors */
144144

145-
int LL_lookup_tables(SHORT nrows, SHORT ncols)
145+
int LL_lookup_tables(int nrows, int ncols)
146146
{
147147
extern double *rowlook, *collook, *lat_diff;
148148
extern struct Cell_head window;
149149
double *nextrow, *nextcol, *next_diff,
150150
lon = 0., lat = window.north - (0.5 * window.ns_res);
151-
SHORT i;
151+
int i;
152152

153153
nextrow = rowlook = (double *)G_calloc(nrows, sizeof(double));
154154
for (i = 0; i < nrows; i++, nextrow++, lat -= window.ns_res)

raster/r.surf.idw/main.c

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,35 @@ CELL *cell, *mask;
4343
double *rowlook, *collook, *lat_diff, /* distances between latitudes */
4444
ew2;
4545

46-
short ll; /* TRUE if latitude-longitude projection */
46+
int ll; /* TRUE if latitude-longitude projection */
4747

4848
/* function pointers for LL function substitutes */
4949

50-
int first_west(EW *, SHORT);
51-
int first_west_LL(EW *, SHORT);
52-
int (*init_row_search)(EW *, SHORT); /* function pointer */
50+
int first_west(EW *, int);
51+
int first_west_LL(EW *, int);
52+
int (*init_row_search)(EW *, int); /* function pointer */
5353

5454
int completed_row(EW *);
5555
int completed_row_LL(EW *);
5656

5757
/* function pointer */
5858
int (*comp_row_search)(EW *);
5959

60-
int find_neighbors(EW *, NEIGHBOR *, SHORT, SHORT, int, SHORT *);
61-
int find_neighbors_LL(EW *, NEIGHBOR *, SHORT, SHORT, int, SHORT *);
60+
int find_neighbors(EW *, NEIGHBOR *, int, int, int, int *);
61+
int find_neighbors_LL(EW *, NEIGHBOR *, int, int, int, int *);
6262

6363
/* function pointer */
64-
int (*locate_neighbors)(EW *, NEIGHBOR *, SHORT, SHORT, int, SHORT *);
64+
int (*locate_neighbors)(EW *, NEIGHBOR *, int, int, int, int *);
6565

66-
int exhaust_search(EW *, NEIGHBOR *, SHORT, SHORT);
67-
int exhaust_search_LL(EW *, NEIGHBOR *, SHORT, SHORT);
66+
int exhaust_search(EW *, NEIGHBOR *, int, int);
67+
int exhaust_search_LL(EW *, NEIGHBOR *, int, int);
6868

6969
/* function pointer */
70-
int (*exhaust_row)(EW *, NEIGHBOR *, SHORT, SHORT);
70+
int (*exhaust_row)(EW *, NEIGHBOR *, int, int);
7171

72-
double offset_distance(SHORT);
73-
double offset_distance_LL(SHORT);
74-
double (*check_offset)(SHORT); /* function pointer */
72+
double offset_distance(int);
73+
double offset_distance_LL(int);
74+
double (*check_offset)(int); /* function pointer */
7575

7676
static int error_flag = 0;
7777
static char *input;
@@ -80,8 +80,8 @@ static char *output;
8080
int main(int argc, char **argv)
8181
{
8282
MELEMENT *rowlist;
83-
SHORT nrows, ncols;
84-
SHORT datarows;
83+
int nrows, ncols;
84+
int datarows;
8585
int npoints;
8686
struct GModule *module;
8787
struct History history;
@@ -199,7 +199,7 @@ int main(int argc, char **argv)
199199
/* calculations depends on the projection type; function */
200200
/* pointers are set dependent on projection type */
201201

202-
int lookup_and_function_ptrs(SHORT nrows, SHORT ncols)
202+
int lookup_and_function_ptrs(int nrows, int ncols)
203203
{
204204
double a, e2; /* used to control geodetic distance calculations */
205205

@@ -232,15 +232,15 @@ int lookup_and_function_ptrs(SHORT nrows, SHORT ncols)
232232
/* a matrix by interpolating from a given set of */
233233
/* irregularly spaced data points */
234234

235-
int interpolate(MELEMENT rowlist[], SHORT nrows, SHORT ncols, SHORT datarows,
235+
int interpolate(MELEMENT rowlist[], int nrows, int ncols, int datarows,
236236
int npoints, int out_fd, int maskfd)
237237
{
238238
extern CELL *cell;
239239

240240
MELEMENT *Rptr;
241241
EW *search, *ewptr, *current_row, /* start row for north/south search */
242242
*lastrow; /* last element in search array */
243-
SHORT row, col;
243+
int row, col;
244244
NEIGHBOR *nbr_head, *Nptr;
245245
double sum1, sum2;
246246

@@ -325,13 +325,13 @@ int interpolate(MELEMENT rowlist[], SHORT nrows, SHORT ncols, SHORT datarows,
325325
/* to be interpolated using data value of its neighbors */
326326

327327
int make_neighbors_list(
328-
EW *firstrow, EW *lastrow, EW *curr_row, SHORT row, SHORT col,
328+
EW *firstrow, EW *lastrow, EW *curr_row, int row, int col,
329329
NEIGHBOR *head, /* head points to dummy plus npoints neighbors */
330330
int npoints)
331331
{
332332
extern CELL *cell;
333333

334-
SHORT neighbors = 0, /* number of neighbors in current list */
334+
int neighbors = 0, /* number of neighbors in current list */
335335
nsearch = 1, ssearch = 1; /* expand search north and south */
336336
EW *north, *south;
337337

@@ -393,11 +393,11 @@ int make_neighbors_list(
393393
/******* END OF FUNCTION "MAKE_NEIGHBORS_LIST" ******************/
394394

395395
int search(EW **ewptr, /* double-indirection !! */
396-
NEIGHBOR *head, SHORT row, SHORT col, int npoints, SHORT *neighbors,
397-
EW *boundary, SHORT south /* search proceeds southward if == 1 */
396+
NEIGHBOR *head, int row, int col, int npoints, int *neighbors,
397+
EW *boundary, int south /* search proceeds southward if == 1 */
398398
)
399399
{
400-
SHORT new = 0; /* no prior search in first row in list */
400+
int new = 0; /* no prior search in first row in list */
401401
EW *current, *prior;
402402

403403
/* reset ewptr if row it points to has been thoroughly searched */
@@ -442,7 +442,7 @@ int search(EW **ewptr, /* double-indirection !! */
442442
}
443443

444444
int exhaust(EW **ewptr, /* double-indirection !! */
445-
NEIGHBOR *head, SHORT row, SHORT col)
445+
NEIGHBOR *head, int row, int col)
446446
{
447447
EW *current;
448448

@@ -469,7 +469,7 @@ int exhaust(EW **ewptr, /* double-indirection !! */
469469
return 0;
470470
}
471471

472-
double offset_distance(SHORT offset)
472+
double offset_distance(int offset)
473473
{
474474
return (offset * offset); /* compare squared distances in this case */
475475
}
@@ -483,8 +483,8 @@ int completed_row(EW *ewptr)
483483
}
484484

485485
EW *next_row(EW *ewptr,
486-
EW *boundary, /* row boundary of map in search direction */
487-
SHORT *new, SHORT south /* search proceeds southward if == 1 */
486+
EW *boundary, /* row boundary of map in search direction */
487+
int *new, int south /* search proceeds southward if == 1 */
488488
)
489489
{
490490
if (ewptr->next)
@@ -505,7 +505,7 @@ EW *next_row(EW *ewptr,
505505
/* by locating the two data closest to the specified column in */
506506
/* a linked list of row data */
507507

508-
int first_west(EW *ewptr, SHORT col)
508+
int first_west(EW *ewptr, int col)
509509
{
510510
if (ewptr->start == NULL) { /* no data in this row */
511511
ewptr->west = ewptr->east = NULL;
@@ -525,8 +525,8 @@ int first_west(EW *ewptr, SHORT col)
525525
/* This function evaluates nearest neighbor status for a given */
526526
/* datum and resets the row search pointer based on the result */
527527

528-
int find_neighbors(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col,
529-
int npoints, SHORT *neighbors)
528+
int find_neighbors(EW *ewptr, NEIGHBOR *nbr_head, int row, int col, int npoints,
529+
int *neighbors)
530530
{
531531
MELEMENT **Mptr; /* double indirection !! */
532532
int westward = 1; /* 1 if west of interpolation point */
@@ -559,7 +559,7 @@ int find_neighbors(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col,
559559
/* This function exhausts all possible nearest neighhbors */
560560
/* within the row indexed by the ew search pointer */
561561

562-
int exhaust_search(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col)
562+
int exhaust_search(EW *ewptr, NEIGHBOR *nbr_head, int row, int col)
563563
{
564564
double distance;
565565

@@ -586,7 +586,7 @@ int exhaust_search(EW *ewptr, NEIGHBOR *nbr_head, SHORT row, SHORT col)
586586
/************************************************************************/
587587
/* triangulate returns the square of the hypoteneuse */
588588

589-
double triangulate(MELEMENT *Mptr, SHORT row, SHORT col)
589+
double triangulate(MELEMENT *Mptr, int row, int col)
590590
{
591591
extern double *rowlook, *collook;
592592
int rowoff, coloff;
@@ -653,7 +653,7 @@ int sort_neighbors(NEIGHBOR *nbr_head, double distance)
653653
}
654654

655655
int free_row_lists(/* frees indexed row lists of data */
656-
MELEMENT *rowlist, SHORT nrows)
656+
MELEMENT *rowlist, int nrows)
657657
{
658658
int i;
659659
MELEMENT *Mptr, *prev;
@@ -674,11 +674,11 @@ int free_row_lists(/* frees indexed row lists of data */
674674
MELEMENT *row_lists(
675675
/* Search and make array-indexed doubly-linked lists of original data points
676676
*/
677-
SHORT rows, SHORT cols, /* total rows and columns in window */
678-
SHORT *datarows, /* number of rows with non-zero input data */
679-
int *npts, /* number of data points available */
680-
int fd, /* file descriptor, input */
681-
CELL *cell /* array of data for a single row */
677+
int rows, int cols, /* total rows and columns in window */
678+
int *datarows, /* number of rows with non-zero input data */
679+
int *npts, /* number of data points available */
680+
int fd, /* file descriptor, input */
681+
CELL *cell /* array of data for a single row */
682682
)
683683
{
684684
int row, col; /* row and column indices */
@@ -737,7 +737,7 @@ MELEMENT *row_lists(
737737
/* Lookup tables containing distance squared (in units of ns.res) */
738738
/* are created for later use in selecting nearest neighbors */
739739

740-
int lookup_tables(SHORT nrows, SHORT ncols)
740+
int lookup_tables(int nrows, int ncols)
741741
{
742742
extern double *rowlook, *collook, ew2;
743743
extern struct Cell_head window;

raster/r.surf.idw/main.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#include <grass/raster.h>
22

3-
#define SHORT short
4-
53
#define MELEMENT struct Melement
64
MELEMENT
75
{
8-
short x, y; /* grid coordinates */
6+
int x, y; /* grid coordinates */
97
int value;
108
MELEMENT *next, *prior; /* next and prior element in row list */
119
};
@@ -27,16 +25,16 @@ NEIGHBOR
2725
#define EW struct ew
2826
EW
2927
{
30-
MELEMENT *east, /* next eastward search in this row */
31-
*west, /* next westward search in this row */
32-
*start; /* starting point of east and west search in this row */
33-
short ealive, walive; /* used only for latitude-longitude,
28+
MELEMENT *east, /* next eastward search in this row */
29+
*west, /* next westward search in this row */
30+
*start; /* starting point of east and west search in this row */
31+
int ealive, walive; /* used only for latitude-longitude,
3432
TRUE if search is active in this direction */
3533
EW *next;
3634
};
3735

3836
/* dist.c */
39-
int G_begin_geodesic_distance_l(short, double, double);
37+
int G_begin_geodesic_distance_l(int, double, double);
4038
double LL_set_geodesic_distance_lat(double);
4139
double set_sdlmr(double);
4240
int LL_set_geodesic_distance(double *, int, int);
@@ -46,20 +44,20 @@ int free_dist_params(void);
4644
/* ll.c */
4745
int extend_west(EW *);
4846
int extend_east(EW *);
49-
double distance_LL(SHORT, SHORT, MELEMENT *);
50-
int LL_lookup_tables(SHORT, SHORT);
47+
double distance_LL(int, int, MELEMENT *);
48+
int LL_lookup_tables(int, int);
5149

5250
/* main.c */
53-
int lookup_and_function_ptrs(SHORT, SHORT);
54-
int interpolate(MELEMENT[], SHORT, SHORT, SHORT, int, int, int);
55-
int make_neighbors_list(EW *, EW *, EW *, SHORT, SHORT, NEIGHBOR *, int);
56-
int search(EW **, NEIGHBOR *, SHORT, SHORT, int, SHORT *, EW *, SHORT);
57-
int exhaust(EW **, NEIGHBOR *, SHORT, SHORT);
58-
EW *next_row(EW *, EW *, SHORT *, SHORT);
59-
double triangulate(MELEMENT *, SHORT, SHORT);
51+
int lookup_and_function_ptrs(int, int);
52+
int interpolate(MELEMENT[], int, int, int, int, int, int);
53+
int make_neighbors_list(EW *, EW *, EW *, int, int, NEIGHBOR *, int);
54+
int search(EW **, NEIGHBOR *, int, int, int, int *, EW *, int);
55+
int exhaust(EW **, NEIGHBOR *, int, int);
56+
EW *next_row(EW *, EW *, int *, int);
57+
double triangulate(MELEMENT *, int, int);
6058
int add_neighbor(MELEMENT **, NEIGHBOR *, double, int);
6159
int replace_neighbor(MELEMENT **, NEIGHBOR *, double);
6260
int sort_neighbors(NEIGHBOR *, double);
63-
int free_row_lists(MELEMENT *, SHORT);
64-
MELEMENT *row_lists(SHORT, SHORT, SHORT *, int *, int, CELL *);
65-
int lookup_tables(SHORT, SHORT);
61+
int free_row_lists(MELEMENT *, int);
62+
MELEMENT *row_lists(int, int, int *, int *, int, CELL *);
63+
int lookup_tables(int, int);

0 commit comments

Comments
 (0)