[GRASS-SVN] r71242 - grass/branches/releasebranch_7_2/lib/raster

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 4 23:46:55 PDT 2017


Author: mmetz
Date: 2017-07-04 23:46:55 -0700 (Tue, 04 Jul 2017)
New Revision: 71242

Modified:
   grass/branches/releasebranch_7_2/lib/raster/get_row.c
   grass/branches/releasebranch_7_2/lib/raster/get_row_colr.c
   grass/branches/releasebranch_7_2/lib/raster/put_row.c
Log:
rasterlib: replace G_alloca for reading rows (avoids stack overflow, #3365, backport trunk r71241)

Modified: grass/branches/releasebranch_7_2/lib/raster/get_row.c
===================================================================
--- grass/branches/releasebranch_7_2/lib/raster/get_row.c	2017-07-05 06:45:24 UTC (rev 71241)
+++ grass/branches/releasebranch_7_2/lib/raster/get_row.c	2017-07-05 06:46:55 UTC (rev 71242)
@@ -137,10 +137,10 @@
 	G_fatal_error(_("Error reading raster data for row %d of <%s>"),
 		      row, fcb->name);
 
-    cmp = G_alloca(readamount);
+    cmp = G_malloc(readamount);
 
     if (read(fcb->data_fd, cmp, readamount) != readamount) {
-	G_freea(cmp);
+	G_free(cmp);
 	G_fatal_error(_("Error reading raster data for row %d of <%s>"),
 		      row, fcb->name);
     }
@@ -172,7 +172,7 @@
     else
 	memcpy(data_buf, cmp, readamount);
 
-    G_freea(cmp2);
+    G_free(cmp2);
 }
 
 static void read_data_uncompressed(int fd, int row, unsigned char *data_buf,
@@ -205,7 +205,7 @@
     if (fcb->gdal->vflip)
 	row = fcb->cellhd.rows - 1 - row;
 
-    buf = fcb->gdal->hflip ? G_alloca(fcb->cellhd.cols * fcb->cur_nbytes)
+    buf = fcb->gdal->hflip ? G_malloc(fcb->cellhd.cols * fcb->cur_nbytes)
 	: data_buf;
 
     err =
@@ -220,7 +220,7 @@
 	    memcpy(data_buf + i * fcb->cur_nbytes,
 		   buf + (fcb->cellhd.cols - 1 - i) * fcb->cur_nbytes,
 		   fcb->cur_nbytes);
-	G_freea(buf);
+	G_free(buf);
     }
 
     if (err != CE_None)
@@ -464,7 +464,7 @@
 static void transfer_to_cell_fi(int fd, void *cell)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    FCELL *work_buf = G_alloca(R__.rd_window.cols * sizeof(FCELL));
+    FCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(FCELL));
     int i;
 
     transfer_to_cell_XX(fd, work_buf);
@@ -473,13 +473,13 @@
 	((CELL *) cell)[i] = (fcb->col_map[i] == 0)
 	    ? 0 : Rast_quant_get_cell_value(&fcb->quant, work_buf[i]);
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void transfer_to_cell_di(int fd, void *cell)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
-    DCELL *work_buf = G_alloca(R__.rd_window.cols * sizeof(DCELL));
+    DCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(DCELL));
     int i;
 
     transfer_to_cell_XX(fd, work_buf);
