Skip to content

Commit 03578b2

Browse files
committed
lib/raster: Fix "Implicit conversion loses precision" warnings
1 parent 4912787 commit 03578b2

8 files changed

Lines changed: 72 additions & 35 deletions

File tree

‎lib/raster/cats.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static CELL read_cats(const char *element, const char *name, const char *mapset,
222222

223223
Rast_init_cats(buff, pcats);
224224
if (num >= 0)
225-
pcats->num = num;
225+
pcats->num = (CELL)num;
226226

227227
if (!old) {
228228
char fmt[256];

‎lib/raster/get_row.c‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
\author Original author CERL
1010
*/
1111

12+
#include <limits.h>
1213
#include <stdint.h>
1314
#include <string.h>
1415
#include <unistd.h>
@@ -98,21 +99,24 @@ static void read_data_fp_compressed(int fd, int row, unsigned char *data_buf,
9899

99100
*nbytes = fcb->nbytes;
100101

101-
ret = G_read_compressed(fcb->data_fd, readamount, data_buf, bufsize,
102-
fcb->cellhd.compressed);
102+
if (readamount > INT_MAX || bufsize > INT_MAX)
103+
G_fatal_error(_("Compressed fp raster row for <%s> is too large"),
104+
fcb->name);
105+
106+
ret = G_read_compressed(fcb->data_fd, (int)readamount, data_buf,
107+
(int)bufsize, fcb->cellhd.compressed);
103108
if (ret <= 0)
104109
G_fatal_error(_("Error uncompressing fp raster data for row %d of "
105110
"<%s>: error code %d"),
106111
row, fcb->name, ret);
107112
}
108113

109114
static void rle_decompress(unsigned char *dst, const unsigned char *src,
110-
int nbytes, int size)
115+
int nbytes, size_t size)
111116
{
112-
int pairs = size / (nbytes + 1);
113-
int i;
117+
size_t pairs = size / ((size_t)nbytes + 1);
114118

115-
for (i = 0; i < pairs; i++) {
119+
for (size_t i = 0; i < pairs; i++) {
116120
int repeat = *src++;
117121
int j;
118122

@@ -131,19 +135,32 @@ static void read_data_compressed(int fd, int row, unsigned char *data_buf,
131135
struct fileinfo *fcb = &R__.fileinfo[fd];
132136
off_t t1 = fcb->row_ptr[row];
133137
off_t t2 = fcb->row_ptr[row + 1];
134-
ssize_t readamount = t2 - t1;
138+
off_t row_size;
139+
size_t readamount;
135140
size_t bufsize;
136141
unsigned char *cmp, *cmp2;
137142
int n;
138143

144+
if (t2 < t1)
145+
G_fatal_error(_("Invalid raster row offset for row %d of <%s>"), row,
146+
fcb->name);
147+
148+
row_size = t2 - t1;
149+
if (row_size > SSIZE_MAX)
150+
G_fatal_error(_("Compressed raster row for <%s> is too large"),
151+
fcb->name);
152+
153+
readamount = (size_t)row_size;
154+
139155
if (lseek(fcb->data_fd, t1, SEEK_SET) == -1)
140156
G_fatal_error(
141157
_("Error seeking raster data file for row %d of <%s>: %s"), row,
142158
fcb->name, strerror(errno));
143159

144160
cmp = G_malloc(readamount);
145161

146-
if (read(fcb->data_fd, cmp, readamount) != readamount) {
162+
ssize_t nread = read(fcb->data_fd, cmp, readamount);
163+
if (nread < 0 || (size_t)nread != readamount) {
147164
G_free(cmp);
148165
G_fatal_error(_("Error reading raster data for row %d of <%s>: %s"),
149166
row, fcb->name, strerror(errno));
@@ -154,6 +171,12 @@ static void read_data_compressed(int fd, int row, unsigned char *data_buf,
154171

155172
/* Now decompress the row */
156173
if (fcb->cellhd.compressed > 0) {
174+
if (readamount == 0) {
175+
G_free(cmp2);
176+
G_fatal_error(_("Error reading raster data for row %d of <%s>"),
177+
row, fcb->name);
178+
}
179+
157180
/* one byte is nbyte count */
158181
n = *nbytes = *cmp++;
159182
readamount--;
@@ -167,9 +190,13 @@ static void read_data_compressed(int fd, int row, unsigned char *data_buf,
167190
if (fcb->cellhd.compressed == 1)
168191
rle_decompress(data_buf, cmp, n, readamount);
169192
else {
170-
if ((n = G_expand(cmp, readamount, data_buf, bufsize,
193+
if (readamount > INT_MAX || bufsize > INT_MAX)
194+
G_fatal_error(_("Compressed raster row for <%s> is too large"),
195+
fcb->name);
196+
197+
if ((n = G_expand(cmp, (int)readamount, data_buf, (int)bufsize,
171198
fcb->cellhd.compressed)) < 0 ||
172-
(unsigned int)n != bufsize) {
199+
(size_t)n != bufsize) {
173200
G_fatal_error(
174201
_("Error uncompressing raster data for row %d of <%s>"),
175202
row, fcb->name);
@@ -612,7 +639,7 @@ static void get_map_row(int fd, void *rast, int row, RASTER_MAP_TYPE data_type,
612639
int null_is_zero, int with_mask)
613640
{
614641
struct fileinfo *fcb = &R__.fileinfo[fd];
615-
int size = Rast_cell_size(data_type);
642+
size_t size = Rast_cell_size(data_type);
616643
CELL *temp_buf = NULL;
617644
void *buf;
618645
int type;
@@ -844,16 +871,15 @@ static int read_null_bits_compressed(int null_fd, unsigned char *flags, int row,
844871
off_t t2 = fcb->null_row_ptr[row + 1];
845872
size_t readamount = t2 - t1;
846873
unsigned char *compressed_buf;
847-
int res;
874+
ssize_t res;
848875

849876
if (lseek(null_fd, t1, SEEK_SET) == -1)
850877
G_fatal_error(
851878
_("Error seeking compressed null data for row %d of <%s>"), row,
852879
fcb->name);
853880

854881
if (readamount == size) {
855-
if ((res = read(null_fd, flags, size)) < 0 ||
856-
(unsigned int)res != size) {
882+
if ((res = read(null_fd, flags, size)) < 0 || (size_t)res != size) {
857883
G_fatal_error(
858884
_("Error reading compressed null data for row %d of <%s>"), row,
859885
fcb->name);
@@ -864,15 +890,19 @@ static int read_null_bits_compressed(int null_fd, unsigned char *flags, int row,
864890
compressed_buf = G_malloc(readamount);
865891

866892
if ((res = read(null_fd, compressed_buf, readamount)) < 0 ||
867-
(unsigned int)res != readamount) {
893+
(size_t)res != readamount) {
868894
G_free(compressed_buf);
869895
G_fatal_error(
870896
_("Error reading compressed null data for row %d of <%s>"), row,
871897
fcb->name);
872898
}
873899

874900
/* null bits file compressed with LZ4, see lib/gis/compress.h */
875-
if (G_lz4_expand(compressed_buf, readamount, flags, size) < 1) {
901+
if (readamount > INT_MAX || size > INT_MAX)
902+
G_fatal_error(_("Compressed null data for row %d of <%s> is too large"),
903+
row, fcb->name);
904+
905+
if (G_lz4_expand(compressed_buf, (int)readamount, flags, (int)size) < 1) {
876906
G_fatal_error(_("Error uncompressing null data for row %d of <%s>"),
877907
row, fcb->name);
878908
}

‎lib/raster/get_row_colr.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void Rast_get_row_colors(int fd, int row, struct Colors *colors,
3939
{
4040
int cols = Rast_window_cols();
4141
int type = Rast_get_map_type(fd);
42-
int size = Rast_cell_size(type);
42+
size_t size = Rast_cell_size(type);
4343
void *array;
4444
unsigned char *set;
4545
void *p;

‎lib/raster/history.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ void Rast_short_history(const char *name, const char *type,
272272
int Rast_command_history(struct History *hist)
273273
{
274274
char *cmdlin;
275-
int cmdlen;
275+
size_t cmdlen;
276276

277277
cmdlin = G_recreate_command();
278278
cmdlen = strlen(cmdlin);

‎lib/raster/null_val.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void Rast__set_null_value(void *rast, int numVals, int null_is_zero,
7979
RASTER_MAP_TYPE data_type)
8080
{
8181
if (null_is_zero) {
82-
G_zero((char *)rast, numVals * Rast_cell_size(data_type));
82+
G_zero((char *)rast, numVals * (int)Rast_cell_size(data_type));
8383
return;
8484
}
8585

‎lib/raster/open.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ static int open_raster_new_gdal(char *map, char *mapset,
537537

538538
fcb->cellhd = R__.wr_window;
539539
fcb->cellhd.compressed = 0;
540-
fcb->nbytes = Rast_cell_size(fcb->map_type);
540+
fcb->nbytes = (int)Rast_cell_size(fcb->map_type);
541541
/* for writing fcb->data is allocated to be R__.wr_window.cols *
542542
sizeof(CELL or DCELL or FCELL) */
543543
fcb->data = G_calloc(R__.wr_window.cols, fcb->nbytes);

‎lib/raster/put_row.c‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
**********************************************************************/
1515

16+
#include <limits.h>
1617
#include <string.h>
1718

1819
#include <sys/types.h>
@@ -424,7 +425,7 @@ static void put_data_gdal(int fd, const void *rast, int row, int n,
424425
int zeros_r_nulls, RASTER_MAP_TYPE map_type)
425426
{
426427
struct fileinfo *fcb = &R__.fileinfo[fd];
427-
int size = Rast_cell_size(map_type);
428+
size_t size = Rast_cell_size(map_type);
428429
DCELL null_val = fcb->gdal->null_val;
429430
const void *src;
430431
void *work_buf, *dst;
@@ -511,8 +512,14 @@ static void write_null_bits_compressed(const unsigned char *flags, int row,
511512
struct fileinfo *fcb = &R__.fileinfo[fd];
512513
unsigned char *compressed_buf;
513514
ssize_t nwrite;
514-
size_t cmax;
515-
int res;
515+
int compressed_size;
516+
int compressed_bound;
517+
ssize_t res;
518+
519+
if (size > INT_MAX)
520+
G_fatal_error(_("Null row is too large to compress"));
521+
522+
compressed_size = (int)size;
516523

517524
fcb->null_row_ptr[row] = lseek(fcb->null_fd, 0L, SEEK_CUR);
518525
if (fcb->null_row_ptr[row] == -1) {
@@ -522,22 +529,22 @@ static void write_null_bits_compressed(const unsigned char *flags, int row,
522529
}
523530

524531
/* get upper bound of compressed size */
525-
cmax = G_compress_bound(size, 3);
526-
compressed_buf = G_malloc(cmax);
532+
compressed_bound = G_compress_bound(compressed_size, 3);
533+
compressed_buf = G_malloc((size_t)compressed_bound);
527534

528535
/* compress null bits file with LZ4, see lib/gis/compress.h */
529-
nwrite = G_compress((unsigned char *)flags, size, compressed_buf, cmax, 3);
536+
nwrite = G_compress((unsigned char *)flags, compressed_size, compressed_buf,
537+
compressed_bound, 3);
530538

531539
if (nwrite > 0 && (size_t)nwrite < size) {
532-
if ((res = write(fcb->null_fd, compressed_buf, nwrite)) < 0 ||
533-
(unsigned int)res != nwrite)
540+
if ((res = write(fcb->null_fd, compressed_buf, (size_t)nwrite)) < 0 ||
541+
res != nwrite)
534542
G_fatal_error(
535543
_("Error writing compressed null data for row %d of <%s>: %s"),
536544
row, fcb->name, strerror(errno));
537545
}
538546
else {
539-
if ((res = write(fcb->null_fd, flags, size)) < 0 ||
540-
(unsigned int)res != size)
547+
if ((res = write(fcb->null_fd, flags, size)) < 0 || (size_t)res != size)
541548
G_fatal_error(
542549
_("Error writing compressed null data for row %d of <%s>: %s"),
543550
row, fcb->name, strerror(errno));
@@ -562,7 +569,7 @@ void Rast__write_null_bits(int fd, const unsigned char *flags)
562569
int row = fcb->null_cur_row++;
563570
off_t offset;
564571
size_t size;
565-
int res;
572+
ssize_t res;
566573

567574
size = Rast__null_bitstream_size(fcb->cellhd.cols);
568575

@@ -576,8 +583,7 @@ void Rast__write_null_bits(int fd, const unsigned char *flags)
576583
if (lseek(fcb->null_fd, offset, SEEK_SET) == -1)
577584
G_fatal_error(_("Error writing null row %d of <%s>"), row, fcb->name);
578585

579-
if ((res = write(fcb->null_fd, flags, size)) < 0 ||
580-
(unsigned int)res != size)
586+
if ((res = write(fcb->null_fd, flags, size)) < 0 || (size_t)res != size)
581587
G_fatal_error(_("Error writing null row %d of <%s>: %s"), row,
582588
fcb->name, strerror(errno));
583589
}

‎lib/raster/reclass.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ int Rast_is_reclassed_to(const char *name, const char *mapset, int *nrmaps,
7979
char ***rmaps)
8080
{
8181
FILE *fd;
82-
int i, j, k, l;
82+
int i;
83+
size_t j, k, l;
8384
char buf2[256], buf3[256];
8485

8586
fd = G_fopen_old_misc("cell_misc", "reclassed_to", name, mapset);

0 commit comments

Comments
 (0)