reduce a big memory leak
authorticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Thu, 26 Mar 2009 07:58:43 +0000 (07:58 +0000)
committerticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Thu, 26 Mar 2009 07:58:43 +0000 (07:58 +0000)
connecting deb_extract Null pointers

git-svn-id: http://opkg.googlecode.com/svn/trunk@206 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/opkg.c
libopkg/opkg_remove.c
libopkg/pkg.c
libopkg/pkg_extract.c
libopkg/str_list.c
libopkg/str_list.h

index 11b5ee79a4e9a6639d95df99e45ce480bcdeda5f..735fb58e681e0a675bdc50931674ee36918011f6 100644 (file)
@@ -1064,6 +1064,7 @@ int opkg_repository_accessibility_check(opkg_t *opkg)
     free(stmp);
 
     str_list_append(src, repo_ptr);
     free(stmp);
 
     str_list_append(src, repo_ptr);
+    free(repo_ptr);
     repositories++;
   }
   while (repositories > 0) 
     repositories++;
   }
   while (repositories > 0) 
index 0b913fd7cae083b118b8ba3d5bd97af9056c04f0..3886ccc6eeb40918c0d9d15a59de2a150fae5e97 100644 (file)
@@ -350,7 +350,7 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
          file_name = (char *)iter->data;
 
          if (file_is_dir(file_name)) {
          file_name = (char *)iter->data;
 
          if (file_is_dir(file_name)) {
-              str_list_append(&installed_dirs, strdup(file_name));
+              str_list_append(&installed_dirs, file_name);
               continue;
          }
 
               continue;
          }
 
index 6e0f3968f700a309601e8445b9c1a7cb11c9d2fc..1ab24e10826ae3d9c1e342fcb723f589ca21cc26 100644 (file)
@@ -1412,6 +1412,7 @@ str_list_t *pkg_get_installed_files(pkg_t *pkg)
               sprintf_alloc(&installed_file_name, "%s", file_name);
          }
          str_list_append(pkg->installed_files, installed_file_name);
               sprintf_alloc(&installed_file_name, "%s", file_name);
          }
          str_list_append(pkg->installed_files, installed_file_name);
+          free(installed_file_name);
          free(line);
      }
 
          free(line);
      }
 
@@ -1433,7 +1434,7 @@ int pkg_free_installed_files(pkg_t *pkg)
          return 0;
 
      if (pkg->installed_files) {
          return 0;
 
      if (pkg->installed_files) {
-         str_list_deinit(pkg->installed_files);
+         str_list_purge(pkg->installed_files);
      }
 
      pkg->installed_files = NULL;
      }
 
      pkg->installed_files = NULL;
index eae7746e176a3a00d22baf277bd1d4b0eb245ea4..0aae4ad578d6b3bb6ca4dd6b532d688c394f42ce 100644 (file)
@@ -37,7 +37,6 @@ int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
        return EINVAL;
     }
 
        return EINVAL;
     }
 
-    /* XXX: QUESTION: Is there a way to do this directly with deb_extract now? */
     fputs(buffer, stream);
     free(buffer);
 
     fputs(buffer, stream);
     free(buffer);
 
