Workarounded cabextract memory leaks by captive memory tracker object.
authorshort <>
Thu, 11 Dec 2003 20:49:59 +0000 (20:49 +0000)
committershort <>
Thu, 11 Dec 2003 20:49:59 +0000 (20:49 +0000)
Fixed SIGSEGV-possible cabextract 'decomp_state' reentrancy.

src/install/acquire/Makefile.am
src/install/acquire/cabinet-memory.c [new file with mode: 0644]
src/install/acquire/cabinet-memory.h [new file with mode: 0644]
src/install/acquire/cabinet.c
src/install/acquire/cabinet.h

index 62f15c0..7f97717 100644 (file)
@@ -34,6 +34,8 @@ captive_install_acquire_SOURCES= \
                cabextract/cabextract.h \
                cabinet.c \
                cabinet.h \
+               cabinet-memory.c \
+               cabinet-memory.h \
                captivemodid.c \
                captivemodid.h \
                diskscan.c \
diff --git a/src/install/acquire/cabinet-memory.c b/src/install/acquire/cabinet-memory.c
new file mode 100644 (file)
index 0000000..6e2c4cc
--- /dev/null
@@ -0,0 +1,217 @@
+/* $Id$
+ * cabextract memory allocation for acquiration installation utility
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include "config.h"
+
+#include "cabinet-memory.h"    /* self */
+#include <glib/gmessages.h>
+#include <glib/ghash.h>
+#include <glib/gtypes.h>
+#include <glib/gslist.h>
+
+
+struct _CaptiveAcquireCabinetMemoryObject {
+       GObject parent_instance;
+
+       /*< private >*/
+       GHashTable *memory_hash;
+       gpointer data;
+       gsize data_size;
+       };
+struct _CaptiveAcquireCabinetMemoryObjectClass {
+       GObjectClass parent_class;
+       };
+
+static CaptiveAcquireCabinetMemoryObject *memory_object;
+static GSList *memory_object_slist;
+
+static gpointer captive_acquire_cabinet_memory_object_parent_class=NULL;
+
+static void captive_acquire_cabinet_memory_object_finalize_foreach
+               (gpointer mem /* key */,gpointer mem_val,gpointer user_data /* unused */)
+{
+       g_return_if_fail(mem!=NULL);
+       g_return_if_fail(mem_val!=NULL);
+       g_return_if_fail(mem==mem_val);
+
+       g_free(mem);
+}
+
+static void captive_acquire_cabinet_memory_object_finalize(CaptiveAcquireCabinetMemoryObject *memory)
+{
+       g_return_if_fail(memory!=NULL);
+       g_return_if_fail(memory!=memory_object);
+
+       g_hash_table_foreach(memory->memory_hash,
+                       captive_acquire_cabinet_memory_object_finalize_foreach, /* func */
+                       NULL);  /* user_data; unused */
+
+       g_hash_table_destroy(memory->memory_hash);
+
+       g_free(memory->data);
+       memory->data=NULL;
+       memory->data_size=0;
+
+       G_OBJECT_CLASS(captive_acquire_cabinet_memory_object_parent_class)->finalize((GObject *)memory);
+}
+
+static void captive_acquire_cabinet_memory_object_class_init(CaptiveAcquireCabinetMemoryObjectClass *class)
+{
+GObjectClass *gobject_class=G_OBJECT_CLASS(class);
+
+       captive_acquire_cabinet_memory_object_parent_class=g_type_class_ref(g_type_parent(G_TYPE_FROM_CLASS(class)));
+       gobject_class->finalize=(void (*)(GObject *object))captive_acquire_cabinet_memory_object_finalize;
+}
+
+static void captive_acquire_cabinet_memory_object_init(CaptiveAcquireCabinetMemoryObject *memory_object)
+{
+       memory_object->memory_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
+}
+
+GType captive_acquire_cabinet_memory_object_get_type(void)
+{
+static GType captive_acquire_cabinet_memory_object_type=0;
+
+       if (!captive_acquire_cabinet_memory_object_type) {
+static const GTypeInfo captive_acquire_cabinet_memory_object_info={
+                               sizeof(CaptiveAcquireCabinetMemoryObjectClass),
+                               NULL,   /* base_init */
+                               NULL,   /* base_finalize */
+                               (GClassInitFunc)captive_acquire_cabinet_memory_object_class_init,
+                               NULL,   /* class_finalize */
+                               NULL,   /* class_data */
+                               sizeof(CaptiveAcquireCabinetMemoryObject),
+                               5,      /* n_preallocs */
+                               (GInstanceInitFunc)captive_acquire_cabinet_memory_object_init,
+                               };
+
+               captive_acquire_cabinet_memory_object_type=g_type_register_static(G_TYPE_OBJECT,
+                               "CaptiveAcquireCabinetMemoryObject",&captive_acquire_cabinet_memory_object_info,0);
+               }
+
+       return captive_acquire_cabinet_memory_object_type;
+}
+
+CaptiveAcquireCabinetMemoryObject *acquire_cabinet_memory_object_new(void)
+{
+       return g_object_new(
+                       CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT,     /* object_type */
+                       NULL);  /* first_property_name */
+}
+
+void acquire_cabinet_memory_object_push(CaptiveAcquireCabinetMemoryObject *memory)
+{
+       g_return_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory));
+
+       if (memory_object)
+               memory_object_slist=g_slist_prepend(memory_object_slist,memory_object);
+       memory_object=memory;
+}
+
+/* 'memory' is just a sanity check */
+void acquire_cabinet_memory_object_pop(CaptiveAcquireCabinetMemoryObject *memory)
+{
+       g_return_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory));
+       g_return_if_fail(memory==memory_object);
+
+       if (!memory_object_slist)
+               memory_object=NULL;
+       else {
+               memory_object=memory_object_slist->data;
+               g_assert(memory_object!=NULL);
+               memory_object_slist=g_slist_delete_link(memory_object_slist,memory_object_slist);
+               }
+}
+
+gpointer acquire_cabinet_memory_malloc(gulong n_bytes)
+{
+gpointer r;
+
+       g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
+
+       r=g_malloc(n_bytes);
+       g_assert((!r)==(!n_bytes));
+       if (r)
+               g_hash_table_insert(memory_object->memory_hash,r,r);
+
+       return r;
+}
+
+gpointer acquire_cabinet_memory_malloc0(gulong n_bytes)
+{
+gpointer r;
+
+       g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
+
+       r=g_malloc0(n_bytes);
+       g_assert((!r)==(!n_bytes));
+       if (r)
+               g_hash_table_insert(memory_object->memory_hash,r,r);
+
+       return r;
+}
+
+gpointer acquire_cabinet_memory_realloc(gpointer mem,gulong n_bytes)
+{
+gpointer r;
+gboolean errbool;
+
+       g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
+
+       if (mem!=NULL) {
+               errbool=g_hash_table_remove(memory_object->memory_hash,mem);
+               g_assert(errbool==TRUE);
+               }
+       r=g_realloc(mem,n_bytes);
+       g_assert((!r)==(!n_bytes));
+       if (r)
+               g_hash_table_insert(memory_object->memory_hash,r,r);
+
+       return r;
+}
+
+void acquire_cabinet_memory_free(gpointer mem)
+{
+gboolean errbool;
+
+       g_return_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object));
+
+       if (mem!=NULL) {
+               errbool=g_hash_table_remove(memory_object->memory_hash,mem);
+               g_assert(errbool==TRUE);
+               }
+       g_free(mem);
+}
+
+/* UGLY HACK for 'cabextract/cabextract.c/decomp_state' */
+gpointer acquire_cabinet_memory_data_get(gsize size)
+{
+       g_return_val_if_fail(size>0,NULL);
+       g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
+
+       if (!memory_object->data_size) {
+               memory_object->data_size=size;
+               memory_object->data=g_malloc0(memory_object->data_size);
+               }
+
+       g_assert(memory_object->data_size==size);
+       g_assert(memory_object->data!=NULL);
+
+       return memory_object->data;
+}
diff --git a/src/install/acquire/cabinet-memory.h b/src/install/acquire/cabinet-memory.h
new file mode 100644 (file)
index 0000000..a829068
--- /dev/null
@@ -0,0 +1,57 @@
+/* $Id$
+ * Include file for cabextract memory allocation for acquiration installation utility
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _CAPTIVE_INSTALL_ACQUIRE_CABINET_MEMORY_H
+#define _CAPTIVE_INSTALL_ACQUIRE_CABINET_MEMORY_H 1
+
+
+#include <glib/gtypes.h>
+#include <glib-object.h>
+
+
+G_BEGIN_DECLS
+
+#define CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT            (captive_acquire_cabinet_memory_object_get_type())
+#define CAPTIVE_ACQUIRE_CABINET_MEMORY_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST((object),CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT,CaptiveAcquireCabinetMemoryObject))
+#define CAPTIVE_ACQUIRE_CABINET_MEMORY_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT,CaptiveAcquireCabinetMemoryObjectClass))
+#define CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE((object),CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT))
+#define CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT))
+#define CAPTIVE_ACQUIRE_CABINET_MEMORY_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj),CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT,CaptiveAcquireCabinetMemoryObjectClass))
+typedef struct _CaptiveAcquireCabinetMemoryObject      CaptiveAcquireCabinetMemoryObject;
+typedef struct _CaptiveAcquireCabinetMemoryObjectClass CaptiveAcquireCabinetMemoryObjectClass;
+
+
+GType captive_acquire_cabinet_memory_object_get_type(void);
+
+CaptiveAcquireCabinetMemoryObject *acquire_cabinet_memory_object_new(void);
+
+void acquire_cabinet_memory_object_push(CaptiveAcquireCabinetMemoryObject *memory);
+void acquire_cabinet_memory_object_pop(CaptiveAcquireCabinetMemoryObject *memory);
+
+gpointer acquire_cabinet_memory_malloc (gulong n_bytes);
+gpointer acquire_cabinet_memory_malloc0(gulong n_bytes);
+gpointer acquire_cabinet_memory_realloc(gpointer mem,gulong n_bytes);
+void     acquire_cabinet_memory_free   (gpointer mem);
+
+gpointer acquire_cabinet_memory_data_get(gsize size);
+
+G_END_DECLS
+
+
+#endif /* _CAPTIVE_INSTALL_ACQUIRE_CABINET_MEMORY_H */
index ade7d80..a748492 100644 (file)
@@ -32,6 +32,7 @@
 #include <signal.h>
 #include <setjmp.h>
 #include <sys/time.h>
