json: Fix off-nominal json ref counting issues.
authorRichard Mudgett <rmudgett@digium.com>
Fri, 21 Feb 2014 18:04:54 +0000 (18:04 +0000)
committerRichard Mudgett <rmudgett@digium.com>
Fri, 21 Feb 2014 18:04:54 +0000 (18:04 +0000)
* 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
include/asterisk/json.h
main/sorcery.c
main/stasis_channels.c
res/ari/resource_endpoints.c
res/res_fax.c
res/res_sorcery_astdb.c
res/res_stasis_recording.c

index 4651bf0..37053b3 100644 (file)
@@ -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;
                }
        }
 
index 15e1108..6ceeb0b 100644 (file)
@@ -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.
index 5104e3a..bbdfa8c 100644 (file)
@@ -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;
                        }
 
index 677527b..fe65c17 100644 (file)
@@ -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;
        }
 
index 2f47b1a..16b7ebd 100644 (file)
@@ -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;
index c4e64cd..e05b101 100644 (file)
@@ -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);
index 10d881e..d04153e 100644 (file)
@@ -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;
                }
index f0fa1e5..0dee1e5 100644 (file)
@@ -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;
                }
        }