fixed many davql exec memory leaks

Sat, 03 Oct 2015 19:55:43 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 03 Oct 2015 19:55:43 +0200
changeset 148
f21ee22170bf
parent 147
458a8dc68048
child 149
509e9e1cbdcc

fixed many davql exec memory leaks

libidav/davqlexec.c file | annotate | diff | comparison | revisions
libidav/methods.c file | annotate | diff | comparison | revisions
libidav/methods.h file | annotate | diff | comparison | revisions
libidav/utils.c file | annotate | diff | comparison | revisions
--- a/libidav/davqlexec.c	Sat Oct 03 18:11:07 2015 +0200
+++ b/libidav/davqlexec.c	Sat Oct 03 19:55:43 2015 +0200
@@ -124,7 +124,9 @@
     }
     *error = DAVQL_OK;
     
-    return sstrdup_a(a, sstrn(buf->space, buf->size));
+    sstr_t ret = sstrdup_a(a, sstrn(buf->space, buf->size));
+    ucx_buffer_free(buf);
+    return ret;
 }
 
 static int fl_add_properties(DavSession *sn, UcxMempool *mp, UcxMap *map, DavQLExpression *expression) {
@@ -177,10 +179,11 @@
             return create_allprop_propfind_request();
         } else if(!sstrcmp(field->name, S("-"))) {
             ucx_map_free(properties);
-            return create_basic_propfind_request();
+            return create_propfind_request(sn, NULL);
         } else {
             if(fl_add_properties(sn, mp, properties, field->expr)) {
                 // TODO: set error
+                ucx_map_free(properties);
                 return NULL;
             }
         }
@@ -196,6 +199,7 @@
     
     UcxBuffer *reqbuf = create_propfind_request(sn, list);
     ucx_list_free(list);
+    ucx_map_free(properties);
     return reqbuf;
 }
 
@@ -271,6 +275,7 @@
             if(str.ptr) {
                 UcxKey key = dav_property_key(field->ns, field->name);
                 ucx_map_put(new_properties, key, str.ptr);
+                free(key.data);
             }
         } else {
             // TODO: error
@@ -316,6 +321,7 @@
         ucx_mempool_destroy(mp);
         return result;
     }
+    ucx_mempool_reg_destr(mp, rqbuf, (ucx_destructor)ucx_buffer_free);
     
     // compile field list
     UcxList *cfieldlist = NULL;
@@ -332,6 +338,7 @@
                 // TODO: set error string
                 return result;
             }
+            ucx_mempool_reg_destr(mp, code, (ucx_destructor)ucx_buffer_free);
             DavCompiledField *cfield = ucx_mempool_malloc(
                     mp,
                     sizeof(DavCompiledField));
@@ -359,6 +366,7 @@
     sstr_t path = dav_format_string(mp->allocator, st->path, ap, &error);
     if(error) {
         // TODO: cleanup
+        ucx_mempool_destroy(mp);
         return result;
     }
     
@@ -366,9 +374,11 @@
     
     UcxBuffer *where = dav_compile_expr(sn->context, mp->allocator, st->where, ap);
     if(st->where && !where) {
+        // TODO: cleanup
         ucx_mempool_destroy(mp);
         return result;
     }
+    ucx_mempool_reg_destr(mp, where, (ucx_destructor)ucx_buffer_free);
     
     // compile order criterion
     UcxList *ordercr = NULL;
@@ -397,6 +407,7 @@
                 } else {
                     // error
                     // TODO: cleanup
+                    ucx_mempool_destroy(mp);
                     return result;
                 }
             } else if(dav_identifier2resprop(column->srctext, &resprop)) {
@@ -408,6 +419,7 @@
             } else {
                 // error
                 // TODO: cleanup
+                ucx_mempool_destroy(mp);
                 return result;
             }
             
@@ -418,6 +430,7 @@
         } else {
             // something is broken
             // TODO: cleanup
+            ucx_mempool_destroy(mp);
             return result;
         }
     }
@@ -433,6 +446,12 @@
     
     // reuseable response buffer
     UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
+    if(!rpbuf) {
+        // TODO: cleanup
+        ucx_mempool_destroy(mp);
+        return result;
+    }
+    ucx_mempool_reg_destr(mp, rpbuf, (ucx_destructor)ucx_buffer_free);
     
     result.result = selroot;
     result.status = 0;
@@ -461,6 +480,7 @@
                     // error
                     result.status = -1;
                     // TODO: free resources
+                    cleanup_response(&response);
                     break;
                 }
                 
@@ -473,6 +493,7 @@
                     
                     // add properties
                     add_properties(root, &response);