@@ -488,12 +488,12 @@
 	((CELL *) cell)[i] = (fcb->col_map[i] == 0)
 	    ? 0 : Rast_quant_get_cell_value(&fcb->quant, work_buf[i]);
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void transfer_to_cell_if(int fd, void *cell)
 {
-    CELL *work_buf = G_alloca(R__.rd_window.cols * sizeof(CELL));
+    CELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
     int i;
 
     transfer_to_cell_XX(fd, work_buf);
@@ -501,12 +501,12 @@
     for (i = 0; i < R__.rd_window.cols; i++)
 	((FCELL *) cell)[i] = work_buf[i];
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void transfer_to_cell_df(int fd, void *cell)
 {
-    DCELL *work_buf = G_alloca(R__.rd_window.cols * sizeof(DCELL));
+    DCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(DCELL));
     int i;
 
     transfer_to_cell_XX(fd, work_buf);
@@ -514,12 +514,12 @@
     for (i = 0; i < R__.rd_window.cols; i++)
 	((FCELL *) cell)[i] = work_buf[i];
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void transfer_to_cell_id(int fd, void *cell)
 {
-    CELL *work_buf = G_alloca(R__.rd_window.cols * sizeof(CELL));
+    CELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
     int i;
 
     transfer_to_cell_XX(fd, work_buf);
@@ -527,12 +527,12 @@
     for (i = 0; i < R__.rd_window.cols; i++)
 	((DCELL *) cell)[i] = work_buf[i];
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void transfer_to_cell_fd(int fd, void *cell)
 {
-    FCELL *work_buf = G_alloca(R__.rd_window.cols * sizeof(FCELL));
+    FCELL *work_buf = G_malloc(R__.rd_window.cols * sizeof(FCELL));
     int i;
 
     transfer_to_cell_XX(fd, work_buf);
@@ -540,7 +540,7 @@
     for (i = 0; i < R__.rd_window.cols; i++)
 	((DCELL *) cell)[i] = work_buf[i];
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 /*
@@ -598,7 +598,7 @@
     int i;
 
     if (fcb->reclass_flag && data_type != CELL_TYPE) {
-	temp_buf = G_alloca(R__.rd_window.cols * sizeof(CELL));
+	temp_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
 	buf = temp_buf;
 	type = CELL_TYPE;
     }
@@ -625,7 +625,9 @@
 	rast = G_incr_void_ptr(rast, size);
     }
 
-    G_freea(temp_buf);
+    if (fcb->reclass_flag && data_type != CELL_TYPE) {
+	G_free(temp_buf);
+    }
 }
 
 /*!
@@ -835,10 +837,10 @@
 	return 1;
     }
 
-    compressed_buf = G_alloca(readamount);
+    compressed_buf = G_malloc(readamount);
 
     if (read(null_fd, compressed_buf, readamount) != readamount) {
-	G_freea(compressed_buf);
+	G_free(compressed_buf);
 	G_fatal_error(_("Error reading null data for row %d of <%s>"),
 		      row, fcb->name);
     }
@@ -849,7 +851,7 @@
 		      row, fcb->name);
     }
 
-    G_freea(compressed_buf);
+    G_free(compressed_buf);
 
     return 1;
 }
@@ -907,13 +909,13 @@
 	    fcb->null_cur_row = -1;
 	    if (fcb->map_type == CELL_TYPE) {
 		/* If can't read null row, assume  that all map 0's are nulls */
-		CELL *mask_buf = G_alloca(R__.rd_window.cols * sizeof(CELL));
+		CELL *mask_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
 
 		get_map_row_nomask(fd, mask_buf, row, CELL_TYPE);
 		for (j = 0; j < R__.rd_window.cols; j++)
 		    flags[j] = (mask_buf[j] == 0);
 
-		G_freea(mask_buf);
+		G_free(mask_buf);
 	    }
 	    else {		/* fp map */
 		/* if can't read null row, assume  that all data is valid */
@@ -969,14 +971,16 @@
 
 static void embed_mask(char *flags, int row)
 {
-    CELL *mask_buf = G_alloca(R__.rd_window.cols * sizeof(CELL));
+    CELL *mask_buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
     int i;
 
-    if (R__.auto_mask <= 0)
+    if (R__.auto_mask <= 0) {
+	G_free(mask_buf);
 	return;
+    }
 
     if (get_map_row_nomask(R__.mask_fd, mask_buf, row, CELL_TYPE) < 0) {
-	G_freea(mask_buf);
+	G_free(mask_buf);
 	return;
     }
 
@@ -989,7 +993,7 @@
 	if (mask_buf[i] == 0 || Rast_is_c_null_value(&mask_buf[i]))
 	    flags[i] = 1;
 
-    G_freea(mask_buf);
+    G_free(mask_buf);
 }
 
 static void get_null_value_row(int fd, char *flags, int row, int with_mask)
@@ -1021,7 +1025,7 @@
 	&& (R__.auto_mask <= 0 || !with_mask))
 	return;
 
-    null_buf = G_alloca(R__.rd_window.cols);
+    null_buf = G_malloc(R__.rd_window.cols);
 
     get_null_value_row(fd, null_buf, row, with_mask);
 
@@ -1036,7 +1040,7 @@
 	buf = G_incr_void_ptr(buf, size);
     }
 
-    G_freea(null_buf);
+    G_free(null_buf);
 }
 
 /*!
@@ -1063,13 +1067,13 @@
     if (!fcb->reclass_flag)
 	get_null_value_row(fd, flags, row, 1);
     else {
-	CELL *buf = G_alloca(R__.rd_window.cols * sizeof(CELL));
+	CELL *buf = G_malloc(R__.rd_window.cols * sizeof(CELL));
 	int i;
 
 	Rast_get_c_row(fd, buf, row);
 	for (i = 0; i < R__.rd_window.cols; i++)
 	    flags[i] = Rast_is_c_null_value(&buf[i]) ? 1 : 0;
 
-	G_freea(buf);
+	G_free(buf);
     }
 }

Modified: grass/branches/releasebranch_7_2/lib/raster/get_row_colr.c
===================================================================
--- grass/branches/releasebranch_7_2/lib/raster/get_row_colr.c	2017-07-05 06:45:24 UTC (rev 71241)
+++ grass/branches/releasebranch_7_2/lib/raster/get_row_colr.c	2017-07-05 06:46:55 UTC (rev 71242)
@@ -48,7 +48,7 @@
     void *p;
     int i;
 
-    array = G_alloca(cols * size);
+    array = G_malloc(cols * size);
 
     Rast_get_row(fd, array, row, type);
 
@@ -56,10 +56,10 @@
 	for (i = 0, p = array; i < cols; i++, p = G_incr_void_ptr(p, size))
 	    nul[i] = Rast_is_null_value(p, type);
 
-    set = G_alloca(cols);
+    set = G_malloc(cols);
 
     Rast_lookup_colors(array, red, grn, blu, set, cols, colors, type);
 
-    G_freea(array);
-    G_freea(set);
+    G_free(array);
+    G_free(set);
 }

Modified: grass/branches/releasebranch_7_2/lib/raster/put_row.c
===================================================================
--- grass/branches/releasebranch_7_2/lib/raster/put_row.c	2017-07-05 06:45:24 UTC (rev 71241)
+++ grass/branches/releasebranch_7_2/lib/raster/put_row.c	2017-07-05 06:46:55 UTC (rev 71242)
@@ -194,7 +194,7 @@
     if (n <= 0)
 	return;
 
-    work_buf = G_alloca(size + 1);
+    work_buf = G_malloc(size + 1);
 
     if (compressed)
 	set_file_pointer(fd, row);
@@ -209,7 +209,7 @@
     else
 	write_data(fd, row, work_buf, n);
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void convert_int(unsigned char *wk, char *null_buf, const CELL * rast,
@@ -340,7 +340,7 @@
     if (n <= 0)
 	return;
 
-    work_buf = G_alloca(fcb->cellhd.cols * sizeof(CELL) + 1);
+    work_buf = G_malloc(fcb->cellhd.cols * sizeof(CELL) + 1);
     wk = work_buf;
 
     if (compressed)
@@ -365,7 +365,7 @@
 	    trim_bytes(wk, n, len, len - nbytes);
 
 	total = nbytes * n;
-	compressed_buf = G_alloca(total + 1);
+	compressed_buf = G_malloc(total + 1);
 
 	compressed_buf[0] = work_buf[0] = nbytes;
 
@@ -394,7 +394,7 @@
 			      row, fcb->name);
 	}
 
-	G_freea(compressed_buf);
+	G_free(compressed_buf);
     }
     else {
 	nwrite = fcb->nbytes * n;
@@ -404,7 +404,7 @@
 			  row, fcb->name);
     }
 
-    G_freea(work_buf);
+    G_free(work_buf);
 }
 
 static void put_data_gdal(int fd, const void *rast, int row, int n,
@@ -426,7 +426,7 @@
     if (n <= 0)
 	return;
 
-    work_buf = G_alloca(n * size);
+    work_buf = G_malloc(n * size);
 
     switch (map_type) {
     case CELL_TYPE:
@@ -456,7 +456,7 @@
     err = Rast_gdal_raster_IO(fcb->gdal->band, GF_Write, 0, row, n, 1,
 			      work_buf, n, 1, datatype, 0, 0);
 
-    G_freea(work_buf);
+    G_free(work_buf);
 
     if (err != CE_None)
 	G_fatal_error(_("Error writing data via GDAL for row %d of <%s>"),
@@ -503,7 +503,7 @@
 
     fcb->null_row_ptr[row] = lseek(fcb->null_fd, 0L, SEEK_CUR);
 
-    compressed_buf = G_alloca(size + 1);
+    compressed_buf = G_malloc(size + 1);
 
     /* compress null bits file with LZ4, see lib/gis/compress.h */
     nwrite = G_lz4_compress(flags, size, compressed_buf, size);
@@ -519,7 +519,7 @@
 			  row, fcb->name);
     }
 
-    G_freea(compressed_buf);
+    G_free(compressed_buf);
 }
 
 /*!
@@ -693,7 +693,7 @@
 	return;
     }
 
-    null_buf = G_alloca(fcb->cellhd.cols);
+    null_buf = G_malloc(fcb->cellhd.cols);
     G_zero(null_buf, fcb->cellhd.cols);
 
     put_raster_data(fd, null_buf, buf, fcb->cur_row, fcb->cellhd.cols,
@@ -716,5 +716,5 @@
     if (!fcb->gdal)
 	put_null_value_row(fd, null_buf);
 
-    G_freea(null_buf);
+    G_free(null_buf);
 }



More information about the grass-commit mailing list