fix load_config stores wrong xml doc pointer in the config object

Fri, 13 Sep 2024 18:21:04 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 13 Sep 2024 18:21:04 +0200
changeset 827
d9928f11970f
parent 826
b6e9fd3f1951
child 828
e082e5d7d75b
child 830
349106ea431c

fix load_config stores wrong xml doc pointer in the config object

dav/config.c file | annotate | diff | comparison | revisions
dav/main.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	Wed Jul 24 23:45:31 2024 +0200
+++ b/dav/config.c	Fri Sep 13 18:21:04 2024 +0200
@@ -136,7 +136,7 @@
     if(stat(file, &s)) {
         switch(errno) {
             case ENOENT: {
-                davconfig = dav_config_new();
+                davconfig = dav_config_new(NULL);
                 return 0;
             }
             default: {
--- a/dav/main.c	Wed Jul 24 23:45:31 2024 +0200
+++ b/dav/main.c	Fri Sep 13 18:21:04 2024 +0200
@@ -2449,7 +2449,10 @@
     
     repo->name.value = cx_strdup_a(a, cx_str(name));
     repo->url.value = cx_strdup_a(a, cx_str(url));
-    dav_repository_set_auth(config, repo, cx_str(user), cx_str(password));
+    
+    cxstring user_s = user ? cx_str(user) : cx_strn(NULL, 0);
+    cxstring password_s = password ? cx_str(password) : cx_strn(NULL, 0);
+    dav_repository_set_auth(config, repo, user_s, password_s);
     
     dav_config_add_repository(config, repo);
     
--- a/libidav/config.c	Wed Jul 24 23:45:31 2024 +0200
+++ b/libidav/config.c	Fri Sep 13 18:21:04 2024 +0200
@@ -98,15 +98,17 @@
 }
 
 
-DavConfig* dav_config_new(void) {
+DavConfig* dav_config_new(xmlDoc *doc) {
     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);
+    if(!doc) {
+        doc = xmlNewDoc(BAD_CAST "1.0");
+        xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration");
+        xmlDocSetRootElement(doc, root);
+    }
     config->doc = doc;
     
     return config;
@@ -121,7 +123,7 @@
         return NULL;
     }
     
-    DavConfig *config = dav_config_new();
+    DavConfig *config = dav_config_new(doc);
     CxMempool *cfg_mp = config->mp;
     cxMempoolRegister(cfg_mp, doc, (cx_destructor_func)xmlFreeDoc);
     
@@ -484,10 +486,14 @@
 
 void dav_repository_set_auth(DavConfig *config, DavCfgRepository *repo, cxstring user, cxstring password) {
     const CxAllocator *a = config->mp->allocator;
-    repo->user.value = cx_strdup_a(a, user);
-    char *pwenc = util_base64encode(password.ptr, password.length);
-    repo->password.value = cx_strdup_a(a, cx_str(pwenc));
-    free(pwenc);
+    if(user.length > 0) {
+        repo->user.value = cx_strdup_a(a, user);
+    }
+    if(password.length > 0) {
+        char *pwenc = util_base64encode(password.ptr, password.length);
+        repo->password.value = cx_strdup_a(a, cx_str(pwenc));
+        free(pwenc);
+    }
 }
 
 cxmutstr dav_repository_get_decodedpassword(DavCfgRepository *repo) {
--- a/libidav/config.h	Wed Jul 24 23:45:31 2024 +0200
+++ b/libidav/config.h	Fri Sep 13 18:21:04 2024 +0200
@@ -160,7 +160,7 @@
     DAV_CONFIG_ERROR_XML = 0
 };
 
-DavConfig* dav_config_new(void);
+DavConfig* dav_config_new(xmlDoc *doc);
 
 DavConfig* dav_config_load(cxmutstr xmlfilecontent, int *error);
 

mercurial