[PATCH 2/3] Config file fixes.

Richard Kettlewell rjk at terraraq.org.uk
Sat Jul 23 10:32:19 BST 2011


* Reject integers in excess of 2^32-1 (rather than reducing them mod
  2^32).
* ptree_dump():
  - Remove a magic number.
  - More realistic recursion limit.
* Various bits of type hygeine.

Signed-off-by: Richard Kettlewell <rjk at terraraq.org.uk>
---
 conffile.c          |    8 ++++----
 conffile.fl         |   21 +++++++++++++++++++--
 conffile_internal.h |    2 ++
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/conffile.c b/conffile.c
index 5cce211..490d5c1 100644
--- a/conffile.c
+++ b/conffile.c
@@ -154,7 +154,7 @@ static void ptree_mangle(struct p_node *t)
 
 #ifdef DUMP_PARSE_TREE
 /* Convert a node type to a string, for parse tree dump */
-static string_t ntype(uint32_t type)
+static const char *ntype(uint32_t type)
 {
     switch(type) {
     case T_STRING:     return "T_STRING";
@@ -179,14 +179,14 @@ static void ptree_indent(int amount)
     for (i=0; i<amount; i++) printf("  . ");
 }
 
-static void ptree_dump(struct p_node *n, uint32_t d)
+static void ptree_dump(struct p_node *n, int d)
 {
     if (!n) {
 	printf("NULL\n");
 	return;
     }
     
-    if (n->type<10) {
+    if (T_IS_PRIMITIVE(n->type)) {
 	switch(n->type) {
 	case T_STRING: printf("T_STRING: \"%s\" (%s line %d)\n",
 			      n->data.string,n->loc.file,n->loc.line); break;
@@ -197,7 +197,7 @@ static void ptree_dump(struct p_node *n, uint32_t d)
 	default:       printf("**unknown primitive type**\n"); break;
 	}
     } else {
-	assert(d<INT_MAX);
+	assert(d<10000);
 	printf("%s: (%s line %d)\n",ntype(n->type),n->loc.file,n->loc.line);
 	ptree_indent(d);
 	printf("  |-");	ptree_dump(n->l, d+1);
diff --git a/conffile.fl b/conffile.fl
index 2cfa21b..7228c9e 100644
--- a/conffile.fl
+++ b/conffile.fl
@@ -29,7 +29,7 @@ do{									\
 #define MAX_INCLUDE_DEPTH 10
 struct include_stack_item {
 	YY_BUFFER_STATE bst;
-	uint32_t lineno;
+	int lineno;
 	cstring_t file;
 };
 struct include_stack_item include_stack[MAX_INCLUDE_DEPTH];
@@ -71,8 +71,25 @@ static struct p_node *stringnode(string_t string)
 static struct p_node *numnode(string_t number)
 {
 	struct p_node *r;
+	unsigned long n;
 	r=leafnode(T_NUMBER);
-	r->data.number=atoi(number);
+	errno = 0;
+	n = strtoul(number, NULL, 10);
+	/* The caller is expected to only give us [0-9]+,
+	 * so we skip some of the usual syntax checking. */
+	r->data.number=n;
+	/* Give a consistent error message for any kind of
+	 * out-of-range condition */
+	if(errno == ERANGE || n != r->data.number) {
+	    Message(M_FATAL,"config file %s line %d: '%s' is too big\n",
+		    config_file, config_lineno, number);
+	    exit(1);
+	}
+	if(errno) {
+	    Message(M_FATAL,"config file %s line %d: '%s': %s\n",
+		    config_file, config_lineno, number, strerror(errno));
+	    exit(1);
+	}
 	return r;
 }
 
diff --git a/conffile_internal.h b/conffile_internal.h
index 073ab26..44d0ac7 100644
--- a/conffile_internal.h
+++ b/conffile_internal.h
@@ -23,6 +23,8 @@ typedef cstring_t atom_t;
 #define T_ALIST      17
 #define T_ERROR      20
 
+#define T_IS_PRIMITIVE(NTYPE) ((NTYPE) < T_ASSIGNMENT)
+
 struct p_node {
     uint32_t type;
     struct cloc loc;
-- 
1.6.4.2




More information about the sgo-software-discuss mailing list