fix dav add-repo crash in case .dav/config.xml doesn't exist

Wed, 24 Jul 2024 23:45:31 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 24 Jul 2024 23:45:31 +0200
changeset 826
b6e9fd3f1951
parent 825
ca568c209dfd
child 827
d9928f11970f

fix dav add-repo crash in case .dav/config.xml doesn't exist

dav/config.c file | annotate | diff | comparison | revisions
libidav/config.c file | annotate | diff | comparison | revisions
libidav/config.h file | annotate | diff | comparison | revisions
--- a/dav/config.c	Tue Jul 23 14:40:20 2024 +0200
+++ b/dav/config.c	Wed Jul 24 23:45:31 2024 +0200
@@ -136,6 +136,7 @@
     if(stat(file, &s)) {
         switch(errno) {
             case ENOENT: {
+                davconfig = dav_config_new();
                 return 0;
             }
             default: {
--- a/libidav/config.c	Tue Jul 23 14:40:20 2024 +0200
+++ b/libidav/config.c	Wed Jul 24 23:45:31 2024 +0200
@@ -98,6 +98,20 @@
 }
 
 
+DavConfig* dav_config_new(void) {
+    CxMempool *cfg_mp = cxMempoolCreate(128, NULL);
+    DavConfig *config = cxMalloc(cfg_mp->allocator, sizeof(DavConfig));
+    memset(config, 0, sizeof(DavConfig));
+    config->mp = cfg_mp;
+    
+    xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
+    xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration");
+    xmlDocSetRootElement(doc, root);
+    config->doc = doc;
+    
+    return config;
+}
+
 DavConfig* dav_config_load(cxmutstr xmlfilecontent, int *error) {
     xmlDoc *doc = xmlReadMemory(xmlfilecontent.ptr, xmlfilecontent.length, NULL, NULL, 0);
     if(!doc) {
@@ -107,12 +121,9 @@
         return NULL;
     }
     
-    CxMempool *cfg_mp = cxMempoolCreate(128, NULL);
+    DavConfig *config = dav_config_new();
+    CxMempool *cfg_mp = config->mp;
     cxMempoolRegister(cfg_mp, doc, (cx_destructor_func)xmlFreeDoc);
-    DavConfig *config = cxMalloc(cfg_mp->allocator, sizeof(DavConfig));
-    memset(config, 0, sizeof(DavConfig));
-    config->mp = cfg_mp;
-    config->doc = doc;
     
     DavCfgRepository *repos_begin = NULL;
     DavCfgRepository *repos_end = NULL;
@@ -723,6 +734,9 @@
 
 
 DavCfgRepository* dav_config_get_repository(DavConfig *config, cxstring name) {
+    if(!config) {
+        return NULL;
+    }
     DavCfgRepository *repo = config->repositories;
     while(repo) {
         if(!cx_strcmp(cx_strcast(repo->name.value), name)) {
--- a/libidav/config.h	Tue Jul 23 14:40:20 2024 +0200
+++ b/libidav/config.h	Wed Jul 24 23:45:31 2024 +0200
@@ -160,6 +160,8 @@
     DAV_CONFIG_ERROR_XML = 0
 };
 
+DavConfig* dav_config_new(void);
+
 DavConfig* dav_config_load(cxmutstr xmlfilecontent, int *error);
 
 void dav_config_free(DavConfig *config);

mercurial