[Pkg-clamav-commits] [SCM] Debian repository for ClamAV branch, debian/unstable, updated. debian/0.95+dfsg-1-6156-g094ec9b

Török Edvin edwin at clamav.net
Sun Apr 4 01:07:58 UTC 2010


The following commit has been merged in the debian/unstable branch:
commit 6e65b70e29b88690ef512c03cca949591d63a8f2
Author: Török Edvin <edwin at clamav.net>
Date:   Fri Oct 2 13:21:33 2009 +0300

    Must free engine on exit, otherwise valgrind incorrectly reports leaks.
    
    This is a cherry-pick of a similar commit on the bytecode branch.
    There this was needed because the JIT stores pointers mangled (stores an integer in low
    bits of the pointer), thus valgrind can't see that pointers to that memory
    region still exist, and reports it as definetely lost, instead of still
    reachable.
    However this seems to happen on master too (where there is no JIT), valgrind
    cannot see that everything allocating in engine is reachable from the engine
    pointer, and reports some of the memory allocated for the engine (the regex
    parts) as possible lost, and reports the rest as still reachable. This seems to
    only happen with certain kernel versions, timings, etc.
    
    The fix is to always free the engine, even if this is the parent after the fork.

diff --git a/clamd/clamd.c b/clamd/clamd.c
index 75cc3f5..db7864a 100644
--- a/clamd/clamd.c
+++ b/clamd/clamd.c
@@ -85,11 +85,24 @@ static void help(void)
 }
 
 static struct optstruct *opts;
-/* needs to be global, so that valgrind reports it as reachable, and not
- * as definetely/indirectly lost when daemonizing clamd */
-static struct cl_engine *engine = NULL;
+
+/* When running under valgrind and daemonizing, valgrind incorrectly reports
+ * leaks from the engine, because it can't see that all the memory is still
+ * reachable (some pointers are stored mangled in the JIT). 
+ * So free the engine on exit from the parent too (during daemonize)
+ */
+static struct cl_engine *gengine = NULL;
+static void free_engine(void)
+{
+    if (gengine) {
+	cl_engine_free(gengine);
+	gengine = NULL;
+    }
+}
+
 int main(int argc, char **argv)
 {
+        static struct cl_engine *engine = NULL;
 	const struct optstruct *opt;
 #ifndef	C_WINDOWS
         struct passwd *user = NULL;
@@ -441,11 +454,14 @@ int main(int argc, char **argv)
 		fcntl(lsockets[ret], F_SETFL, fcntl(lsockets[ret], F_GETFL) | O_NONBLOCK);
 	}
 #endif
+	gengine = engine;
+	atexit(free_engine);
 	if(daemonize() == -1) {
 	    logg("!daemonize() failed\n");
 	    ret = 1;
 	    break;
 	}
+	gengine = NULL;
 #ifdef C_BSD
 	for(ret=0;ret<nlsockets;ret++) {
 		fcntl(lsockets[ret], F_SETFL, fcntl(lsockets[ret], F_GETFL) & ~O_NONBLOCK);

-- 
Debian repository for ClamAV



More information about the Pkg-clamav-commits mailing list