From 483c4d7573439e71d0bdfe5a9192b3ffc06d35c1 Mon Sep 17 00:00:00 2001 From: ticktock35 Date: Thu, 26 Mar 2009 03:58:43 -0400 Subject: reduce a big memory leak connecting deb_extract Null pointers git-svn-id: http://opkg.googlecode.com/svn/trunk@206 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- (limited to 'libopkg') diff --git a/libopkg/opkg.c b/libopkg/opkg.c index 11b5ee7..735fb58 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -1064,6 +1064,7 @@ int opkg_repository_accessibility_check(opkg_t *opkg) free(stmp); str_list_append(src, repo_ptr); + free(repo_ptr); repositories++; } while (repositories > 0) diff --git a/libopkg/opkg_remove.c b/libopkg/opkg_remove.c index 0b913fd..3886ccc 100644 --- a/libopkg/opkg_remove.c +++ b/libopkg/opkg_remove.c @@ -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)) { - str_list_append(&installed_dirs, strdup(file_name)); + str_list_append(&installed_dirs, file_name); continue; } diff --git a/libopkg/pkg.c b/libopkg/pkg.c index 6e0f396..1ab24e1 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -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); + free(installed_file_name); free(line); } @@ -1433,7 +1434,7 @@ int pkg_free_installed_files(pkg_t *pkg) return 0; if (pkg->installed_files) { - str_list_deinit(pkg->installed_files); + str_list_purge(pkg->installed_files); } pkg->installed_files = NULL; diff --git a/libopkg/pkg_extract.c b/libopkg/pkg_extract.c index eae7746..0aae4ad 100644 --- a/libopkg/pkg_extract.c +++ b/libopkg/pkg_extract.c @@ -37,7 +37,6 @@ int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream) return EINVAL; } - /* XXX: QUESTION: Is there a way to do this directly with deb_extract now? */ 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; + char *buffer = NULL; 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, @@ -65,23 +65,26 @@ int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, 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) { - 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); - /* 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; } @@ -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) { + 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 @@ -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. */ - 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. */ + if (buffer == NULL) { + return EINVAL; + } + free(buffer); return 0; } diff --git a/libopkg/str_list.c b/libopkg/str_list.c index b354c9e..563f3a1 100644 --- a/libopkg/str_list.c +++ b/libopkg/str_list.c @@ -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) { + if (elt->data) + free(elt->data); 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_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) { - 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) { - 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) @@ -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); } -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); + + 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); + if (str) + free(str); } 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) { - 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); } diff --git a/libopkg/str_list.h b/libopkg/str_list.h index b187a06..4f62fe7 100644 --- a/libopkg/str_list.h +++ b/libopkg/str_list.h @@ -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); -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); -- cgit v0.9.1