[file-roller/wip/jtojnar/clang-analysis: 3/8] archive-libarchive: Clean up memory management of libarchive structs
- From: Jan Tojnar <jtojnar src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [file-roller/wip/jtojnar/clang-analysis: 3/8] archive-libarchive: Clean up memory management of libarchive structs
- Date: Tue, 6 Sep 2022 23:42:00 +0000 (UTC)
commit 99f442699d2f1a5a0345cdb1821aaa83e903a23e
Author: Jan Tojnar <jtojnar gmail com>
Date: Sat Sep 3 16:58:18 2022 +0200
archive-libarchive: Clean up memory management of libarchive structs
Define auto-cleanup functions, taken from
https://github.com/fwupd/fwupd/blob/4df0d2176be4c3439d95310a974d8077ae40dbe0/libfwupdplugin/fu-archive.c#L326-L357
Although I removed the close functions since we want to capture the error when close fails.
src/fr-archive-libarchive.c | 57 +++++++++++++++++++++++++++++++--------------
1 file changed, 39 insertions(+), 18 deletions(-)
---
diff --git a/src/fr-archive-libarchive.c b/src/fr-archive-libarchive.c
index ac9f0b69..5f189014 100644
--- a/src/fr-archive-libarchive.c
+++ b/src/fr-archive-libarchive.c
@@ -42,6 +42,38 @@
#define FILE_ATTRIBUTES_NEEDED_BY_ARCHIVE_ENTRY ("standard::*,time::*,access::*,unix::*")
+/* workaround the struct types of libarchive */
+typedef struct archive _archive_read_ctx;
+
+static void
+_archive_read_ctx_free(_archive_read_ctx *arch)
+{
+ archive_read_free(arch);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(_archive_read_ctx, _archive_read_ctx_free)
+
+typedef struct archive _archive_write_ctx;
+
+static void
+_archive_write_ctx_free(_archive_write_ctx *arch)
+{
+ archive_write_free(arch);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(_archive_write_ctx, _archive_write_ctx_free)
+
+typedef struct archive_entry _archive_entry_ctx;
+
+static void
+_archive_entry_ctx_free(_archive_entry_ctx *entry)
+{
+ archive_entry_free(entry);
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(_archive_entry_ctx, _archive_entry_ctx_free)
+
+
typedef struct {
gssize compressed_size;
gssize uncompressed_size;
@@ -374,7 +406,7 @@ load_data_close (struct archive *a,
static int
create_read_object (LoadData *load_data,
- struct archive **a)
+ _archive_read_ctx **a)
{
*a = archive_read_new ();
archive_read_support_filter_all (*a);
@@ -436,7 +468,7 @@ list_archive_thread (GSimpleAsyncResult *result,
GCancellable *cancellable)
{
LoadData *load_data;
- struct archive *a;
+ g_autoptr (_archive_read_ctx) a = NULL;
struct archive_entry *entry;
int r;
@@ -447,7 +479,6 @@ list_archive_thread (GSimpleAsyncResult *result,
r = create_read_object (load_data, &a);
if (r != ARCHIVE_OK) {
- archive_read_free(a);
return;
}
@@ -510,7 +541,6 @@ list_archive_thread (GSimpleAsyncResult *result,
if (load_data->error != NULL)
g_simple_async_result_set_from_error (result, load_data->error);
- archive_read_free (a);
load_data_free (load_data);
}
@@ -751,7 +781,7 @@ extract_archive_thread (GSimpleAsyncResult *result,
GHashTable *created_files;
GHashTable *folders_created_during_extraction;
GHashTable *symlinks;
- struct archive *a;
+ g_autoptr (_archive_read_ctx) a = NULL;
struct archive_entry *entry;
int r;
@@ -760,7 +790,6 @@ extract_archive_thread (GSimpleAsyncResult *result,
r = create_read_object (load_data, &a);
if (r != ARCHIVE_OK) {
- archive_read_free(a);
return;
}
@@ -1054,7 +1083,6 @@ extract_archive_thread (GSimpleAsyncResult *result,
g_hash_table_unref (created_files);
g_hash_table_unref (checked_folders);
g_hash_table_unref (symlinks);
- archive_read_free (a);
extract_data_free (extract_data);
}
@@ -1548,7 +1576,7 @@ _archive_write_file (struct archive *b,
{
LoadData *load_data = LOAD_DATA (save_data);
g_autoptr (GFileInfo) info = NULL;
- struct archive_entry *w_entry;
+ g_autoptr (_archive_entry_ctx) w_entry = NULL;
int rb;
/* write the file header */
@@ -1563,14 +1591,12 @@ _archive_write_file (struct archive *b,
w_entry = archive_entry_new ();
if (! _archive_entry_copy_file_info (w_entry, info, save_data)) {
- archive_entry_free (w_entry);
return WRITE_ACTION_SKIP_ENTRY;
}
/* honor the update flag */
if (save_data->update && (r_entry != NULL) && (archive_entry_mtime (w_entry) < archive_entry_mtime
(r_entry))) {
- archive_entry_free (w_entry);
return WRITE_ACTION_WRITE_ENTRY;
}
@@ -1598,8 +1624,6 @@ _archive_write_file (struct archive *b,
if ((load_data->error == NULL) && (rb <= ARCHIVE_FAILED))
load_data->error = g_error_new_literal (FR_ERROR, FR_ERROR_COMMAND_ERROR,
archive_error_string (b));
- archive_entry_free (w_entry);
-
return (load_data->error == NULL) ? WRITE_ACTION_SKIP_ENTRY : WRITE_ACTION_ABORT;
}
@@ -1611,7 +1635,8 @@ save_archive_thread (GSimpleAsyncResult *result,
{
SaveData *save_data;
LoadData *load_data;
- struct archive *a, *b;
+ g_autoptr (_archive_read_ctx) a = NULL;
+ g_autoptr (_archive_write_ctx) b = NULL;
struct archive_entry *r_entry;
int ra = ARCHIVE_OK, rb = ARCHIVE_OK;
@@ -1629,7 +1654,7 @@ save_archive_thread (GSimpleAsyncResult *result,
save_data->begin_operation (save_data, save_data->user_data);
while ((load_data->error == NULL) && (ra = archive_read_next_header (a, &r_entry)) == ARCHIVE_OK) {
- struct archive_entry *w_entry;
+ g_autoptr (_archive_entry_ctx) w_entry = NULL;
WriteAction action;
if (g_cancellable_is_cancelled (cancellable))
@@ -1672,8 +1697,6 @@ save_archive_thread (GSimpleAsyncResult *result,
}
else if (action == WRITE_ACTION_SKIP_ENTRY)
fr_archive_progress_inc_completed_bytes (load_data->archive, archive_entry_size
(r_entry));
-
- archive_entry_free (w_entry);
}
if (g_error_matches (load_data->error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
@@ -1693,8 +1716,6 @@ save_archive_thread (GSimpleAsyncResult *result,
if (load_data->error != NULL)
g_simple_async_result_set_from_error (result, load_data->error);
- archive_read_free (a);
- archive_write_free (b);
save_data_free (save_data);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]