[GRASS-SVN] r56185 - grass/trunk/raster/r.drain

svn_grass at osgeo.org svn_grass at osgeo.org
Fri May 10 04:22:56 PDT 2013


Author: huhabla
Date: 2013-05-10 04:22:56 -0700 (Fri, 10 May 2013)
New Revision: 56185

Modified:
   grass/trunk/raster/r.drain/filldir.c
   grass/trunk/raster/r.drain/main.c
Log:
Dynamic memory allocation for start points. Fixed possible segfault.


Modified: grass/trunk/raster/r.drain/filldir.c
===================================================================
--- grass/trunk/raster/r.drain/filldir.c	2013-05-10 09:50:16 UTC (rev 56184)
+++ grass/trunk/raster/r.drain/filldir.c	2013-05-10 11:22:56 UTC (rev 56185)
@@ -120,7 +120,7 @@
     }
     G_percent(1, 1, 1);
     advance_band3(fe, bnd);
-    build_one_row(i, nl, bnd->ns, bnd, dir, m[i]);
+    build_one_row(nl - 1, nl, bnd->ns, bnd, dir, m[nl - 1]);
     write(fd, dir, bufsz);
 
     G_free(dir);

Modified: grass/trunk/raster/r.drain/main.c
===================================================================
--- grass/trunk/raster/r.drain/main.c	2013-05-10 09:50:16 UTC (rev 56184)
+++ grass/trunk/raster/r.drain/main.c	2013-05-10 11:22:56 UTC (rev 56185)
@@ -50,7 +50,7 @@
 #include "local.h"
 
 /* should probably be updated to a pointer array & malloc/realloc as needed */
-#define MAX_POINTS 1024
+#define POINTS_INCREMENT 1024
 
 /* define a data structure to hold the point data */
 struct point
@@ -67,7 +67,9 @@
     int fe, fd, dir_fd;
     int i, have_points = 0;
     int new_id;
-    int nrows, ncols, points_row[MAX_POINTS], points_col[MAX_POINTS], npoints;
+    int nrows, ncols;
+    int *points_row = NULL, *points_col = NULL, npoints;
+    int increment_count;
     int cell_open(), cell_open_new();
     int map_id, dir_id;
     char map_name[GNAME_MAX], new_map_name[GNAME_MAX], dir_name[GNAME_MAX];
@@ -220,10 +222,11 @@
     ncols = Rast_window_cols();
 
     /* calculate true cell resolution */
-    m = (struct metrics *)G_malloc(nrows * sizeof(struct metrics));
-
-    if (m == NULL)
-	G_fatal_error(_("Metrics allocation"));
+    m = (struct metrics *)G_malloc(nrows * sizeof(struct metrics));    
+    points_row = (int*)G_calloc(POINTS_INCREMENT, sizeof(int));
+    points_col = (int*)G_calloc(POINTS_INCREMENT, sizeof(int));
+    
+    increment_count = 1;
     npoints = 0;
     if (coordopt->answer) {
 	for (i = 0; coordopt->answers[i] != NULL; i += 2) {
@@ -241,8 +244,12 @@
 	    points_row[npoints] = start_row;
 	    points_col[npoints] = start_col;
 	    npoints++;
-	    if (npoints >= MAX_POINTS)
-		G_fatal_error(_("Too many start points"));
+	    if (npoints == POINTS_INCREMENT * increment_count)
+	    {
+		increment_count++;
+		points_row = (int*)G_realloc(points_row, POINTS_INCREMENT * increment_count * sizeof(int));
+		points_col = (int*)G_realloc(points_col, POINTS_INCREMENT * increment_count * sizeof(int));
+	    }
 	    have_points = 1;
 	}
     }
@@ -293,8 +300,12 @@
 		points_row[npoints] = start_row;
 		points_col[npoints] = start_col;
 		npoints++;
-		if (npoints >= MAX_POINTS)
-		    G_fatal_error(_("Too many start points"));
+		if (npoints == POINTS_INCREMENT * increment_count)
+		{
+		    increment_count++;
+		    points_row = (int*)G_realloc(points_row, POINTS_INCREMENT * increment_count * sizeof(int));
+		    points_col = (int*)G_realloc(points_col, POINTS_INCREMENT * increment_count * sizeof(int));
+		}
 		have_points = 1;
 	    }
 	    Vect_close(&In);
@@ -572,6 +583,10 @@
     unlink(tempfile2);
     G_free(in_buf);
     G_free(out_buf);
+    if(points_row)
+	G_free(points_row);
+    if(points_col)
+	G_free(points_col);
 
     if (costmode == 1) {
 	close(dir_fd);



More information about the grass-commit mailing list