@@ -54,10 +53,11 @@ int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
                                                 const char *prefix)
 {
     char *dir_with_prefix;
                                                 const char *prefix)
 {
     char *dir_with_prefix;
+    char *buffer = NULL;
 
     sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
 
 
     sprintf_alloc(&dir_with_prefix, "%s/%s", dir, prefix);
 
-    deb_extract(pkg->local_filename, stderr,
+    buffer = deb_extract(pkg->local_filename, stderr,
                extract_control_tar_gz
                 | extract_all_to_fs| extract_preserve_date
                | extract_unconditional,
                extract_control_tar_gz
                 | extract_all_to_fs| extract_preserve_date
                | extract_unconditional,
@@ -65,23 +65,26 @@ int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
 
     free(dir_with_prefix);
 
 
     free(dir_with_prefix);
 
-    /* XXX: BUG: how do we know if deb_extract worked or not? This is
-       a defect in the current deb_extract from what I can tell.
-
-       Once this is fixed, audit all calls to deb_extract. */
+    if (buffer == NULL) {
+        return EINVAL;
+    }
+    free(buffer);
     return 0;
 }
 
 int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
 {
     return 0;
 }
 
 int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
 {
-    deb_extract(pkg->local_filename, stderr,
+    char *buffer = NULL;
+    buffer = deb_extract(pkg->local_filename, stderr,
                extract_data_tar_gz
                 | extract_all_to_fs| extract_preserve_date
                | extract_unconditional,
                dir, NULL);
 
                extract_data_tar_gz
                 | extract_all_to_fs| extract_preserve_date
                | extract_unconditional,
                dir, NULL);
 
-    /* BUG: How do we know if deb_extract worked or not? This is a
-       defect in the current deb_extract from what I can tell. */
+    if (buffer == NULL) {
+        return EINVAL;
+    }
+    free(buffer);
     return 0;
 }
 
     return 0;
 }
 
@@ -148,6 +151,7 @@ int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
 
 int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
 {
 
 int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
 {
+    char *buffer = NULL;
     /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the
        data file names with a '.' as the first character. I've taught
        opkg how to cope with the presence or absence of the '.', but
     /* XXX: DPKG_INCOMPATIBILITY: deb_extract will extract all of the
        data file names with a '.' as the first character. I've taught
        opkg how to cope with the presence or absence of the '.', but
@@ -160,11 +164,15 @@ int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
        If we wanted to, we could workaround the deb_extract behavior
        right here, by writing to a tmpfile, then munging things as we
        wrote to the actual stream. */
        If we wanted to, we could workaround the deb_extract behavior
        right here, by writing to a tmpfile, then munging things as we
        wrote to the actual stream. */
-     deb_extract(pkg->local_filename, file,
+    buffer = deb_extract(pkg->local_filename, file,
                 extract_quiet | extract_data_tar_gz | extract_list,
                 NULL, NULL);
 
     /* BUG: How do we know if deb_extract worked or not? This is a
        defect in the current deb_extract from what I can tell. */
                 extract_quiet | extract_data_tar_gz | extract_list,
                 NULL, NULL);
 
     /* BUG: How do we know if deb_extract worked or not? This is a
        defect in the current deb_extract from what I can tell. */
+    if (buffer == NULL) {
+        return EINVAL;
+    }
+    free(buffer);
     return 0;
 }
     return 0;
 }
index b354c9e3e84eef8d0aacafeca1d32c843f756633..563f3a144315c6d9ec6ed3617e2832ef38c7bac3 100644 (file)
@@ -26,6 +26,8 @@ int str_list_elt_init(str_list_elt_t *elt, char *data)
 
 void str_list_elt_deinit(str_list_elt_t *elt)
 {
 
 void str_list_elt_deinit(str_list_elt_t *elt)
 {
+    if (elt->data)
+        free(elt->data);
     void_list_elt_deinit((void_list_elt_t *) elt);
 }
 
     void_list_elt_deinit((void_list_elt_t *) elt);
 }
 
@@ -44,17 +46,26 @@ int str_list_init(str_list_t *list)
 
 void str_list_deinit(str_list_t *list)
 {
 
 void str_list_deinit(str_list_t *list)
 {
-    void_list_deinit((void_list_t *) list);
+    str_list_elt_t *elt;
+    while (!void_list_empty(list)) {
+        elt = str_list_first(list);
+        if (!elt)
+            return;
+        list_del_init(&elt->node);
+        free(elt->data);
+        elt->data=NULL;
+        free(elt);
+    }
 }
 
 int str_list_append(str_list_t *list, char *data)
 {
 }
 
 int str_list_append(str_list_t *list, char *data)
 {
-    return void_list_append((void_list_t *) list, data);
+    return void_list_append((void_list_t *) list, strdup(data));
 }
 
 int str_list_push(str_list_t *list, char *data)
 {
 }
 
 int str_list_push(str_list_t *list, char *data)
 {
-    return void_list_push((void_list_t *) list, data);
+    return void_list_push((void_list_t *) list, strdup(data));
 }
 
 str_list_elt_t *str_list_pop(str_list_t *list)
 }
 
 str_list_elt_t *str_list_pop(str_list_t *list)
@@ -62,17 +73,21 @@ str_list_elt_t *str_list_pop(str_list_t *list)
     return (str_list_elt_t *) void_list_pop((void_list_t *) list);
 }
 
     return (str_list_elt_t *) void_list_pop((void_list_t *) list);
 }
 
-str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter)
+void str_list_remove(str_list_t *list, str_list_elt_t **iter)
 {
 {
-    return (str_list_elt_t *) void_list_remove((void_list_t *) list,
+    str_list_elt_t * elt = void_list_remove((void_list_t *) list,
                                               (void_list_elt_t **) iter);
                                               (void_list_elt_t **) iter);
+
+    str_list_elt_deinit(elt);
 }
 
 }
 
-char *str_list_remove_elt(str_list_t *list, const char *target_str)
+void str_list_remove_elt(str_list_t *list, const char *target_str)
 {
 {
-     return (char *)void_list_remove_elt((void_list_t *) list,
+     char *str = void_list_remove_elt((void_list_t *) list,
                                         (void *)target_str,
                                         (void_list_cmp_t)strcmp);
                                         (void *)target_str,
                                         (void_list_cmp_t)strcmp);
+     if (str) 
+         free(str);
 }
 
 str_list_elt_t *str_list_first(str_list_t *list) {
 }
 
 str_list_elt_t *str_list_first(str_list_t *list) {
@@ -93,14 +108,6 @@ str_list_elt_t *str_list_last(str_list_t *list) {
 
 
 void str_list_purge(str_list_t *list) {
 
 
 void str_list_purge(str_list_t *list) {
-    str_list_elt_t *elt;
-    while (!void_list_empty(list)) {
-        elt = str_list_first(list);
-        if (!elt)
-            return;
-        list_del_init(&elt->node);
-        free(elt->data);
-        elt->data=NULL;
-        free(elt);
-    }
+    str_list_deinit(list);
+    free(list);
 }
 }
index b187a064914fee43f2a481e2475f71cb946343c9..4f62fe7a33375dad17e4ba65c7839666e7db5a32 100644 (file)
@@ -34,8 +34,8 @@ void str_list_deinit(str_list_t *list);
 int str_list_append(str_list_t *list, char *data);
 int str_list_push(str_list_t *list, char *data);
 str_list_elt_t *str_list_pop(str_list_t *list);
 int str_list_append(str_list_t *list, char *data);
 int str_list_push(str_list_t *list, char *data);
 str_list_elt_t *str_list_pop(str_list_t *list);
-str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter);
-char *str_list_remove_elt(str_list_t *list, const char *target_str);
+void str_list_remove(str_list_t *list, str_list_elt_t **iter);
+void str_list_remove_elt(str_list_t *list, const char *target_str);
 
 str_list_elt_t *str_list_first(str_list_t *list);
 str_list_elt_t *str_list_prev(str_list_t *list, str_list_elt_t *node);
 
 str_list_elt_t *str_list_first(str_list_t *list);
 str_list_elt_t *str_list_prev(str_list_t *list, str_list_elt_t *node);