OSDN Git Service

libc: atexit: reuse free slots at the end of exit functions table
authorRonald Wahl <ronald.wahl@raritan.com>
Mon, 4 Feb 2013 13:51:46 +0000 (14:51 +0100)
committerCarmelo Amoroso <carmelo.amoroso@st.com>
Tue, 5 Feb 2013 14:59:18 +0000 (15:59 +0100)
Continuosly dlopen and dlclose of shared object will cause a memory leak
in atexit function. This fix reuse free slots at the end of the list.

For further detail see https://bugs.busybox.net/show_bug.cgi?id=2455

Signed-off-by: Ronald Wahl <ronald.wahl@raritan.com>
Tested-by: Filippo Arcidiacono <filippo.arcidiacono@st.com>
Signed-off-by: Carmelo Amoroso <carmelo.amoroso@st.com>
libc/stdlib/_atexit.c

index 4bb8840..ef6772f 100644 (file)
@@ -241,6 +241,16 @@ struct exit_function attribute_hidden *__new_exitfn(void)
 
     __UCLIBC_MUTEX_LOCK(__atexit_lock);
 
+       /*
+        * Reuse free slots at the end of the list.
+        * This avoids eating memory when dlopen and dlclose modules multiple times.
+       */
+       while (__exit_count > 0) {
+               if (__exit_function_table[__exit_count-1].type == ef_free) {
+                       --__exit_count;
+               } else break;
+       }
+
 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
     /* If we are out of function table slots, make some more */
     if (__exit_slots < __exit_count+1) {