Skip to content

Commit ee87f21

Browse files
authored
CQ: we should check the return value of lseek (#5783)
An attempt to unify checks and error messages. There are more places where `lseek` return value should be checked, but I don't have time to work on them now. * CQ: we should check the return value of lseek An attempt to unify checks and error messages * r.random: remove unnecesarry lseek Rewinding a raster map can lead to lseek errors and generally should not be done, as raster reading functions perform seek on their own if required. * QA: add more checks for lseek return value * QC: finish cleaning up lseek error handling: * no need to cast -1 to off_t as we do not use K&R C * make error message arguments relocable * unify error messges * add missing includes for error reporting * Fix wording of error message on seek fail * Clean up unintentional changes
1 parent 56aa2b4 commit ee87f21

File tree

48 files changed

+756
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+756
-186
lines changed

imagery/i.ortho.photo/i.ortho.rectify/readcell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ block *get_block(struct cache *c, int idx)
135135
c->grid[idx] = p;
136136
c->refs[replace] = idx;
137137

138-
if (lseek(c->fd, offset, SEEK_SET) < 0)
138+
if (lseek(c->fd, offset, SEEK_SET) == -1)
139139
G_fatal_error(_("Error seeking on segment file"));
140140

141141
if (read(c->fd, p, sizeof(block)) < 0)

imagery/i.rectify/readcell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ block *get_block(struct cache *c, int idx)
119119
c->grid[idx] = p;
120120
c->refs[replace] = idx;
121121

122-
if (lseek(c->fd, offset, SEEK_SET) < 0)
122+
if (lseek(c->fd, offset, SEEK_SET) == -1)
123123
G_fatal_error(_("Error seeking on segment file"));
124124

125125
if (read(c->fd, p, sizeof(block)) < 0)

imagery/i.segment/cluster.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <unistd.h>
2323
#include <math.h>
2424
#include <time.h>
25+
#include <string.h>
26+
#include <errno.h>
27+
2528
#include <grass/gis.h>
2629
#include <grass/raster.h>
2730
#include <grass/glocale.h>
@@ -421,7 +424,11 @@ CELL cluster_bands(struct globals *globals)
421424
G_fatal_error(_("Too many objects: integer overflow"));
422425

423426
/* rewind temp file */
424-
lseek(cfd, 0, SEEK_SET);
427+
if (lseek(cfd, 0, SEEK_SET) == -1) {
428+
int err = errno;
429+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
430+
strerror(err), err);
431+
}
425432

