[GRASS-SVN] r52728 - grass/branches/develbranch_6/ps/ps.map

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Aug 19 06:23:07 PDT 2012


Author: mmetz
Date: 2012-08-19 06:23:07 -0700 (Sun, 19 Aug 2012)
New Revision: 52728

Modified:
   grass/branches/develbranch_6/ps/ps.map/local_proto.h
   grass/branches/develbranch_6/ps/ps.map/main.c
   grass/branches/develbranch_6/ps/ps.map/r_cell.c
   grass/branches/develbranch_6/ps/ps.map/r_instructions.c
Log:
fix various bugs introduced with r45586 including memory corruption, fix buffer overflow

Modified: grass/branches/develbranch_6/ps/ps.map/local_proto.h
===================================================================
--- grass/branches/develbranch_6/ps/ps.map/local_proto.h	2012-08-19 08:12:53 UTC (rev 52727)
+++ grass/branches/develbranch_6/ps/ps.map/local_proto.h	2012-08-19 13:23:07 UTC (rev 52728)
@@ -196,11 +196,6 @@
 void reset_map_location(void);
 void print_papers(void);
 
-/* r_instructions.c */
-void read_instructions(FILE *, struct PS_data *, int, int, int,
-		       struct scalebar *, int *, int *,
-		       struct PS_group *);
-
 /* r_border.c */
 int read_border(void);
 
@@ -216,6 +211,9 @@
 /* r_info.c */
 int read_info(void);
 
+/* r_instructions.c */
+void read_instructions(int, int);
+
 /* r_labels.c */
 int read_labels(char *, char *);
 

Modified: grass/branches/develbranch_6/ps/ps.map/main.c
===================================================================
--- grass/branches/develbranch_6/ps/ps.map/main.c	2012-08-19 08:12:53 UTC (rev 52727)
+++ grass/branches/develbranch_6/ps/ps.map/main.c	2012-08-19 13:23:07 UTC (rev 52728)
@@ -132,6 +132,10 @@
     if (G_parser(argc, argv))
 	usage(0);
     
+    /* PS.map_* variables are set to 0 (not defined) and then may be
+     * reset by 'maploc'.  When script is read, main() should call
+     * reset_map_location() to reset map size to fit to paper */
+    
     G_zero(&PS, sizeof(struct PS_data));
     
     /* Print paper sizes to stdout */
@@ -187,10 +191,6 @@
     PS.cell_fd = -1;
     PS.do_border = TRUE;
 
