[Linux-cachefs] [PATCH] cachefilesd: avoid memory leak on failure exit

Wang Sheng-Hui shhuiw at gmail.com
Mon Jan 14 15:30:13 UTC 2013


On failure exit, we may have allocated space for the culltalbes.
Free the space before exit.

Signed-off-by: Wang Sheng-Hui <shhuiw at gmail.com>
---
  cachefilesd.c |   29 ++++++++++++++++++++++++++---
  1 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/cachefilesd.c b/cachefilesd.c
index f5c04ec..0ac5e5c 100644
--- a/cachefilesd.c
+++ b/cachefilesd.c
@@ -92,8 +92,8 @@ static int jumpstart_scan = 0;
   *   culled objects
   */
  static unsigned culltable_size = 4096;
-static struct object **cullbuild;
-static struct object **cullready;
+static struct object **cullbuild = NULL;
+static struct object **cullready = NULL;

  static int oldest_build = -1;
  static int oldest_ready = -1;
@@ -145,6 +145,26 @@ void help(void)
  	exit(2);
  }

+static void free_culltable(void)
+{
+	if (cullbuild) {
+		while (oldest_build >= 0) {
+			if (((long)cullbuild[oldest_build] & 0xf0000000) != 0x60000000)
+				free(cullbuild[oldest_build]);
+			oldest_build--;
+		}
+		free(cullbuild);
+	}
+	if (cullready) {
+		while (oldest_ready >= 0) {
+			if (((long)cullready[oldest_ready] & 0xf0000000) != 0x60000000)
+				free(cullready[oldest_ready]);
+			oldest_ready--;
+		}
+		free(cullready);
+	}
+}
+
  static __attribute__((noreturn, format(printf, 2, 3)))
  void __error(int excode, const char *fmt, ...)
  {
@@ -168,6 +188,7 @@ void __error(int excode, const char *fmt, ...)
  		closelog();
  	}

+	free_culltable();
  	exit(excode);
  }

@@ -659,6 +680,8 @@ static void cachefilesd(void)
  			reap_graveyard();
  	}

+	free_culltable();
+
  	notice("Daemon Terminated");
  	exit(0);
  }
@@ -1419,7 +1442,7 @@ static void decant_cull_table(void)
  check:
  	for (loop = 0; loop < oldest_ready; loop++)
  		if (((long)cullready[loop] & 0xf0000000) == 0x60000000)
-			abort();
+			error("Bad object in cullready table (%d:%d)", oldest_ready, loop);
  }

  /*****************************************************************************/
-- 
1.6.0.2




More information about the Linux-cachefs mailing list