Skip to content

Commit 6f5677f

Browse files
authored
Check result of gmt_get_ext before passing to other functions (#4981)
* Check result of gmt_get_ext before passing to other functions See #4979. The problem was we had the result of gmt_get_ext being passed directly to a strcmp function. However, this file had no extension so gmt_get_ext return NULL and that crashed the strcmp. There were two places in GMT where we did this. Now replaced by a separate check if an extensino was found. Closes #4979. * Update gmt_esri_io.c
1 parent 309b014 commit 6f5677f

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/gmt_esri_io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,12 @@ GMT_LOCAL int gmtesriio_read_info (struct GMT_CTRL *GMT, FILE *fp, struct GMT_GR
350350
int gmtlib_is_esri_grid (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header) {
351351
/* Determine if file is an ESRI Interchange ASCII file */
352352
FILE *fp = NULL;
353-
char record[GMT_BUFSIZ] = {""};
353+
char record[GMT_BUFSIZ] = {""}, *e = NULL;
354354
struct GMT_GRID_HEADER_HIDDEN *HH = gmt_get_H_hidden (header);
355355

356356
if (!strcmp (HH->name, "="))
357357
return (GMT_GRDIO_PIPE_CODECHECK); /* Cannot check on pipes */
358-
if (!strcmp (gmt_get_ext (HH->name), GMT_TILE_EXTENSION_REMOTE)) return (-1); /* Watch out for .jp2 tiles since they may contain W|E|S|N codes as well and the ESRI check comes before GDAL check*/
358+
if ((e = gmt_get_ext (HH->name)) && !strcmp (e, GMT_TILE_EXTENSION_REMOTE)) return (-1); /* Watch out for .jp2 tiles since they may contain W|E|S|N codes as well and the ESRI check comes before GDAL check*/
359359
if ((fp = gmt_fopen (GMT, HH->name, "r")) == NULL)
360360
return (GMT_GRDIO_OPEN_FAILED);
361361
if (fgets (record, GMT_BUFSIZ, fp) == NULL) { /* Just get first line. Not using gmt_fgets since we may be reading a binary file */

src/grdimage.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static int parse (struct GMT_CTRL *GMT, struct GRDIMAGE_CTRL *Ctrl, struct GMT_O
283283
Ctrl->A.file = strdup (opt->arg);
284284
Ctrl->A.way = 1; /* Building image directly, use TRPa layout, no call to GDAL */
285285
}
286-
else if (!strcmp (gmt_get_ext (opt->arg), "ppm")) { /* Want a ppm image which we can do without GDAL */
286+
else if ((c = gmt_get_ext (opt->arg)) && !strcmp (c, "ppm")) { /* Want a ppm image which we can do without GDAL */
287287
Ctrl->A.file = strdup (opt->arg);
288288
Ctrl->A.way = 1; /* Building image directly, use TRP layout, no call to GDAL, writing a PPM file */
289289
}
@@ -1141,7 +1141,7 @@ EXTERN_MSC int GMT_grdimage (void *V_API, int mode, void *args) {
11411141
uint64_t node, k, kk, dim[GMT_DIM_SIZE] = {0, 0, 3, 0};
11421142
int error = 0, ret_val = GMT_NOERROR, ftype = GMT_NOTSET;
11431143

1144-
char *img_ProjectionRefPROJ4 = NULL, *way[2] = {"via GDAL", "directly"}, cmd[GMT_LEN256] = {""}, data_grd[GMT_VF_LEN] = {""};
1144+
char *img_ProjectionRefPROJ4 = NULL, *way[2] = {"via GDAL", "directly"}, cmd[GMT_LEN256] = {""}, data_grd[GMT_VF_LEN] = {""}, *e = NULL;
11451145
unsigned char *bitimage_8 = NULL, *bitimage_24 = NULL, *rgb_used = NULL;
11461146

11471147
double dx, dy, x_side, y_side, x0 = 0.0, y0 = 0.0;
@@ -1192,7 +1192,7 @@ EXTERN_MSC int GMT_grdimage (void *V_API, int mode, void *args) {
11921192
use_intensity_grid = (Ctrl->I.active && !Ctrl->I.constant); /* We want to use an intensity grid */
11931193
if (Ctrl->A.file) {
11941194
Ctrl->Out.file = Ctrl->A.file; Ctrl->A.file = NULL; /* Only use Out.file for writing */
1195-
if (strcmp (gmt_get_ext (Ctrl->Out.file), "ppm")) { /* Turn off the automatic creation of aux files by GDAL */
1195+
if ((e = gmt_get_ext (Ctrl->Out.file)) && strcmp (e, "ppm")) { /* Turn off the automatic creation of aux files by GDAL */
11961196
#ifdef WIN32
11971197
if (_putenv ("GDAL_PAM_ENABLED=NO"))
11981198
#else

0 commit comments

Comments
 (0)