PolicyKit: Branch 'master' - 3 commits

David Zeuthen david at kemper.freedesktop.org
Tue Apr 8 12:45:07 PDT 2008


 src/kit/kit-list.c                   |    6 +-
 src/kit/kit-memory.c                 |   84 +++++++++++++++++++++++++++++++++++
 src/kit/kit-memory.h                 |    1 
 src/kit/kit-string.c                 |    4 -
 src/kit/kit-test.c                   |    4 +
 src/polkit/polkit-authorization-db.c |   10 +++-
 6 files changed, 102 insertions(+), 7 deletions(-)

New commits:
commit 228a6ec9691c7382a9794c79e34dbf1f5316528b
Author: David Zeuthen <davidz at redhat.com>
Date:   Tue Apr 8 15:42:46 2008 -0400

    don't leak the copied authorizations list on OOM
    
    Now the test suite passes again.

diff --git a/src/kit/kit-list.c b/src/kit/kit-list.c
index b00ebfc..da030a3 100644
--- a/src/kit/kit-list.c
+++ b/src/kit/kit-list.c
@@ -93,14 +93,16 @@ KitList *
 kit_list_copy (KitList *list)
 {
         KitList *l;
+        KitList *l2;
         KitList *j;
 
         l = NULL;
         for (j = list; j != NULL; j = j->next) {
                 /* TODO: prepend, then reverse */
-                l = kit_list_append (l, j->data);
-                if (l == NULL)
+                l2 = kit_list_append (l, j->data);
+                if (l2 == NULL)
                         goto oom;
+                l = l2;
         }
 
         return l;
diff --git a/src/polkit/polkit-authorization-db.c b/src/polkit/polkit-authorization-db.c
index 39bd12c..f40f5fd 100644
--- a/src/polkit/polkit-authorization-db.c
+++ b/src/polkit/polkit-authorization-db.c
@@ -342,6 +342,8 @@ _authdb_get_auths_for_uid (PolKitAuthorizationDB *authdb,
                 goto out;
         }
 
+        //kit_warning ("standard_output='%s'", standard_output);
+
         if (standard_output != NULL) {
                 uid_t uid2;
                 len = strlen (standard_output);
@@ -472,8 +474,12 @@ _internal_foreach (PolKitAuthorizationDB       *authdb,
          * may disappear from under us due to revoke_if_one_shot...
          */
         auths_copy = kit_list_copy (auths);
-        if (auths_copy == NULL)
+        if (auths_copy == NULL) {
+                polkit_error_set_error (error,
+                                        POLKIT_ERROR_OUT_OF_MEMORY,
+                                        "No memory");
                 goto out;
+        }
         for (l = auths_copy; l != NULL; l = l->next)
                 polkit_authorization_ref ((PolKitAuthorization *) l->data);
 
@@ -1280,7 +1286,7 @@ _run_test (void)
                 //kit_warning ("%p: %d: %s: %s", 
                 //             error, 
                 //             polkit_error_get_error_code (error), 
-                //            polkit_error_get_error_name (error),
+                //             polkit_error_get_error_name (error),
                 //             polkit_error_get_error_message (error));
                 kit_assert (polkit_error_is_set (error) && 
                             polkit_error_get_error_code (error) == POLKIT_ERROR_OUT_OF_MEMORY);
commit fdea41e34e67f4007ab8705dccfaede4b9e95304
Author: David Zeuthen <davidz at redhat.com>
Date:   Tue Apr 8 15:41:54 2008 -0400

    clear the right block when growing a string

diff --git a/src/kit/kit-string.c b/src/kit/kit-string.c
index 9ce0570..0647815 100644
--- a/src/kit/kit-string.c
+++ b/src/kit/kit-string.c
@@ -853,9 +853,9 @@ kit_string_ensure_size (KitString *s, size_t new_size)
                 if (p == NULL)
                         goto oom;
                 /* zero the new block we got */
-                memset (s->buf + s->buf_len, 0, grow_to - s->buf_len);
                 s->buf = p;
-                s->buf_len += KIT_STRING_BLOCK_SIZE;
+                memset (s->buf + s->buf_len, 0, grow_to - s->buf_len);
+                s->buf_len = grow_to;
         }
 
         return TRUE;
commit d0d42aa293e8784c5a47f9cf8acf92236a51a414
Author: David Zeuthen <davidz at redhat.com>
Date:   Tue Apr 8 15:41:11 2008 -0400

    print stack traces for where leaks were allocated

diff --git a/src/kit/kit-memory.c b/src/kit/kit-memory.c
index 50d9abc..f4f84b4 100644
--- a/src/kit/kit-memory.c
+++ b/src/kit/kit-memory.c
@@ -59,12 +59,29 @@ static int _cur_allocs = 0;
 static int _total_allocs = 0;
 static int _fail_nth = -1;
 
+
+#if defined(KIT_BUILD_TESTS) && defined(BUILT_R_DYNAMIC)
+typedef struct _KitAllocationEntry {
+        const void *memory;
+        void *backtrace[100];
+        int backtrace_size;
+        struct _KitAllocationEntry *next;
+} KitAllocationEntry;
+
+static KitAllocationEntry *alloc_list_head = NULL;
+#endif
+
 void 
 _kit_memory_reset (void)
 {
         _cur_allocs = 0;
         _total_allocs = 0;
         _fail_nth = -1;
+
+#if defined(KIT_BUILD_TESTS) && defined(BUILT_R_DYNAMIC)
+        /* TODO: free existing allocs */
+        alloc_list_head = NULL;
+#endif
 }
 
 int 
@@ -85,6 +102,67 @@ _kit_memory_fail_nth_alloc (int number)
         _fail_nth = number;
 }
 
+static inline void
+_alloc_add (const void *memory)
+{
+#if defined(KIT_BUILD_TESTS) && defined(BUILT_R_DYNAMIC)
+        KitAllocationEntry *entry;
+
+        entry = malloc (sizeof (KitAllocationEntry));
+        entry->memory = memory;
+        entry->backtrace_size = backtrace (entry->backtrace, 100);
+        entry->next = alloc_list_head;
+
+        alloc_list_head = entry;
+#endif
+}
+
+static inline void
+_alloc_remove (const void *memory)
+{
+#if defined(KIT_BUILD_TESTS) && defined(BUILT_R_DYNAMIC)
+        KitAllocationEntry *l;
+        KitAllocationEntry **prev;
+
+        prev = &alloc_list_head;
+        for (l = alloc_list_head; l != NULL; l = l->next) {
+                if (l->memory == memory) {
+                        *prev = l->next;
+                        free (l);
+                        break;
+                }
+
+                prev = &(l->next);
+        }
+#endif
+}
+
+void
+_kit_memory_print_outstanding_allocations (void)
+{
+#if defined(KIT_BUILD_TESTS) && defined(BUILT_R_DYNAMIC)
+        KitAllocationEntry *l;
+        for (l = alloc_list_head; l != NULL; l = l->next) {
+                int i;
+                char **syms;
+
+                syms = backtrace_symbols (l->backtrace, l->backtrace_size);
+
+                i = 0;
+                while (i < l->backtrace_size)
+                {
+                        fprintf (stderr, "  %s\n", syms[i]);
+                        ++i;
+                }
+                fprintf (stderr, "\n");
+                fflush (stderr);
+
+                free (syms);
+        }
+#endif
+}
+
+
 /**
  * kit_malloc:
  * @bytes: number of 8-bit bytes to allocate
@@ -113,6 +191,7 @@ kit_malloc (size_t bytes)
         if (p != NULL)  {
                 _cur_allocs++;
                 _total_allocs++;
+                _alloc_add (p);
         }
 
         return p;
@@ -141,6 +220,7 @@ kit_malloc0 (size_t bytes)
         if (p != NULL)  {
                 _cur_allocs++;
                 _total_allocs++;
+                _alloc_add (p);
         }
 
         return p;
@@ -173,7 +253,10 @@ kit_realloc (void *memory, size_t bytes)
                 return NULL;
         }
 
+        _alloc_remove (p);
         p = realloc (memory, bytes);
+        if (p != NULL)
+                _alloc_add (p);
 
         return p;
 }
@@ -189,6 +272,7 @@ kit_free (void *memory)
 {
         free (memory);
         if (memory != NULL) {
+                _alloc_remove (memory);
                 _cur_allocs--;
         }
 }
diff --git a/src/kit/kit-memory.h b/src/kit/kit-memory.h
index 7c887fc..fae2da1 100644
--- a/src/kit/kit-memory.h
+++ b/src/kit/kit-memory.h
@@ -71,6 +71,7 @@ void  _kit_memory_reset (void);
 int   _kit_memory_get_current_allocations (void);
 int   _kit_memory_get_total_allocations (void);
 void  _kit_memory_fail_nth_alloc (int number);
+void  _kit_memory_print_outstanding_allocations (void);
 
 KIT_END_DECLS
 
diff --git a/src/kit/kit-test.c b/src/kit/kit-test.c
index 5aae02e..3f8718f 100644
--- a/src/kit/kit-test.c
+++ b/src/kit/kit-test.c
@@ -98,6 +98,7 @@ kit_test_run (KitTest **tests, size_t num_tests)
                 delta = _kit_memory_get_current_allocations ();
                 if (delta != 0) {
                         printf ("  Unit test leaked %d allocations\n", delta);
+                        _kit_memory_print_outstanding_allocations ();
                         ret = FALSE;
                 }
                 if (num_fd != num_fd_after) {
@@ -121,7 +122,8 @@ kit_test_run (KitTest **tests, size_t num_tests)
                         
                         delta = _kit_memory_get_current_allocations ();
                         if (delta != 0) {
-                                printf ("  Unit test leaked %d allocations\n", delta);
+                                printf ("  Unit test leaked %d allocations:\n", delta);
+                                _kit_memory_print_outstanding_allocations ();
                                 ret = FALSE;
                         }
                         if (num_fd != num_fd_after) {


More information about the hal-commit mailing list