From d277f3ec3ee7a7a38971f5da56bd79347ac6bf9a Mon Sep 17 00:00:00 2001
From: Richard Mudgett <rmudgett@digium.com>
Date: Fri, 21 Feb 2014 18:04:54 +0000
Subject: [PATCH] json: Fix off-nominal json ref counting issues.

* Fixed off-nominal json ref counting issue with using the following API
calls: ast_json_object_set() and ast_json_array_append().

* Fixed off-nominal error reporting in ast_ari_endpoints_list().

* Fixed some miscellaneous off-nominal json ref counting issues in
report_receive_fax_status() and dial_to_json().
........

Merged revisions 408713 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@408714 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_meetme.c            | 17 ++++++-----------
 include/asterisk/json.h      | 20 ++++++++++----------
 main/sorcery.c               | 10 ++++------
 main/stasis_channels.c       |  3 +++
 res/ari/resource_endpoints.c | 10 +---------
 res/res_fax.c                |  1 +
 res/res_sorcery_astdb.c      |  1 -
 res/res_stasis_recording.c   |  1 -
 8 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/apps/app_meetme.c b/apps/app_meetme.c
index 4651bf0eba..37053b3e67 100644
--- a/apps/app_meetme.c
+++ b/apps/app_meetme.c
@@ -1362,25 +1362,20 @@ static void meetme_stasis_generate_msg(struct ast_conference *meetme_conference,
 	if (user) {
 		struct timeval now = ast_tvnow();
 		long duration = (long)(now.tv_sec - user->jointime);
-		RAII_VAR(struct ast_json *, json_user, ast_json_integer_create(user->user_no), ast_json_unref);
-		RAII_VAR(struct ast_json *, json_user_duration, NULL, ast_json_unref);
+		struct ast_json *json_user;
+		struct ast_json *json_user_duration;
 
-		if (ast_json_object_set(json_object, "user", json_user)) {
+		json_user = ast_json_integer_create(user->user_no);
+		if (!json_user || ast_json_object_set(json_object, "user", json_user)) {
 			return;
 		}
-		json_user = NULL;
 
 		if (duration > 0) {
 			json_user_duration = ast_json_integer_create(duration);
-
-			if (!json_user_duration) {
-				return;
-			}
-
-			if (ast_json_object_set(json_object, "duration", json_user_duration)) {
+			if (!json_user_duration
+				|| ast_json_object_set(json_object, "duration", json_user_duration)) {
 				return;
 			}
-			json_user_duration = NULL;
 		}
 	}
 
diff --git a/include/asterisk/json.h b/include/asterisk/json.h
index 15e1108da6..6ceeb0b86f 100644
--- a/include/asterisk/json.h
+++ b/include/asterisk/json.h
@@ -443,8 +443,8 @@ struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index);
  * \brief Change an element in an array.
  * \since 12.0.0
  *
- * The \a array steals the \a value reference; use ast_json_ref() to safely keep a pointer
- * to it.
+ * \note The \a array steals the \a value reference even if it returns error;
+ * use ast_json_ref() to safely keep a pointer to it.
  *
  * \param array JSON array to modify.
  * \param index Zero-based index into array.
@@ -458,8 +458,8 @@ int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *va
  * \brief Append to an array.
  * \since 12.0.0
  *
- * The array steals the \a value reference; use ast_json_ref() to safely keep a pointer
- * to it.
+ * \note The \a array steals the \a value reference even if it returns error;
+ * use ast_json_ref() to safely keep a pointer to it.
  *
  * \param array JSON array to modify.
  * \param value New JSON value to store at the end of \a array.
@@ -472,8 +472,8 @@ int ast_json_array_append(struct ast_json *array, struct ast_json *value);
  * \brief Insert into an array.
  * \since 12.0.0
  *
- * The array steals the \a value reference; use ast_json_ref() to safely keep a pointer
- * to it.
+ * \note The \a array steals the \a value reference even if it returns error;
+ * use ast_json_ref() to safely keep a pointer to it.
  *
  * \param array JSON array to modify.
  * \param index Zero-based index into array.
@@ -554,8 +554,8 @@ struct ast_json *ast_json_object_get(struct ast_json *object, const char *key);
  * \brief Set a field in a JSON object.
  * \since 12.0.0
  *
- * The object steals the \a value reference; use ast_json_ref() to safely keep a pointer
- * to it.
+ * \note The object steals the \a value reference even if it returns error;
+ * use ast_json_ref() to safely keep a pointer to it.
  *
  * \param object JSON object to modify.
  * \param key Key of field to set.
@@ -701,8 +701,8 @@ struct ast_json *ast_json_object_iter_value(struct ast_json_iter *iter);
  * \brief Set the value of the field pointed to by an iterator.
  * \since 12.0.0
  *
- * The array steals the value reference; use ast_json_ref() to safely keep a
- * pointer to it.
+ * \note The object steals the \a value reference even if it returns error;
+ * use ast_json_ref() to safely keep a pointer to it.
  *
  * \param object JSON object \a iter was obtained from.
  * \param iter JSON object iterator.
diff --git a/main/sorcery.c b/main/sorcery.c
index 5104e3ada9..bbdfa8cf2b 100644
--- a/main/sorcery.c
+++ b/main/sorcery.c
@@ -1095,8 +1095,7 @@ struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sor
 			for (field = tmp; field; field = field->next) {
 				struct ast_json *value = ast_json_string_create(field->value);
 
-				if (value && ast_json_object_set(json, field->name, value)) {
-					ast_json_unref(value);
+				if (!value || ast_json_object_set(json, field->name, value)) {
 					res = -1;
 				}
 			}
@@ -1106,10 +1105,9 @@ struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sor
 			char *buf = NULL;
 			struct ast_json *value = NULL;
 
-			if ((res = object_field->handler(object, object_field->args, &buf)) ||
-				!(value = ast_json_string_create(buf)) ||
-				ast_json_object_set(json, object_field->name, value)) {
-				ast_json_unref(value);
+			if ((res = object_field->handler(object, object_field->args, &buf))
+				|| !(value = ast_json_string_create(buf))
+				|| ast_json_object_set(json, object_field->name, value)) {
 				res = -1;
 			}
 
diff --git a/main/stasis_channels.c b/main/stasis_channels.c
index 677527ba32..fe65c1766a 100644
--- a/main/stasis_channels.c
+++ b/main/stasis_channels.c
@@ -977,6 +977,9 @@ static struct ast_json *dial_to_json(
 		"forward", ast_json_object_get(blob, "forward"),
 		"dialstring", ast_json_object_get(blob, "dialstring"));
 	if (!json) {
+		ast_json_unref(caller_json);
+		ast_json_unref(peer_json);
+		ast_json_unref(forwarded_json);
 		return NULL;
 	}
 
diff --git a/res/ari/resource_endpoints.c b/res/ari/resource_endpoints.c
index 2f47b1a1a0..16b7ebd8d2 100644
--- a/res/ari/resource_endpoints.c
+++ b/res/ari/resource_endpoints.c
@@ -71,16 +71,8 @@ void ast_ari_endpoints_list(struct ast_variable *headers,
 		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
 		struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
 		struct ast_json *json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
-		int r;
 
-		if (!json_endpoint) {
-			ao2_iterator_destroy(&i);
-			return;
-		}
-
-		r = ast_json_array_append(
-			json, json_endpoint);
-		if (r != 0) {
+		if (!json_endpoint || ast_json_array_append(json, json_endpoint)) {
 			ao2_iterator_destroy(&i);
 			ast_ari_response_alloc_failed(response);
 			return;
diff --git a/res/res_fax.c b/res/res_fax.c
index c4e64cd717..e05b101f74 100644
--- a/res/res_fax.c
+++ b/res/res_fax.c
@@ -1774,6 +1774,7 @@ static int report_receive_fax_status(struct ast_channel *chan, const char *filen
 	struct ast_json *json_filename = ast_json_string_create(filename);
 
 	if (!json_array || !json_filename) {
+		ast_json_unref(json_filename);
 		return -1;
 	}
 	ast_json_array_append(json_array, json_filename);
diff --git a/res/res_sorcery_astdb.c b/res/res_sorcery_astdb.c
index 10d881e8fb..d04153ea25 100644
--- a/res/res_sorcery_astdb.c
+++ b/res/res_sorcery_astdb.c
@@ -76,7 +76,6 @@ static struct ast_json *sorcery_objectset_to_json(const struct ast_variable *obj
 			ast_json_unref(json);
 			return NULL;
 		} else if (ast_json_object_set(json, field->name, value)) {
-			ast_json_unref(value);
 			ast_json_unref(json);
 			return NULL;
 		}
diff --git a/res/res_stasis_recording.c b/res/res_stasis_recording.c
index f0fa1e5f6f..0dee1e5be7 100644
--- a/res/res_stasis_recording.c
+++ b/res/res_stasis_recording.c
@@ -229,7 +229,6 @@ static void recording_publish(struct stasis_app_recording *recording, const char
 		}
 
 		if (ast_json_object_set(json, "cause", failure_cause)) {
-			ast_json_unref(failure_cause);
 			return;
 		}
 	}
-- 
GitLab