426433
/****************************************************
427434
* PASS 2 *

lib/gis/open.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ int G_open_update(const char *element, const char *name)
192192

193193
fd = G__open(element, name, G_mapset(), 2);
194194
if (fd >= 0)
195-
lseek(fd, 0L, SEEK_END);
195+
if (lseek(fd, 0L, SEEK_END) == -1) {
196+
int err = errno;
197+
G_warning(_("File read/write operation failed: %s (%d)"),
198+
strerror(err), err);
199+
return -1;
200+
}
196201

197202
return fd;
198203
}
@@ -279,7 +284,12 @@ FILE *G_fopen_append(const char *element, const char *name)
279284
fd = G__open(element, name, G_mapset(), 2);
280285
if (fd < 0)
281286
return (FILE *)0;
282-
lseek(fd, 0L, SEEK_END);
287+
if (lseek(fd, 0L, SEEK_END) == -1) {
288+
int err = errno;
289+
G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
290+
err);
291+
return NULL;
292+
}
283293

284294
G_debug(2, "\tfile open: append (mode = a)");
285295
return fdopen(fd, "a");
@@ -307,7 +317,12 @@ FILE *G_fopen_modify(const char *element, const char *name)
307317
fd = G__open(element, name, G_mapset(), 2);
308318
if (fd < 0)
309319
return (FILE *)0;
310-
lseek(fd, 0L, SEEK_END);
320+
if (lseek(fd, 0L, SEEK_END) == -1) {
321+
int err = errno;
322+
G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
323+
err);
324+
return NULL;
325+
}
311326

312327
G_debug(2, "\tfile open: modify (mode = r+)");
313328
return fdopen(fd, "r+");

lib/gis/open_misc.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ int G_open_update_misc(const char *dir, const char *element, const char *name)
155155

156156
fd = G__open_misc(dir, element, name, G_mapset(), 2);
157157
if (fd >= 0)
158-
lseek(fd, 0L, SEEK_END);
158+
if (lseek(fd, 0L, SEEK_END) == -1) {
159+
int err = errno;
160+
G_warning(_("File read/write operation failed: %s (%d)"),
161+
strerror(err), err);
162+
return -1;
163+
}
159164

160165
return fd;
161166
}
@@ -222,7 +227,12 @@ FILE *G_fopen_append_misc(const char *dir, const char *element,
222227
fd = G__open_misc(dir, element, name, G_mapset(), 2);
223228
if (fd < 0)
224229
return (FILE *)0;
225-
lseek(fd, 0L, SEEK_END);
230+
if (lseek(fd, 0L, SEEK_END) == -1) {
231+
int err = errno;
232+
G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
233+
err);
234+
return (FILE *)-1;
235+
}
226236

227237
return fdopen(fd, "a");
228238
}
@@ -235,7 +245,12 @@ FILE *G_fopen_modify_misc(const char *dir, const char *element,
235245
fd = G__open_misc(dir, element, name, G_mapset(), 2);
236246
if (fd < 0)
237247
return (FILE *)0;
238-
lseek(fd, 0L, SEEK_END);
248+
if (lseek(fd, 0L, SEEK_END) == -1) {
249+
int err = errno;
250+
G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
251+
err);
252+
return (FILE *)-1;
253+
}
239254

240255
return fdopen(fd, "r+");
241256
}

lib/gis/seek.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ off_t G_ftell(FILE *fp)
5050
void G_fseek(FILE *fp, off_t offset, int whence)
5151
{
5252
#ifdef HAVE_FSEEKO
53-
if (fseeko(fp, offset, whence) != 0)
54-
G_fatal_error(_("Unable to seek: %s"), strerror(errno));
53+
if (fseeko(fp, offset, whence) != 0) {
54+
int err = errno;
55+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
56+
strerror(err), err);
57+
}
5558
#else
5659
long loff = (long)offset;
5760

5861
if ((off_t)loff != offset)
5962
G_fatal_error(_("Seek offset out of range"));
60-
if (fseek(fp, loff, whence) != 0)
61-
G_fatal_error(_("Unable to seek: %s"), strerror(errno));
63+
if (fseek(fp, loff, whence) != 0) {
64+
int err = errno;
65+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
66+
strerror(err), err);
67+
}
6268
#endif
6369
}

lib/raster/close.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ static int close_new(int fd, int ok)
396396
if (fcb->null_row_ptr) { /* compressed nulls */
397397
fcb->null_row_ptr[fcb->cellhd.rows] =
398398
lseek(fcb->null_fd, 0L, SEEK_CUR);
399+
if (fcb->null_row_ptr[fcb->cellhd.rows] == -1) {
400+
int err = errno;
401+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
402+
strerror(err), err);
403+
}
399404
Rast__write_null_row_ptrs(fd, fcb->null_fd);
400405
}
401406

@@ -437,6 +442,11 @@ static int close_new(int fd, int ok)
437442

438443
if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */
439444
fcb->row_ptr[fcb->cellhd.rows] = lseek(fcb->data_fd, 0L, SEEK_CUR);
445+
if (fcb->row_ptr[fcb->cellhd.rows] == -1) {
446+
int err = errno;
447+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
448+
strerror(err), err);
449+
}
440450
Rast__write_row_ptrs(fd);
441451
}
442452

@@ -531,6 +541,11 @@ void Rast__close_null(int fd)
531541

532542
if (fcb->null_row_ptr) { /* compressed nulls */
533543
fcb->null_row_ptr[fcb->cellhd.rows] = lseek(fcb->null_fd, 0L, SEEK_CUR);
544+
if (fcb->null_row_ptr[fcb->cellhd.rows] == -1) {
545+
int err = errno;
546+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
547+
strerror(err), err);
548+
}
534549
Rast__write_null_row_ptrs(fd, fcb->null_fd);
535550
G_free(fcb->null_row_ptr);
536551
}