+                    cleanup_response(&response);
                     
                     if(root == selroot) {
                         // The current root is the root of the select query.
@@ -500,6 +521,7 @@
                             sn,
                             &response,
                             root->path);
+                    cleanup_response(&response);
                     // check where clause
                     DavQLStackObj where_result;
                     if(!dav_exec_expr(where, child, &where_result)) {
@@ -526,7 +548,7 @@
                     }
                 }    
             }
-            
+            destroy_propfind_parser(parser);
         } else  {
             dav_session_set_error(sn, ret, http_status);
             result.result = NULL;
--- a/libidav/methods.c	Sat Oct 03 18:11:07 2015 +0200
+++ b/libidav/methods.c	Sat Oct 03 19:55:43 2015 +0200
@@ -66,7 +66,7 @@
 }
 
 UcxBuffer* create_allprop_propfind_request() {
-    UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
+    UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOFREE);
     sstr_t s;
     
     s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
@@ -82,7 +82,7 @@
 }
 
 UcxBuffer* create_propfind_request(DavSession *sn, UcxList *properties) {
-    UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
+    UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
     sstr_t s;
     
     int add_crypto_name = 1;
@@ -192,7 +192,7 @@
 }
 
 UcxBuffer* create_basic_propfind_request() {
-    UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
+    UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
     sstr_t s;
     
     s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
@@ -254,6 +254,13 @@
     return NULL;
 }
 
+void destroy_propfind_parser(PropfindParser *parser) {
+    if(parser->document) {
+        xmlFreeDoc(parser->document);
+    }
+    free(parser);
+}
+
 int get_propfind_response(PropfindParser *parser, ResponseTag *result) {
     if(parser->current == NULL) {
         return 0;
@@ -265,6 +272,8 @@
     char *crypto_name = NULL; // name set by crypto-name property
     char *crypto_key = NULL;
     
+    result->properties = NULL;
+    
     xmlNode *node = parser->current->children;
     while(node) {
         if(node->type == XML_ELEMENT_NODE) {
@@ -352,6 +361,12 @@
     return 1;
 }
 
+void cleanup_response(ResponseTag *result) {
+    if(result) {
+        ucx_list_free(result->properties);
+    }
+}
+
 int hrefeq(DavSession *sn, char *href1, char *href2) {
     sstr_t href_s = sstr(util_url_decode(sn, href1));
     sstr_t href_r = sstr(util_url_decode(sn, href2));
@@ -664,7 +679,7 @@
 }
 
 UcxBuffer* create_proppatch_request(DavResourceData *data) {
-    UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
+    UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
     sstr_t s;
     
     UcxMap *namespaces = ucx_map_new(8);
@@ -771,7 +786,7 @@
 }
 
 UcxBuffer* create_crypto_proppatch_request(DavSession *sn, DavKey *key, char *name) {
-    UcxBuffer *buf = ucx_buffer_new(NULL, 512, 0);
+    UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
     sstr_t s;
     
     s = S("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
--- a/libidav/methods.h	Sat Oct 03 18:11:07 2015 +0200
+++ b/libidav/methods.h	Sat Oct 03 19:55:43 2015 +0200
@@ -75,7 +75,9 @@
 UcxBuffer* create_basic_propfind_request();
 
 PropfindParser* create_propfind_parser(UcxBuffer *response, char *url);
+void destroy_propfind_parser(PropfindParser *parser);
 int get_propfind_response(PropfindParser *parser, ResponseTag *result);
+void cleanup_response(ResponseTag *result);
 
 int hrefeq(DavSession *sn, char *href1, char *href2);
 DavResource* response2resource(DavSession *sn, ResponseTag *response, char *parent_path);
--- a/libidav/utils.c	Sat Oct 03 18:11:07 2015 +0200
+++ b/libidav/utils.c	Sat Oct 03 19:55:43 2015 +0200
@@ -453,14 +453,14 @@
 }
 
 void util_generate_key(DavKey *key, char *password) {
-    key->data = malloc(SHA256_DIGEST_LENGTH);
-    key->length = SHA256_DIGEST_LENGTH;
+    //key->data = malloc(SHA256_DIGEST_LENGTH);
+    //key->length = SHA256_DIGEST_LENGTH;
     key->type =  DAV_KEY_AES256;
     
-    SHA256_CTX sha256;
-    SHA256_Init(&sha256);
-    SHA256_Update(&sha256, password, strlen(password));
-    SHA256_Final(key->data, &sha256);
+    //SHA256_CTX sha256;
+    //SHA256_Init(&sha256);
+    //SHA256_Update(&sha256, password, strlen(password));
+    //SHA256_Final(key->data, &sha256);
 }
 
 char* util_key_input(DavContext *ctx, DavKey *key) {

mercurial