@@ -1636,7 +1636,7 @@ GMT_LOCAL char ** gmtapi_process_keys (void *V_API, const char *string, char typ
1636
1636
n = kk; /* May have lost some NULLs. Make a revised string for debug output */
1637
1637
for (k = 0; k < n; k++) {
1638
1638
strcat (revised, ",");
1639
- strcat (revised, s[k]);
1639
+ strncat (revised, s[k], GMT_LEN64-1 );
1640
1640
}
1641
1641
if (revised[0]) GMT_Report (API, GMT_MSG_DEBUG, "gmtapi_process_keys: Revised keys string is %s\n", &revised[1]);
1642
1642
*n_items = (unsigned int)n; /* Total number of remaining keys for this module */
@@ -4068,7 +4068,7 @@ GMT_LOCAL int gmtapi_import_ppm_header (struct GMT_CTRL *GMT, char *fname, bool
4068
4068
return GMT_ERROR_ON_FOPEN;
4069
4069
}
4070
4070
while ((c = fgetc (fp)) != '\n' && k < GMT_LEN128) text[k++] = c; /* Get first record up to newline */
4071
- text[k ] = '\0'; /* Terminate line */
4071
+ text[MIN(k,GMT_LEN128-1) ] = '\0'; /* Terminate line & check that we don't overflow */
4072
4072
if (text[1] == '5') /* Used P5 for grayscale image */
4073
4073
I->header->n_bands = 1;
4074
4074
else if (text[1] == '6') /* Used P6 for rgb image */
@@ -4086,7 +4086,7 @@ GMT_LOCAL int gmtapi_import_ppm_header (struct GMT_CTRL *GMT, char *fname, bool
4086
4086
ungetc (c, fp);
4087
4087
k = 0;
4088
4088
while ((c = fgetc (fp)) != '\n' && k < GMT_LEN128) text[k++] = c; /* Get next record up to newline */
4089
- text[k ] = '\0'; /* Terminate line */
4089
+ text[MIN(k,GMT_LEN128-1) ] = '\0'; /* Terminate line & check that we don't overflow */
4090
4090
n = sscanf (text, "%d %d %d", &I->header->n_rows, &I->header->n_columns, &max);
4091
4091
if (n == 2) { /* Must skip past a separate record with the max pixel value */
4092
4092
while ((c = fgetc (fp)) != '\n' ) k++;
@@ -4524,14 +4524,20 @@ GMT_LOCAL int gmtapi_export_ppm (struct GMT_CTRL *GMT, char *fname, struct GMT_I
4524
4524
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Alpha-channel not supported by PPM format - ignored\n");
4525
4525
n = I->header->size * I->header->n_bands;
4526
4526
if (!strncmp (I->header->mem_layout, "TRP", 3U)) { /* Easy street! */
4527
- if (fwrite (I->data, sizeof(char), n, fp) != n) return GMT_IMAGE_WRITE_ERROR;
4527
+ if (fwrite (I->data, sizeof(char), n, fp) != n) {
4528
+ gmt_fclose (GMT, fp);
4529
+ return GMT_IMAGE_WRITE_ERROR;
4530
+ }
4528
4531
}
4529
4532
else { /* Must change image layout first as PPM is strictly TRP */
4530
4533
char *data = NULL;
4531
4534
GMT_Report (GMT->parent, GMT_MSG_VERBOSE, "Must convert image from %s to TRP in order to write PPM file\n", I->header->mem_layout);
4532
4535
if ((data = gmt_M_memory_aligned (GMT, NULL, n, char)) == NULL) {
4533
4536
GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unable to allocate image memory in gmtapi_export_ppm to force TRP format - written as is\n");
4534
- if (fwrite (I->data, sizeof(char), n, fp) != n) return GMT_IMAGE_WRITE_ERROR;
4537
+ if (fwrite (I->data, sizeof(char), n, fp) != n) {
4538
+ gmt_fclose (GMT, fp);
4539
+ return GMT_IMAGE_WRITE_ERROR;
4540
+ }
4535
4541
}
4536
4542
else { /* Convert from TRB to TRP */
4537
4543
GMT_Change_Layout (GMT->parent, GMT_IS_IMAGE, "TRP", 0, I, data, NULL);
@@ -5367,7 +5373,7 @@ GMT_LOCAL struct GMT_MATRIX * gmtapi_read_matrix (struct GMT_CTRL *GMT, void *so
5367
5373
return (M);
5368
5374
}
5369
5375
5370
- GMT_LOCAL void * gmtapi_grid2matrix (struct GMTAPI_CTRL *API, struct GMT_GRID *In, struct GMT_MATRIX *Out) {
5376
+ GMT_LOCAL void *gmtapi_grid2matrix (struct GMTAPI_CTRL *API, struct GMT_GRID *In, struct GMT_MATRIX *Out) {
5371
5377
bool alloc = (Out == NULL);
5372
5378
uint64_t row, col, ij, ij_M;
5373
5379
double d;
@@ -5821,7 +5827,7 @@ GMT_LOCAL int gmtapi_export_vector (struct GMTAPI_CTRL *API, int object_ID, unsi
5821
5827
return GMT_NOERROR;
5822
5828
}
5823
5829
5824
- GMT_LOCAL struct GMT_VECTOR * gmtapi_read_vector (struct GMT_CTRL *GMT, void *source, unsigned int src_type, unsigned int mode) {
5830
+ GMT_LOCAL struct GMT_VECTOR *gmtapi_read_vector (struct GMT_CTRL *GMT, void *source, unsigned int src_type, unsigned int mode) {
5825
5831
/* We read the VECTOR from fp [or stdin].
5826
5832
* src_type can be GMT_IS_[FILE|STREAM|FDESC]
5827
5833
* mode is not used yet. We only do ascii file for now - later need to deal with -b
@@ -6298,8 +6304,10 @@ GMT_LOCAL int gmtapi_init_import (struct GMTAPI_CTRL *API, enum GMT_enum_family
6298
6304
gmt_M_memcpy (wesn, API->GMT->common.R.wesn, 4U, double);
6299
6305
}
6300
6306
}
6301
- if ((object_ID = GMT_Register_IO (API, family|GMT_VIA_MODULE_INPUT, GMT_IS_FILE, geometry, GMT_IN, wesn, current->arg)) == GMT_NOTSET)
6307
+ if ((object_ID = GMT_Register_IO (API, family|GMT_VIA_MODULE_INPUT, GMT_IS_FILE, geometry, GMT_IN, wesn, current->arg)) == GMT_NOTSET) {
6308
+ gmt_M_free (API->GMT, wesn);
6302
6309
return_value (API, API->error, GMT_NOTSET); /* Failure to register */
6310
+ }
6303
6311
n_reg++; /* Count of new items registered */
6304
6312
gmt_M_free (API->GMT, wesn);
6305
6313
if (first_ID == GMT_NOTSET) first_ID = object_ID; /* Found our first ID */
@@ -7921,9 +7929,11 @@ void * GMT_Read_Data (void *V_API, unsigned int family, unsigned int method, uns
7921
7929
if (infile) input = strdup (infile);
7922
7930
API = gmtapi_get_api_ptr (V_API);
7923
7931
API->error = GMT_NOERROR;
7924
- just_get_data = (gmt_M_file_is_memory (input)); /* A regular GMT resource passed via memory */
7925
- if (just_get_data && gmtapi_M_is_output (input)) /* A virtual output file created elsewhere, retrieve and we are done */
7932
+ just_get_data = (gmt_M_file_is_memory (input)); /* A regular GMT resource passed via memory */
7933
+ if (just_get_data && gmtapi_M_is_output (input)) { /* A virtual output file created elsewhere, retrieve and we are done */
7934
+ gmt_M_str_free (input);
7926
7935
return (GMT_Read_VirtualFile (API, input));
7936
+ }
7927
7937
reset = (mode & GMT_IO_RESET); /* We want to reset resource as unread after reading it */
7928
7938
if (reset) mode -= GMT_IO_RESET;
7929
7939
module_input = (family & GMT_VIA_MODULE_INPUT); /* Are we reading a resource that should be considered a module input? */
@@ -8085,6 +8095,7 @@ void * GMT_Read_Data (void *V_API, unsigned int family, unsigned int method, uns
8085
8095
}
8086
8096
if ((new_obj = gmtapi_get_data (API, in_ID, mode, data)) == NULL) {
8087
8097
if (reg_here) gmtlib_unregister_io (API, in_ID, GMT_IN); /* Since reading failed */
8098
+ gmt_M_str_free (input); /* Done with this variable) */
8088
8099
return_null (API, API->error);
8089
8100
}
8090
8101
if (reset) API->object[item]->status = 0; /* Reset to unread */
@@ -9154,7 +9165,10 @@ GMT_LOCAL int gmtapi_put_record_init (struct GMTAPI_CTRL *API, unsigned int mode
9154
9165
return_error (API, GMT_MEMORY_ERROR);
9155
9166
for (col = 0; col < V_obj->n_columns; col++) /* Set same export data type for all vectors */
9156
9167
V_obj->type[col] = GMT->current.setting.export_type;
9157
- if ((error = gmtlib_alloc_vectors (GMT, V_obj, S_obj->n_alloc)) != GMT_NOERROR) return (gmtlib_report_error (API, error));
9168
+ if ((error = gmtlib_alloc_vectors (GMT, V_obj, S_obj->n_alloc)) != GMT_NOERROR) {
9169
+ /* Have to free V_obj here */
9170
+ return (gmtlib_report_error (API, error));
9171
+ }
9158
9172
if (record->text) V_obj->text = gmt_M_memory (GMT, NULL, S_obj->n_alloc, char *);
9159
9173
S_obj->resource = V_obj; /* Save so we can get it next time */
9160
9174
}
@@ -12544,7 +12558,7 @@ GMT_LOCAL void * gmtapi_dataset2dataset (struct GMTAPI_CTRL *API, struct GMT_DAT
12544
12558
return Out;
12545
12559
}
12546
12560
12547
- GMT_LOCAL void * gmtapi_dataset2matrix (struct GMTAPI_CTRL *API, struct GMT_DATASET *In, struct GMT_MATRIX *Out, unsigned int header, unsigned int mode) {
12561
+ GMT_LOCAL void *gmtapi_dataset2matrix (struct GMTAPI_CTRL *API, struct GMT_DATASET *In, struct GMT_MATRIX *Out, unsigned int header, unsigned int mode) {
12548
12562
/* Convert a dataset to a matrix.
12549
12563
* If Out is not NULL then we assume it has enough rows and columns to hold the dataset records.
12550
12564
* Header controls if segment headers are written as NaN recs
@@ -12572,8 +12586,14 @@ GMT_LOCAL void * gmtapi_dataset2matrix (struct GMTAPI_CTRL *API, struct GMT_DATA
12572
12586
MH = gmt_get_M_hidden (Out);
12573
12587
MH->alloc_mode = GMT_ALLOC_INTERNALLY;
12574
12588
}
12575
- if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL) return (NULL);
12576
- if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL) return (NULL);
12589
+ if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL) {
12590
+ gmt_M_free (GMT, Out);
12591
+ return (NULL);
12592
+ }
12593
+ if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL) {
12594
+ gmt_M_free (GMT, Out);
12595
+ return (NULL);
12596
+ }
12577
12597
for (tbl = row_out = 0; tbl < In->n_tables; tbl++) {
12578
12598
D = In->table[tbl]; /* Shorthand to current input data table */
12579
12599
for (seg = 0; seg < D->n_segments; seg++) {
@@ -12618,7 +12638,10 @@ GMT_LOCAL void * gmtapi_dataset2vector (struct GMTAPI_CTRL *API, struct GMT_DATA
12618
12638
return (NULL);
12619
12639
}
12620
12640
}
12621
- if ((api_put_val = gmtapi_select_put_function (API, Out->type[0])) == NULL) return NULL; /* Since all columns are of same type we get the pointer here */
12641
+ if ((api_put_val = gmtapi_select_put_function (API, Out->type[0])) == NULL) { /* Since all columns are of same type we get the pointer here */
12642
+ gmt_M_free (GMT, Out);
12643
+ return NULL;
12644
+ }
12622
12645
for (tbl = row_out = 0; tbl < In->n_tables; tbl++) {
12623
12646
D = In->table[tbl]; /* Shorthand to current input data table */
12624
12647
for (seg = 0; seg < D->n_segments; seg++) {
@@ -12769,10 +12792,14 @@ GMT_LOCAL void * gmtapi_vector2matrix (struct GMTAPI_CTRL *API, struct GMT_VECTO
12769
12792
MH->alloc_mode = GMT_ALLOC_INTERNALLY;
12770
12793
}
12771
12794
12772
- if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL)
12795
+ if ((GMT_2D_to_index = gmtapi_get_2d_to_index (API, Out->shape, GMT_GRID_IS_REAL)) == NULL) {
12796
+ gmt_M_free (GMT, Out);
12773
12797
return (NULL);
12774
- if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL) /* Since all columns are of same type we get the pointer here */
12798
+ }
12799
+ if ((api_put_val = gmtapi_select_put_function (API, Out->type)) == NULL) { /* Since all columns are of same type we get the pointer here */
12800
+ gmt_M_free (GMT, Out);
12775
12801
return (NULL);
12802
+ }
12776
12803
for (col = 0; col < In->n_columns; col++) {
12777
12804
if ((api_get_val = gmtapi_select_get_function (API, In->type[col])) == NULL) {
12778
12805
gmt_M_free (GMT, Out);
@@ -12793,7 +12820,7 @@ GMT_LOCAL void * gmtapi_vector2matrix (struct GMTAPI_CTRL *API, struct GMT_VECTO
12793
12820
#define GMT_TYPE_MODE 1
12794
12821
#define GMT_FORMAT_MODE 1 /* Same as GMT_TYPE_MODE [not a typo] */
12795
12822
12796
- void * GMT_Convert_Data (void *V_API, void *In, unsigned int family_in, void *Out, unsigned int family_out, unsigned int flag[]) {
12823
+ void *GMT_Convert_Data (void *V_API, void *In, unsigned int family_in, void *Out, unsigned int family_out, unsigned int flag[]) {
12797
12824
/* Convert between valid pairs of objects, If Out == NULL then we allocate an output object,
12798
12825
* otherwise we assume we are given adequate space already. This is most likely restricted to a GMT_MATRIX.
12799
12826
* flag is an array with two unsigned integers controlling various aspects of the conversion:
@@ -13243,7 +13270,7 @@ GMT_LOCAL int gmtapi_change_imagelayout (struct GMTAPI_CTRL *API, char *code, un
13243
13270
}
13244
13271
13245
13272
if (changed) { /* Update the mem_layout for this image */
13246
- strncpy (I->header->mem_layout, code, strlen(code));
13273
+ strncpy (I->header->mem_layout, code, MIN( strlen(code),4 ));
13247
13274
if (I->alpha) I->header->mem_layout[3] = 'a'; /* Flag that we have transparency */
13248
13275
}
13249
13276
@@ -13800,7 +13827,7 @@ int GMT_Get_FilePath (void *V_API, unsigned int family, unsigned int direction,
13800
13827
if ((mode & GMT_FILE_CHECK) == 0) { /* Pass the local path back */
13801
13828
GMT_Report (API, GMT_MSG_DEBUG, "Replace file %s with %s\n", file, local_path);
13802
13829
if (c) /* Also append any file directives via modifiers */
13803
- strcat (local_path, c);
13830
+ strncat (local_path, c, PATH_MAX-1 );
13804
13831
gmt_M_str_free (*file_ptr);
13805
13832
*file_ptr = strdup (local_path);
13806
13833
}
0 commit comments