lib/raster/format.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ static int write_row_ptrs(int nrows, off_t *row_ptr, int fd)
190190
unsigned char *buf, *b;
191191
int len, row, result;
192192

193-
lseek(fd, 0L, SEEK_SET);
193+
if (lseek(fd, 0L, SEEK_SET) == -1) {
194+
int err = errno;
195+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
196+
strerror(err), err);
197+
}
194198

195199
len = (nrows + 1) * nbytes + 1;
196200
b = buf = G_malloc(len);

lib/raster/get_row.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void read_data_fp_compressed(int fd, int row, unsigned char *data_buf,
9393
size_t bufsize = fcb->cellhd.cols * fcb->nbytes;
9494
int ret;
9595

96-
if (lseek(fcb->data_fd, t1, SEEK_SET) < 0)
96+
if (lseek(fcb->data_fd, t1, SEEK_SET) == -1)
9797
G_fatal_error(
9898
_("Error seeking fp raster data file for row %d of <%s>: %s"), row,
9999
fcb->name, strerror(errno));
@@ -138,7 +138,7 @@ static void read_data_compressed(int fd, int row, unsigned char *data_buf,
138138
unsigned char *cmp, *cmp2;
139139
int n;
140140

141-
if (lseek(fcb->data_fd, t1, SEEK_SET) < 0)
141+
if (lseek(fcb->data_fd, t1, SEEK_SET) == -1)
142142
G_fatal_error(
143143
_("Error seeking raster data file for row %d of <%s>: %s"), row,
144144
fcb->name, strerror(errno));
@@ -854,7 +854,7 @@ static int read_null_bits_compressed(int null_fd, unsigned char *flags, int row,
854854
unsigned char *compressed_buf;
855855
int res;
856856

857-
if (lseek(null_fd, t1, SEEK_SET) < 0)
857+
if (lseek(null_fd, t1, SEEK_SET) == -1)
858858
G_fatal_error(
859859
_("Error seeking compressed null data for row %d of <%s>"), row,
860860
fcb->name);
@@ -914,7 +914,7 @@ int Rast__read_null_bits(int fd, int row, unsigned char *flags)
914914

915915
offset = (off_t)size * R;
916916

917-
if (lseek(null_fd, offset, SEEK_SET) < 0)
917+
if (lseek(null_fd, offset, SEEK_SET) == -1)
918918
G_fatal_error(_("Error seeking null row %d for <%s>"), R, fcb->name);
919919

920920
if (read(null_fd, flags, size) != size)

lib/raster/put_row.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ static void set_file_pointer(int fd, int row)
140140
struct fileinfo *fcb = &R__.fileinfo[fd];
141141

142142
fcb->row_ptr[row] = lseek(fcb->data_fd, 0L, SEEK_CUR);
143+
if (fcb->row_ptr[row] == -1) {
144+
int err = errno;
145+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
146+
strerror(err), err);
147+
}
143148
}
144149

145150
static void convert_float(float *work_buf, char *null_buf, const FCELL *rast,
@@ -514,6 +519,11 @@ static void write_null_bits_compressed(const unsigned char *flags, int row,
514519
int res;
515520

516521
fcb->null_row_ptr[row] = lseek(fcb->null_fd, 0L, SEEK_CUR);
522+
if (fcb->null_row_ptr[row] == -1) {
523+
int err = errno;
524+
G_fatal_error(_("File read/write operation failed: %s (%d)"),
525+
strerror(err), err);
526+
}
517527

518528
/* get upper bound of compressed size */
519529
cmax = G_compress_bound(size, 3);
@@ -567,7 +577,7 @@ void Rast__write_null_bits(int fd, const unsigned char *flags)
567577

568578
offset = (off_t)size * row;
569579

570-
if (lseek(fcb->null_fd, offset, SEEK_SET) < 0)
580+
if (lseek(fcb->null_fd, offset, SEEK_SET) == -1)
571581
G_fatal_error(_("Error writing null row %d of <%s>"), row, fcb->name);
572582

573583
if ((res = write(fcb->null_fd, flags, size)) < 0 ||

0 commit comments

Comments
 (0)