From e4dafd3d0b9568c0dbc9ef37325dfd46c607ff33 Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Thu, 28 May 2009 20:36:49 +0000
Subject: [PATCH] Ensure that accidental calls to
 ast_string_field_free_memory() on embedded stringfield pools are safe.

It is possible for a stringfield manager structure (and pool) structure to be allocated
as part of a larger structure allocation (using ast_calloc_with_strinfields()); when
this is done, the stringfield pool cannot be separately freed, but users of the tructure
may not be aware (and shouldn't have to be aware) of whether the pool was embedded.
This patch modifies the behavior so that they can always call ast_string_field_free_memory()
and the function will do the right thing for both embedded and non-embedded situations.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@197775 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/utils.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/main/utils.c b/main/utils.c
index fe2d083d58..4c0ff3aa8f 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1559,11 +1559,17 @@ int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_
 		return add_string_pool(mgr, pool_head, needed, file, lineno, func);
 	}
 
+	/* if there is an embedded pool, we can't actually release *all*
+	 * pools, we must keep the embedded one. if the caller is about
+	 * to free the structure that contains the stringfield manager
+	 * and embedded pool anyway, it will be freed as part of that
+	 * operation.
+	 */
+	if ((needed < 0) && mgr->embedded_pool) {
+		needed = 0;
+	}
+
 	if (needed < 0) {		/* reset all pools */
-		if (*pool_head == NULL) {
-			ast_log(LOG_WARNING, "trying to reset empty pool\n");
-			return -1;
-		}
 		cur = *pool_head;
 	} else if (mgr->embedded_pool) { /* preserve the embedded pool */
 		preserve = mgr->embedded_pool;
-- 
GitLab