Fixed config parser bug

Sat, 25 Feb 2012 12:43:26 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 25 Feb 2012 12:43:26 +0100
changeset 25
5dee29c7c530
parent 24
1a7853a4257e
child 26
37ff8bf54b89

Fixed config parser bug

src/server/Makefile file | annotate | diff | comparison | revisions
src/server/config/Makefile file | annotate | diff | comparison | revisions
src/server/config/conf.c file | annotate | diff | comparison | revisions
src/server/config/conf.h file | annotate | diff | comparison | revisions
src/server/config/initconf.c file | annotate | diff | comparison | revisions
src/server/config/objconf.c file | annotate | diff | comparison | revisions
src/server/daemon/config.c file | annotate | diff | comparison | revisions
src/server/daemon/configmanager.c file | annotate | diff | comparison | revisions
src/server/daemon/httplistener.c file | annotate | diff | comparison | revisions
src/server/daemon/httpparser.c file | annotate | diff | comparison | revisions
src/server/daemon/main.c file | annotate | diff | comparison | revisions
src/server/ucx/Makefile file | annotate | diff | comparison | revisions
src/server/ucx/mempool.c file | annotate | diff | comparison | revisions
src/server/ucx/string.c file | annotate | diff | comparison | revisions
src/server/ucx/string.h file | annotate | diff | comparison | revisions
src/server/util/io.c file | annotate | diff | comparison | revisions
src/server/webdav/saxhandler.cpp file | annotate | diff | comparison | revisions
--- a/src/server/Makefile	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/Makefile	Sat Feb 25 12:43:26 2012 +0100
@@ -30,7 +30,8 @@
 
 CFLAGS  =
 
-LDFLAGS = -L/usr/lib/mps -R/usr/lib/mps -lplds4 -lplc4 -lnspr4 -lpthread -ldl -lposix4  -lsocket -lnsl -lgen -lm -lsendfile -lxerces-c -pg
+# LDFLAGS += pg
+LDFLAGS = -L/usr/lib/mps -R/usr/lib/mps -lplds4 -lplc4 -lnspr4 -lpthread -ldl -lposix4  -lsocket -lnsl -lgen -lm -lsendfile -lxerces-c
 
 OBJ_DIR = $(BUILD_ROOT)build/
 
--- a/src/server/config/Makefile	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/config/Makefile	Sat Feb 25 12:43:26 2012 +0100
@@ -27,8 +27,8 @@
 #
 
 $(CONF_OBJPRE)%.o: config/%.c
-	cc -o $@ -c $(CFLAGS) $<
+	cc -o $@ -c -g $(CFLAGS) $<
 
 $(CONF_OBJPRE)%.o: config/%.cpp
-	CC -o $@ -c $(CFLAGS) $<
+	CC -o $@ -c -g $(CFLAGS) $<
 	
--- a/src/server/config/conf.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/config/conf.c	Sat Feb 25 12:43:26 2012 +0100
@@ -40,13 +40,15 @@
     mline.length = 0;
     ConfigLine *start_line;
     ConfigLine *end_line;
-
+    
     // read file
     sstr_t l;
     while((l = cfg_readln(in)).ptr != NULL) {
+        void *org_ptr = l.ptr;
+        
         // put the line to the list
         ConfigLine *line = OBJ_NEW(parser->mp, ConfigLine);
-        line->line = sstrdub(l);
+        line->line = sstrdup_mp(parser->mp, l); // TODO: check for 0-len str
         line->object = NULL;
         line->type = LINE_OTHER;
         parser->lines = ucx_dlist_append(parser->lines, line);
@@ -59,7 +61,10 @@
             // check for multi line
             if(mline.ptr != NULL) {
                 // concate lines
-                char *ptr = malloc(mline.length + l.length + 1);
+                char *ptr = ucx_mempool_malloc(
+                        parser->mp,
+                        mline.length + l.length + 1);
+                
                 memcpy(ptr, mline.ptr, mline.length);
                 memcpy(ptr + mline.length - 1, l.ptr, l.length);
                 mline.length += l.length;
@@ -73,7 +78,7 @@
             }
             if(l.ptr[l.length - 1] == '\\') {
                 if(mline.ptr == NULL) {
-                    mline = sstrdub(l);
+                    mline = sstrdup_mp(parser->mp, l);
                     start_line = line;
                 }
             } else {
@@ -88,7 +93,7 @@
                 } else {
                     ll = mline;
                 }
-
+                
                 // parse
                 int r = parser->parse(parser, start_line, end_line, ll);
 
@@ -106,6 +111,8 @@
                 }
             }
         }
+        
+        free(org_ptr);
     }
 
     return 0;
@@ -136,7 +143,7 @@
         }
 
         sstr_t line = sstr(buf);
-        return line;
+        return sstrdub(line);
     }
 
     sstr_t s;