+#include "cabinet-memory.h"
 
 #include <captive/macros.h>
 
@@ -287,6 +288,7 @@ struct acquire_cabinet *r;
        acquire_cabinet_set_uri(r,uri);
        r->cabinet_done=0;
        r->cabinet_used=cabinet_used;
+       r->memory=acquire_cabinet_memory_object_new();
 
        return r;
 }
@@ -347,6 +349,7 @@ href_done:;
 
        acquire_cabinet_set_uri(r,uri);
        gnome_vfs_uri_unref(uri);
+       r->memory=acquire_cabinet_memory_object_new();
 
        return r;
 }
@@ -362,6 +365,7 @@ void acquire_cabinet_free(struct acquire_cabinet *acquire_cabinet)
        g_free((/* de-const */ gchar *)acquire_cabinet->filename);
        gnome_vfs_uri_unref(acquire_cabinet->uri);
        gnome_vfs_uri_unref(acquire_cabinet->handle_uri);
+       g_object_unref(acquire_cabinet->memory);
        g_free(acquire_cabinet);
 }
 
@@ -397,13 +401,15 @@ struct file *filelist,*fi;
        if ((*ui_progress)(acquire_cabinet->uri))
                return;
 
+       acquire_cabinet_memory_object_push(acquire_cabinet->memory);
+
        basecab=find_cabs_in_file(acquire_cabinet);
        if (!basecab)
