[sheepdog] [PATCH stable-0.6 2/2] sheep: correct a way of freeing sockfd_cache_entry

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Sep 2 04:39:32 CEST 2013


This patch does the same fix done in the commit 44abf0b8. Below is the
commit message of the commit.

  Previous implementation of the sockfd caching mechanism has two memory
  leaks. The leak is caused when sockfd_cache_destroy(),
  sockfd_cache_add_nolock() try to free a sockfd cache entry. They
  forget to free entry->fds.

  This patch adds a new function, free_cache_entry() for safe freeing of
  sockfd cache entries. And let the above two functions to use it.

  Also this patch lets sockfd_cache_add() use free_cache_entry() for
  consistency.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 sheep/sockfd_cache.c |   13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sheep/sockfd_cache.c b/sheep/sockfd_cache.c
index 82ce0aa..e96ec61 100644
--- a/sheep/sockfd_cache.c
+++ b/sheep/sockfd_cache.c
@@ -182,6 +182,12 @@ static inline void destroy_all_slots(struct sockfd_cache_entry *entry)
 			close(entry->fds[i].fd);
 }
 
+static void free_cache_entry(struct sockfd_cache_entry *entry)
+{
+	free(entry->fds);
+	free(entry);
+}
+
 /*
  * Destroy all the Cached FDs of the node
  *
@@ -209,7 +215,7 @@ static bool sockfd_cache_destroy(const struct node_id *nid)
 	pthread_rwlock_unlock(&sockfd_cache.lock);
 
 	destroy_all_slots(entry);
-	free(entry);
+	free_cache_entry(entry);
 
 	return true;
 false_out:
@@ -242,7 +248,7 @@ static void sockfd_cache_add_nolock(const struct node_id *nid)
 
 	memcpy(&new->nid, nid, sizeof(struct node_id));
 	if (sockfd_cache_insert(new)) {
-		free(new);
+		free_cache_entry(new);
 		return;
 	}
 	sockfd_cache.count++;
@@ -277,8 +283,7 @@ void sockfd_cache_add(const struct node_id *nid)
 
 	memcpy(&new->nid, nid, sizeof(struct node_id));
 	if (sockfd_cache_insert(new)) {
-		free(new->fds);
-		free(new);
+		free_cache_entry(new);
 		pthread_rwlock_unlock(&sockfd_cache.lock);
 		return;
 	}
-- 
1.7.10.4




More information about the sheepdog mailing list