@@ -276,7 +283,7 @@
 
     // create directive object
     ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective);
-    directive->directive_type = sstrdub(name);
+    directive->directive_type = sstrdup_mp(mp, name);
     directive->type_num = cfg_get_directive_type_num(name);
     directive->condition = NULL; // set later by main parsing function
     directive->param = NULL;
@@ -296,15 +303,10 @@
 
         // create param object
         ConfigParam *param = OBJ_NEW(mp, ConfigParam);
-        /*
-         * TODO:
-         * Wenn man sstrdub_mp statt sstrdub nimmt, wird der Inhalt von pname
-         * verunstaltet. Warum?
-         */
-        param->name = sstrdub(pname);
+        param->name = sstrdup_mp(mp, pname);
 
         if(pvalue.length > 0) {
-            param->value = sstrdub(pvalue);
+            param->value = sstrdup_mp(mp, pvalue);
         } else {
             param->value.ptr = NULL;
             param->value.length = 0;
@@ -370,7 +372,7 @@
         // this line is to short to be correct
         return LINE_ERROR;
     }
-
+    
     if(line.ptr[0] == '<') {
         // start or end tag
         // TODO: check for space between '<' and '/'
@@ -452,7 +454,7 @@
 
     // create tag object
     ConfigTag *tag = OBJ_NEW(mp, ConfigTag);
-    tag->name = sstrdub_mp(mp, name);
+    tag->name = sstrdup_mp(mp, name);
     tag->param = NULL;
 
     // parse parameters
@@ -474,9 +476,9 @@
 
         // create param object
         ConfigParam *param = OBJ_NEW(mp, ConfigParam);
-        param->name = sstrdub_mp(mp, pname);
+        param->name = sstrdup_mp(mp, pname);
         if(pvalue.length > 0) {
-            param->value = sstrdub_mp(mp, pvalue);
+            param->value = sstrdup_mp(mp, pvalue);
         } else {
             param->value.ptr = NULL;
             param->value.length = 0;
--- a/src/server/config/conf.h	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/config/conf.h	Sat Feb 25 12:43:26 2012 +0100
@@ -41,7 +41,7 @@
 #ifdef	__cplusplus
 extern "C" {
 #endif
-
+   
 // mempool malloc macro
 #define OBJ_NEW(pool, type) (type*)ucx_mempool_malloc(pool, sizeof(type))
 #define OBJ_NEW_N(pool, type) (type*)ucx_mempool_calloc(pool, 1, sizeof(type))
--- a/src/server/config/initconf.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/config/initconf.c	Sat Feb 25 12:43:26 2012 +0100
@@ -37,10 +37,11 @@
     if(in == NULL) {
         return NULL;
     }
-
+    
     InitConfig *conf = malloc(sizeof(InitConfig));
     conf->parser.parse = initconf_parse;
     conf->file = file;
+    conf->directives = NULL;
 
     int r = cfg_parse_basic_file((ConfigParser*)conf, in);
     if(r != 0) {
@@ -64,7 +65,7 @@
 
 int initconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
     InitConfig *conf = p;
-
+    
     // parse directive
     ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp);
     if(d == NULL) {
@@ -76,7 +77,7 @@
     if(d->type_num == 6) {
         conf->directives = ucx_dlist_append(conf->directives, d);
     } else {
-        fprintf(stderr, "Warning: Non Init directive in init.conf");
+        fprintf(stderr, "Warning: Non Init directive in init.conf\n");
     }
 
     return 0;
--- a/src/server/config/objconf.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/config/objconf.c	Sat Feb 25 12:43:26 2012 +0100
@@ -49,6 +49,8 @@
     conf->file = file;
     conf->conditions = NULL;
     conf->levels = NULL;
+    conf->objects = NULL;
+    conf->lines = NULL;
 
     int r = cfg_parse_basic_file((ConfigParser*)conf, in);
     if(r != 0) {
--- a/src/server/daemon/config.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/daemon/config.c	Sat Feb 25 12:43:26 2012 +0100
@@ -275,6 +275,7 @@
         return -1;
     }
     file->last_modified = s.st_mtim.tv_sec;
+    return 0;
 }
 
 HTTPObjectConfig* load_obj_conf(char *file) {
@@ -315,7 +316,7 @@
         httpd_object *obj = object_new(name);
         obj->path = NULL;
 
-        conf->objects[i] = obj;
+        conf->objects[i] = obj; // TODO: beyond array bounds write
 
         /* add directives */
         for(int i=0;i<6;i++) {
--- a/src/server/daemon/configmanager.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/daemon/configmanager.c	Sat Feb 25 12:43:26 2012 +0100
@@ -50,7 +50,7 @@
 }
 
 ConfigFile* cfgmgr_get_file(sstr_t name) {
-    ucx_map_sstr_get(config_files, name);
+    return ucx_map_sstr_get(config_files, name);
 }
 
 int cfgmgr_load_config() {
--- a/src/server/daemon/httplistener.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/daemon/httplistener.c	Sat Feb 25 12:43:26 2012 +0100
@@ -186,5 +186,10 @@
                 conn);
 
         /* ready for new connection */
+        if(0) {
+            break;
+        }
     }
+    
+    return NULL;
 }
--- a/src/server/daemon/httpparser.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/daemon/httpparser.c	Sat Feb 25 12:43:26 2012 +0100
@@ -67,6 +67,7 @@
             return 0;
         }
     }
