]> pilppa.com Git - libplp.git/commitdiff
set_config_value(): fix memory leak
authorMika Laitio <lamikr@pilppa.org>
Sun, 23 Jan 2011 22:16:04 +0000 (00:16 +0200)
committerMika Laitio <lamikr@pilppa.org>
Sun, 23 Jan 2011 22:16:04 +0000 (00:16 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/config.c

index 0b0994439e1db484683273d2ea21cecda3517a4b..74bfd8bd15a07fa3605570a1cd588da46eb7f99e 100644 (file)
@@ -22,6 +22,7 @@ bool set_config_value(const char *conf_dir_name,
        char                    *fname;
        int                     b_count;
        bool                    save;
+       struct uci_element      *e;
        bool                    ret_val;
 
        ret_val = false;
@@ -39,57 +40,60 @@ bool set_config_value(const char *conf_dir_name,
                        strncat(fname, "/", 1);
                        strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
                        ctx     = uci_alloc_context();
-                       sct     = NULL;
-                       uci_set_confdir(ctx, conf_dir_name);
-                       if (access(fname, F_OK) != 0) {
-                               FILE *fp;
-
-                               fp      = fopen(fname, "w+");
-                               fclose(fp);
-                       }
-                       err_flg = uci_load(ctx, fname, &pkg);
-                       struct uci_element *e;
-                       uci_foreach_element(&pkg->sections, e) {
-                               tmp_sct = uci_to_section(e);
-                               if (strcmp(tmp_sct->type, section_type) == 0) {
-                                       sct     = tmp_sct;
-                                       break;
+                       if (ctx != NULL) {
+                               sct     = NULL;
+                               uci_set_confdir(ctx, conf_dir_name);
+                               if (access(fname, W_OK) != 0) {
+                                       if (access(fname, F_OK) != 0) {
+                                               FILE *fp;
+                                               fp      = fopen(fname, "w+");
+                                               fclose(fp);
+                                       }
                                }
-                       }
-                       //sct   = uci_lookup_section(ctx, pkg, "service");
-                       if (sct != NULL) {
-                               log_debug("section found\n");
-                       }
-                       else {
-                               log_debug("adding new section\n");
-                               err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
-                               log_debug("err_flg: %d, section name: %s\n", err_flg, sct->e.name);
-                       }
-                       opt     = uci_lookup_option(ctx, sct, key);
-                       if (opt != NULL) {
-                               struct uci_ptr ptr;
-                               memset(&ptr, 0, sizeof(ptr));
-                               ptr.package     = pkg->e.name;
-                               ptr.section     = sct->e.name;
-                               ptr.option      = key;
+                               err_flg = uci_load(ctx, fname, &pkg);
+                               uci_foreach_element(&pkg->sections, e) {
+                                       tmp_sct = uci_to_section(e);
+                                       if (strcmp(tmp_sct->type, section_type) == 0) {
+                                               sct     = tmp_sct;
+                                               break;
+                                       }
+                               }
+                               //sct   = uci_lookup_section(ctx, pkg, "service");
+                               if (sct != NULL) {
+                                       log_debug("section found\n");
+                               }
+                               else {
+                                       log_debug("adding new section\n");
+                                       err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
+                                       log_debug("err_flg: %d, section name: %s\n", err_flg, sct->e.name);
+                               }
+                               opt     = uci_lookup_option(ctx, sct, key);
+                               if (opt != NULL) {
+                                       struct uci_ptr ptr;
+                                       memset(&ptr, 0, sizeof(ptr));
+                                       ptr.package     = pkg->e.name;
+                                       ptr.section     = sct->e.name;
+                                       ptr.option      = key;
 
-                               if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
-                                       log_debug("trying to change option value\n");
-                                       ptr.value       = strdup(value);
-                                       uci_set(ctx, &ptr);
-                                       save            = true;
+                                       if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
+                                               log_debug("trying to change option value\n");
+                                               ptr.value       = strdup(value);
+                                               uci_set(ctx, &ptr);
+                                               save            = true;
+                                       }
                                }
-                       }
-                       else {
-                               opt     = uci_alloc_option(sct, key, value);
-                               save    = true;
-                       }
-                       if (save == true) {
-                               log_debug("saving config file: %s\n", fname);
-                               uci_save(ctx, pkg);
+                               else {
+                                       opt     = uci_alloc_option(sct, key, value);
+                                       save    = true;
+                               }
+                               if (save == true) {
+                                       log_debug("saving config file: %s\n", fname);
+                                       uci_save(ctx, pkg);
+                               }
+                               uci_free_context(ctx);
+                               ret_val = true;
                        }
                        free(fname);
-                       ret_val = true;
                }
                else {
                        log_error("Could not change config value, out of memory");