diff --git a/imagery/i.landsat.acca/algorithm.c b/imagery/i.landsat.acca/algorithm.c index 9987e2782a7..a77e8c0513d 100644 --- a/imagery/i.landsat.acca/algorithm.c +++ b/imagery/i.landsat.acca/algorithm.c @@ -75,9 +75,12 @@ extern int hist_n; void acca_algorithm(Gfile *out, Gfile band[], int single_pass, int with_shadow, int cloud_signature) { - int i, count[5], hist_cold[hist_n], hist_warm[hist_n]; + int i, count[5]; double max, value[5], signa[5], idesert, review_warm, shift; + int *hist_cold = G_malloc(hist_n * sizeof(int)); + int *hist_warm = G_malloc(hist_n * sizeof(int)); + /* Reset variables ... */ for (i = 0; i < 5; i++) { count[i] = 0; @@ -213,6 +216,9 @@ void acca_algorithm(Gfile *out, Gfile band[], int single_pass, int with_shadow, acca_second(out, band[BAND6], review_warm, value[KUPPER], value[KLOWER]); /* CATEGORIES: IS_WARM_CLOUD, IS_COLD_CLOUD, IS_SHADOW, NULL (= NO_CLOUD) */ + G_free(hist_cold); + G_free(hist_warm); + return; } diff --git a/lib/gis/strings.c b/lib/gis/strings.c index 94b71877818..43d540473ed 100644 --- a/lib/gis/strings.c +++ b/lib/gis/strings.c @@ -267,17 +267,16 @@ char *G_str_replace(const char *buffer, const char *old_str, char *G_str_concat(const char **src_strings, int num_strings, const char *sep, int maxsize) { - char buffer[maxsize]; - int i; - char *end = buffer + maxsize; - char *p = NULL; - if (maxsize < 1 || num_strings < 1) return NULL; - memset(buffer, 0, sizeof(buffer)); + char *concat_str = NULL; + char *p = NULL; + char *buffer = G_malloc(maxsize * sizeof(char)); + char *end = buffer + maxsize; - for (i = 0; i < num_strings; i++) { + memset(buffer, 0, maxsize); + for (unsigned int i = 0; i < num_strings; i++) { if (i == 0) p = (char *)G__memccpy(buffer, src_strings[i], '\0', maxsize); else { @@ -287,8 +286,10 @@ char *G_str_concat(const char **src_strings, int num_strings, const char *sep, p = (char *)G__memccpy(p - 1, src_strings[i], '\0', end - p); } } + concat_str = G_store(buffer); + G_free(buffer); - return G_store(buffer); + return concat_str; } /*! diff --git a/lib/imagery/iscatt_core.c b/lib/imagery/iscatt_core.c index 7b0ee80169b..0c8e9279a6a 100644 --- a/lib/imagery/iscatt_core.c +++ b/lib/imagery/iscatt_core.c @@ -628,7 +628,7 @@ static void get_needed_bands(struct scCats *cats, int *b_needed_bands) */ static void free_compute_scatts_data(int *fd_bands, struct rast_row *bands_rows, int n_a_bands, int *bands_ids, - int *fd_cats_rasts, + int *fd_cats_rasts, int *b_needed_bands, FILE **f_cats_rasts_conds, int n_a_cats) { int i, band_id; @@ -641,16 +641,31 @@ static void free_compute_scatts_data(int *fd_bands, struct rast_row *bands_rows, G_free(bands_rows[band_id].null_row); } } + if (bands_rows) + G_free(bands_rows); - if (f_cats_rasts_conds) + if (fd_bands) + G_free(fd_bands); + + if (bands_ids) + G_free(bands_ids); + + if (b_needed_bands) + G_free(b_needed_bands); + + if (f_cats_rasts_conds) { for (i = 0; i < n_a_cats; i++) if (f_cats_rasts_conds[i]) fclose(f_cats_rasts_conds[i]); + G_free(f_cats_rasts_conds); + } - if (fd_cats_rasts) + if (fd_cats_rasts) { for (i = 0; i < n_a_cats; i++) if (fd_cats_rasts[i] >= 0) Rast_close(fd_cats_rasts[i]); + G_free(fd_cats_rasts); + } } /*! @@ -687,19 +702,21 @@ int I_compute_scatts(struct Cell_head *region, struct scCats *scatt_conds, const char *mapset; char header[1024]; - int fd_cats_rasts[scatt_conds->n_a_cats]; - FILE *f_cats_rasts_conds[scatt_conds->n_a_cats]; + int *fd_cats_rasts = + G_malloc(scatt_conds->n_a_cats * sizeof(scatt_conds->n_a_cats)); + FILE **f_cats_rasts_conds = + G_malloc(scatt_conds->n_a_cats * sizeof(FILE *)); - struct rast_row bands_rows[n_bands]; + struct rast_row *bands_rows = G_malloc(n_bands * sizeof(struct rast_row)); RASTER_MAP_TYPE data_type; int nrows, i_band, n_a_bands, band_id; int i_row, head_nchars, i_cat, id_cat; - int fd_bands[n_bands]; - int bands_ids[n_bands]; - int b_needed_bands[n_bands]; + int *fd_bands = G_malloc(n_bands * sizeof(n_bands)); + int *bands_ids = G_malloc(n_bands * sizeof(n_bands)); + int *b_needed_bands = G_malloc(n_bands * sizeof(n_bands)); Rast_set_window(region); @@ -727,7 +744,7 @@ int I_compute_scatts(struct Cell_head *region, struct scCats *scatt_conds, if ((mapset = G_find_raster2(bands[band_id], "")) == NULL) { free_compute_scatts_data(fd_bands, bands_rows, n_a_bands, - bands_ids, NULL, NULL, + bands_ids, NULL, b_needed_bands, NULL, scatt_conds->n_a_cats); G_warning(_("Unable to find raster <%s>"), bands[band_id]); return -1; @@ -736,7 +753,7 @@ int I_compute_scatts(struct Cell_head *region, struct scCats *scatt_conds, if ((fd_bands[n_a_bands] = Rast_open_old(bands[band_id], mapset)) < 0) { free_compute_scatts_data(fd_bands, bands_rows, n_a_bands, - bands_ids, NULL, NULL, + bands_ids, NULL, b_needed_bands, NULL, scatt_conds->n_a_cats); G_warning(_("Unable to open raster <%s>"), bands[band_id]); return -1; @@ -755,7 +772,7 @@ int I_compute_scatts(struct Cell_head *region, struct scCats *scatt_conds, if (Rast_read_range(bands[band_id], mapset, &bands_rows[band_id].rast_range) != 1) { free_compute_scatts_data(fd_bands, bands_rows, n_a_bands, - bands_ids, NULL, NULL, + bands_ids, NULL, b_needed_bands, NULL, scatt_conds->n_a_cats); G_warning(_("Unable to read range of raster <%s>"), bands[band_id]); @@ -781,7 +798,7 @@ int I_compute_scatts(struct Cell_head *region, struct scCats *scatt_conds, if (!f_cats_rasts_conds[i_cat]) { free_compute_scatts_data( fd_bands, bands_rows, n_a_bands, bands_ids, fd_cats_rasts, - f_cats_rasts_conds, scatt_conds->n_a_cats); + b_needed_bands, f_cats_rasts_conds, scatt_conds->n_a_cats); G_warning( _("Unable to open category raster condition file <%s>"), bands[band_id]); @@ -815,13 +832,13 @@ int I_compute_scatts(struct Cell_head *region, struct scCats *scatt_conds, bands_rows, scatts, fd_cats_rasts) == -1) { free_compute_scatts_data(fd_bands, bands_rows, n_a_bands, bands_ids, - fd_cats_rasts, f_cats_rasts_conds, - scatt_conds->n_a_cats); + fd_cats_rasts, b_needed_bands, + f_cats_rasts_conds, scatt_conds->n_a_cats); return -1; } } free_compute_scatts_data(fd_bands, bands_rows, n_a_bands, bands_ids, - fd_cats_rasts, f_cats_rasts_conds, + fd_cats_rasts, b_needed_bands, f_cats_rasts_conds, scatt_conds->n_a_cats); return 0; } diff --git a/raster/r.fill.stats/main.c b/raster/r.fill.stats/main.c index 5a99dd19092..24d110e29b6 100644 --- a/raster/r.fill.stats/main.c +++ b/raster/r.fill.stats/main.c @@ -156,8 +156,10 @@ void print_weights_matrix(long int rows, long int cols) { int i, j; size_t weight_matrix_line_length = 80; - char weight_matrix_line_buf[weight_matrix_line_length + 1]; - char weight_matrix_weight_buf[weight_matrix_line_length + 1]; + char *weight_matrix_line_buf = + G_malloc((weight_matrix_line_length + 1) * sizeof(char)); + char *weight_matrix_weight_buf = + G_malloc((weight_matrix_line_length + 1) * sizeof(char)); G_message(_("Spatial weights neighborhood (cells):")); for (i = 0; i < rows; i++) { @@ -184,6 +186,8 @@ void print_weights_matrix(long int rows, long int cols) } fprintf(stdout, "%s\n", weight_matrix_line_buf); } + G_free(weight_matrix_line_buf); + G_free(weight_matrix_weight_buf); } /* diff --git a/raster/r.mapcalc/map.c b/raster/r.mapcalc/map.c index 9ea0fafd439..118453c0610 100644 --- a/raster/r.mapcalc/map.c +++ b/raster/r.mapcalc/map.c @@ -725,9 +725,9 @@ void copy_history(const char *dst, int idx) Rast_write_history((char *)dst, &hist); } +#define RECORD_LEN 80 void create_history(const char *dst, expression *e) { - int RECORD_LEN = 80; int WIDTH = RECORD_LEN - 12; struct History hist; char *expr = format_expression(e);