Browse Source

libgis: Check that option names are valid
(merge https://trac.osgeo.org/grass/changeset/62105 from trunk)


git-svn-id: https://svn.osgeo.org/grass/grass/branches/releasebranch_7_0@62926 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 10 years ago
parent
commit
f464a6c5c0
1 changed files with 23 additions and 2 deletions
  1. 23 2
      lib/gis/parser.c

+ 23 - 2
lib/gis/parser.c

@@ -101,6 +101,7 @@ struct state *st = &state;
 /* local prototypes */
 static void set_flag(int);
 static int contains(const char *, int);
+static int valid_option_name(const char *);
 static int is_option(const char *);
 static void set_option(const char *);
 static void check_opts(void);
@@ -337,10 +338,13 @@ int G_parser(int argc, char **argv)
     /* Stash default answers */
 
     opt = &st->first_option;
-    while (opt) {
+    while (st->n_opts && opt) {
 	if (opt->required)
 	    st->has_required = 1;
 
+	if (!valid_option_name(opt->key))
+	    G_warning(_("BUG in option name, '%s' is not valid"), opt->key);
+
 	/* Parse options */
 	if (opt->options) {
 	    int cnt = 0;
@@ -658,7 +662,7 @@ char *G_recreate_command(void)
     }
 
     opt = &st->first_option;
-    while (opt) {
+    while (st->n_opts && opt) {
 	if (opt->answer && opt->answers && opt->answers[0]) {
 	    slen = strlen(opt->key) + strlen(opt->answers[0]) + 4;	/* +4 for: ' ' = " " */
 	    if (len + slen >= nalloced) {
@@ -874,6 +878,23 @@ static int contains(const char *s, int c)
     return FALSE;
 }
 
+static int valid_option_name(const char *string)
+{
+    int m = strlen(string);
+    int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");
+
+    if (!m)
+	return 0;
+
+    if (m != n)
+	return 0;
+
+    if (string[m-1] == '_')
+	return 0;
+
+    return 1;
+}
+
 static int is_option(const char *string)
 {
     int n = strspn(string, "abcdefghijklmnopqrstuvwxyz0123456789_");