-               return;
+               goto fail_memory_pop;
        if (basecab->next)
-               return;
+               goto fail_memory_pop;
        if (basecab->prevcab || basecab->nextcab)
-               return;
+               goto fail_memory_pop;
 
        filelist=process_files(basecab);
 
@@ -418,7 +424,7 @@ int errint;
                uri_fi=gnome_vfs_uri_append_file_name(acquire_cabinet->uri,fi->filename);
                if ((*ui_progress)(uri_fi)) {
                        gnome_vfs_uri_unref(uri_fi);
-                       return;
+                       goto fail_memory_pop;
                        }
 
                file_write_fi_assertion=fi;
@@ -433,7 +439,7 @@ int errint;
                                        TRUE);  /* free_segment */
                        gnome_vfs_uri_unref(uri_fi);
                        if (!errint)
-                               return;
+                               goto fail_memory_pop;
                        continue;
                        }
                file_buffer=g_byte_array_free(file_write_bytearray,
@@ -442,4 +448,9 @@ int errint;
                gnome_vfs_uri_unref(uri_fi);
                g_free(file_buffer);
     }
+
+       /* FALLTHRU */
+
+fail_memory_pop:
+       acquire_cabinet_memory_object_pop(acquire_cabinet->memory);
 }
index 5de7520..e94a25c 100644 (file)
@@ -24,6 +24,7 @@
 #include <glib/gtypes.h>
 #include <libgnomevfs/gnome-vfs-file-size.h>
 #include <libgnomevfs/gnome-vfs-handle.h>
+#include "cabinet-memory.h"
 
 
 G_BEGIN_DECLS
@@ -47,6 +48,7 @@ struct acquire_cabinet {
        GnomeVFSResult (*read)
                        (struct acquire_cabinet *acquire_cabinet,gpointer buffer,GnomeVFSFileSize bytes,GnomeVFSFileSize *bytes_read);
        gint cabinet_done,cabinet_used;
+       CaptiveAcquireCabinetMemoryObject *memory;      /* cabextract/cabextract.c memory leaks tracker */
        };
 
 void acquire_cabinet_seek(struct acquire_cabinet *acquire_cabinet,GnomeVFSFileOffset offset);