+    return -1;
 }
 
 int get_start_line(HttpParser *parser) {
--- a/src/server/daemon/main.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/daemon/main.c	Sat Feb 25 12:43:26 2012 +0100
@@ -81,13 +81,15 @@
     }
 
     fclose(log_out);
+    
+    return NULL;
 }
 
 int main(int argc, char **argv) {
     //test();
 
     /* if the -c parameter is specified, we don't create a daemon */
-    int d = 1;
+    int d = 0;
     for(int i=0;i<argc;i++) {
         char *p = argv[i];
         if(p[0] == '-' && p[1] == 'c') {
--- a/src/server/ucx/Makefile	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/ucx/Makefile	Sat Feb 25 12:43:26 2012 +0100
@@ -27,5 +27,5 @@
 #
 
 $(UCX_OBJPRE)%.o: ucx/%.c
-	cc -o $@ -c $(CFLAGS) $<
+	cc -o $@ -c -g $(CFLAGS) $<
 	
--- a/src/server/ucx/mempool.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/ucx/mempool.c	Sat Feb 25 12:43:26 2012 +0100
@@ -50,7 +50,7 @@
     }
 }
 
-void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
+void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {   
     ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
     if (mem == NULL) return NULL;
 
@@ -65,7 +65,7 @@
     return &mem->c;
 }
 
-void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
+void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {  
     void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
     if(ptr == NULL) {
         return NULL;
@@ -74,7 +74,7 @@
     return ptr;
 }
 
-void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
+void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 
     void *mem = ((char*)ptr) - sizeof(ucx_destructor);
     char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
     if (newm == NULL) return NULL;
--- a/src/server/ucx/string.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/ucx/string.c	Sat Feb 25 12:43:26 2012 +0100
@@ -85,7 +85,7 @@
 }
 
 int sstrcmp(sstr_t s1, sstr_t s2) {
-    return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
+    return memcmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
 }
 
 sstr_t sstrdub(sstr_t s) {
@@ -95,7 +95,7 @@
     newstring.ptr[newstring.length] = 0;
 
     memcpy(newstring.ptr, s.ptr, s.length);
-
+    
     return newstring;
 }
 
@@ -125,17 +125,14 @@
     return newstr;
 }
 
-sstr_t sstrdub_mp(UcxMempool *mp, sstr_t s) {
+sstr_t sstrdup_mp(UcxMempool *mp, sstr_t s) {
     sstr_t newstring;
     newstring.ptr = ucx_mempool_malloc(mp, s.length + 1);
     newstring.length = s.length;
     newstring.ptr[newstring.length] = 0;
 
     /* TODO: sometimes memcpy and/or memmove destroy the source */
-    //memcpy(newstring.ptr, s.ptr, s.length);
-    for(int i=0;i<s.length;i++) {
-        newstring.ptr[i] = s.ptr[i];
-    }
+    memcpy(newstring.ptr, s.ptr, s.length);
 
     return newstring;
 }
--- a/src/server/ucx/string.h	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/ucx/string.h	Sat Feb 25 12:43:26 2012 +0100
@@ -74,7 +74,7 @@
 sstr_t sstrdub(sstr_t s);
 
 sstr_t sstrtrim(sstr_t string);
-sstr_t sstrdub_mp(UcxMempool *mp, sstr_t s);
+sstr_t sstrdup_mp(UcxMempool *mp, sstr_t s);
 
 #ifdef	__cplusplus
 }
--- a/src/server/util/io.c	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/util/io.c	Sat Feb 25 12:43:26 2012 +0100
@@ -66,6 +66,7 @@
     st->fd = fd;
     st->max_read = 0;
     st->rd = 0;
+    return (IOStream*)st;
 }
 
 ssize_t net_stream_write(IOStream *st, void *buf, size_t nbytes) {
--- a/src/server/webdav/saxhandler.cpp	Thu Feb 23 13:10:04 2012 +0100
+++ b/src/server/webdav/saxhandler.cpp	Sat Feb 25 12:43:26 2012 +0100
@@ -36,7 +36,7 @@
 
 #include "saxhandler.h"
 
-using std;
+using namespace std;
 
 PropfindHandler::PropfindHandler(PropfindRequest *rq, pool_handle_t *p) {
     davrq = rq;

mercurial