-    /* PS.map_* variables are set to 0 (not defined) and then may be
-     * reset by 'maploc'.  When script is read, main() should call
-     * reset_map_location() to reset map size to fit to paper */
-    
     /* arguments */
     if (input_file->answer && strcmp(input_file->answer, "-")) {
 	inputfd = fopen(input_file->answer, "r");
@@ -244,9 +244,8 @@
     if (G_set_window(&PS.w) == -1)
 	G_fatal_error(_("Current region cannot be set."));
     
-    read_instructions(inputfd, &PS, copies_set, ps_copies, can_reset_scale,
-		      &sb, &do_mapinfo, &do_vlegend, &grp);
-    
+    read_instructions(copies_set, can_reset_scale);
+
     /* reset map location base on 'paper' on 'location' */
     reset_map_location();
     

Modified: grass/branches/develbranch_6/ps/ps.map/r_cell.c
===================================================================
--- grass/branches/develbranch_6/ps/ps.map/r_cell.c	2012-08-19 08:12:53 UTC (rev 52727)
+++ grass/branches/develbranch_6/ps/ps.map/r_cell.c	2012-08-19 13:23:07 UTC (rev 52728)
@@ -9,7 +9,8 @@
 
 int read_cell(char *name, char *mapset)
 {
-    char fullname[100];
+    /* full name can be "name at mapset in mapset" */
+    char fullname[GNAME_MAX + 2 * GMAPSET_MAX + 4];
 
     PS.do_colortable = 0;
     if (PS.cell_fd >= 0) {

Modified: grass/branches/develbranch_6/ps/ps.map/r_instructions.c
===================================================================
--- grass/branches/develbranch_6/ps/ps.map/r_instructions.c	2012-08-19 08:12:53 UTC (rev 52727)
+++ grass/branches/develbranch_6/ps/ps.map/r_instructions.c	2012-08-19 13:23:07 UTC (rev 52728)
@@ -1,4 +1,5 @@
 #include <string.h>
+#include <stdio.h>
 
 #include <grass/colors.h>
 #include <grass/imagery.h>
@@ -7,6 +8,11 @@
 
 #define KEY(x) (strcmp(key,x)==0)
 
+extern FILE *inputfd;
+extern int do_mapinfo;
+extern int do_vlegend;
+extern int ps_copies;
+
 static char *help[] = {
     "rast       rastermap             setcolor   val_range(s) color",
     "vpoints    vector points map     scalebar   [f|s]",
@@ -26,14 +32,12 @@
     ""
 };
 
-void read_instructions(FILE *inputfd, struct PS_data *PS,
-		       int copies_set, int ps_copies, int can_reset_scale,
-		       struct scalebar *sb, int *do_mapinfo, int *do_vlegend,
-		       struct PS_group *grp)
+void read_instructions(int copies_set, int can_reset_scale)
 {
     int i;
     int iflag;
-    char name[GNAME_MAX], mapset[GMAPSET_MAX];
+    /* name can be fully qualified */
+    char name[GNAME_MAX + GMAPSET_MAX], mapset[GMAPSET_MAX];
     char buf[1024];
 
     iflag = 0;
@@ -44,8 +48,17 @@
 
 	if (!input(1, buf, help)) {
 	    if (!iflag) {
-		if (G_getl2(buf, 12, inputfd))
-		    G_warning(_("Data exists after final 'end' instruction!"));
+		/* TODO: check also if instructions are piped in 
+		 * through stdin, not interactive */
+		if (inputfd != stdin) {
+		    while (G_getl2(buf, 12, inputfd)) {
+			/* empty lines and comments are fine */
+			if (key_data(buf, &key, &data))
+			    G_warning(_("Data exist after final 'end' instruction!"));
+		    }
+		    fclose(inputfd);
+		    inputfd = stdin;
+		}
 		break;
 	    }
 	    iflag = 0;
@@ -86,11 +99,11 @@
 
 	    n = sscanf(data, "%lf %lf %lf %lf", &x, &y, &w, &h);
 	    if (n == 2 || n == 4) {
-		PS->map_x_orig = x;
-		PS->map_y_loc = y;
+		PS.map_x_orig = x;
+		PS.map_y_loc = y;
 		if (n == 4) {
-		    PS->map_width = w;
-		    PS->map_height = h;
+		    PS.map_width = w;
+		    PS.map_height = h;
 		}
 	    }
 	    else {
@@ -122,7 +135,7 @@
 	    char colorbuf[100];
 	    char catsbuf[100];
 
-	    if (PS->cell_fd < 0)
+	    if (PS.cell_fd < 0)
 		error(key, data, _("no raster map selected yet"));
 
 	    if (sscanf(data, "%s %[^\n]", catsbuf, colorbuf) == 2) {
@@ -131,11 +144,11 @@
 		    error(key, colorbuf, _("illegal color request")); 
 
 		if (strncmp(catsbuf, "null", 4) == 0) {
-		    G_set_null_value_color(r, g, b, &(PS->colors));
+		    G_set_null_value_color(r, g, b, &(PS.colors));
 		    continue;
 		}
 		if (strncmp(catsbuf, "default", 7) == 0) {
-		    G_set_default_color(r, g, b, &(PS->colors));
+		    G_set_default_color(r, g, b, &(PS.colors));
 		    continue;
 		}
 		if ((count = parse_val_list(catsbuf, &val_list)) < 0)
@@ -145,7 +158,7 @@
 		    dmin = val_list[i];
 		    dmax = val_list[i + 1];
 		    G_add_d_raster_color_rule(&dmin, r, g, b, &dmax,
-					      r, g, b, &(PS->colors));
+					      r, g, b, &(PS.colors));
 		}
 		G_free(val_list);
 	    }
@@ -153,21 +166,21 @@
 	}
 
 	if (KEY("colortable")) {
-	    PS->do_colortable = 0;
+	    PS.do_colortable = 0;
 	    /*
-	       if (PS->cell_fd < 0)
+	       if (PS.cell_fd < 0)
 	       error(key, data, "no raster map selected yet");
 	       else
 	     */
-	    PS->do_colortable = yesno(key, data);
-	    if (PS->do_colortable)
+	    PS.do_colortable = yesno(key, data);
+	    if (PS.do_colortable)
 		read_colortable();
 	    continue;
 	}
 
 	if (KEY("border")) {
-	    PS->do_border = yesno(key, data);
-	    if (PS->do_border)
+	    PS.do_border = yesno(key, data);
+	    if (PS.do_border)
 		read_border();
 	    continue;
 	}
@@ -178,11 +191,11 @@
 		      _("scalebar is not appropriate for this projection"));
 		gobble_input();
 	    }
-	    PS->do_scalebar = 1;
-	    if (sscanf(data, "%s", sb->type) != 1)
-		strcpy(sb->type, "f");	/* default to fancy scalebar */
+	    PS.do_scalebar = 1;
+	    if (sscanf(data, "%s", sb.type) != 1)
+		strcpy(sb.type, "f");	/* default to fancy scalebar */
 	    read_scalebar();
-	    if (sb->length <= 0.) {
+	    if (sb.length <= 0.) {
 		error(key, data, _("Bad scalebar length"));
 		gobble_input();
 	    }
@@ -285,9 +298,9 @@
 	    if (!can_reset_scale)
 		continue;
 	    if (check_scale(data))
-		strcpy(PS->scaletext, data);
+		strcpy(PS.scaletext, data);
 	    else {
-		PS->scaletext[0] = 0;
+		PS.scaletext[0] = 0;
 		error(key, data, _("illegal scale request"));
 	    }
 	    continue;
@@ -301,24 +314,24 @@
 
 	if (KEY("header")) {
 	    read_header();
-	    PS->do_header = 1;
+	    PS.do_header = 1;
 	    continue;
 	}
 
 	if (KEY("mapinfo")) {
 	    read_info();
-	    *do_mapinfo = 1;
+	    do_mapinfo = 1;
 	    continue;
 	}
 
 	if (KEY("vlegend")) {
 	    read_vlegend();
-	    *do_vlegend = 1;
+	    do_vlegend = 1;
 	    continue;
 	}
 
 	if (KEY("outline")) {
-	    if (PS->cell_fd < 0) {
+	    if (PS.cell_fd < 0) {
 		error(key, data, _("no raster map selected yet"));
 		gobble_input();
 	    }
@@ -336,15 +349,15 @@
 	if (KEY("greyrast") || KEY("grayrast")) {
 	    if (scan_gis("cell", "raster", key, data, name, mapset, 0))
 		read_cell(name, mapset);
-	    PS->grey = 1;
+	    PS.grey = 1;
 	    continue;
 	}
 
 	if (KEY("group")) {
 	    G_strip(data);
 	    if (I_find_group(data)) {
-		grp->group_name = G_store(data);
-		grp->do_group = 1;
+		grp.group_name = G_store(data);
+		grp.do_group = 1;
 		read_group();
 	    }
 	    else
@@ -354,7 +367,7 @@
 
 	if (KEY("rgb")) {
 	    G_strip(data);
-	    grp->do_group = 1;
+	    grp.do_group = 1;
 	    read_rgb(key, data);
 	    continue;
 	}
@@ -385,11 +398,11 @@
 	}
 
 	if (KEY("grid")) {
-	    PS->grid = -1;
-	    PS->grid_numbers = 0;
-	    sscanf(data, "%d", &(PS->grid));
-	    if (PS->grid < 0) {
-		PS->grid = 0;
+	    PS.grid = -1;
+	    PS.grid_numbers = 0;
+	    sscanf(data, "%d", &(PS.grid));
+	    if (PS.grid < 0) {
+		PS.grid = 0;
 		error(key, data, _("illegal grid spacing"));
 		gobble_input();
 	    }
@@ -406,11 +419,11 @@
 	    }
 	    /*          if (G_projection() == PROJECTION_LL)
 	       G_message(_("geogrid referenced to [???] ellipsoid"));  */
-	    PS->geogrid = -1.;
-	    PS->geogrid_numbers = 0;
-	    sscanf(data, "%d %s", &(PS->geogrid), PS->geogridunit);
-	    if (PS->geogrid < 0) {
-		PS->geogrid = 0;
+	    PS.geogrid = -1.;
+	    PS.geogrid_numbers = 0;
+	    sscanf(data, "%d %s", &(PS.geogrid), PS.geogridunit);
+	    if (PS.geogrid < 0) {
+		PS.geogrid = 0;
 		error(key, data, _("illegal geo-grid spacing"));
 		gobble_input();
 	    }
@@ -420,11 +433,11 @@
 	}
 
 	if (KEY("psfile")) {
-	    if (PS->num_psfiles >= MAX_PSFILES)
+	    if (PS.num_psfiles >= MAX_PSFILES)
 		continue;
 	    G_strip(data);
-	    PS->psfiles[PS->num_psfiles] = G_store(data);
-	    PS->num_psfiles++;
+	    PS.psfiles[PS.num_psfiles] = G_store(data);
+	    PS.num_psfiles++;
 	    continue;
 	}
 
@@ -433,10 +446,10 @@
 
 	    ret = G_str_to_color(data, &r, &g, &b);
 	    if (ret == 1) {
-		PS->mask_r = r / 255.0;
-		PS->mask_g = g / 255.0;
-		PS->mask_b = b / 255.0;
-		PS->mask_color = 1;
+		PS.mask_r = r / 255.0;
+		PS.mask_g = g / 255.0;
+		PS.mask_b = b / 255.0;
+		PS.mask_color = 1;
 		continue;
 	    }
 	    else if (ret == 2) {	/* none */



More information about the grass-commit mailing list