chiark / gitweb /
Config file fixes.
authorRichard Kettlewell <rjk@terraraq.org.uk>
Mon, 11 Jul 2011 19:02:31 +0000 (20:02 +0100)
committerRichard Kettlewell <rjk@terraraq.org.uk>
Sat, 10 Dec 2011 12:15:53 +0000 (12:15 +0000)
* 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@terraraq.org.uk>
conffile.c
conffile.fl
conffile_internal.h

index 53c334bd6b0279625574242437c3002cc66ac5de..ede9c49d55eb5c92221525e41d1b9fc936f05477 100644 (file)
@@ -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);
index 2cfa21b10f96202635e8d33431802d136a6ca791..7228c9e8fa6cd672ea5a3ac480d8328fc161cede 100644 (file)
@@ -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;
 }
 
index 073ab26ba6d51b185ea757bff0d6e29fb6fd7bd0..44d0ac7d4ba7dcc864248e7805321fb4f9dc2be8 100644 (file)
@@ -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;