[svn-commits] file: branch 12 r405325 - in /branches/12: include/asterisk/ res/ res/ari/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sun Jan 12 16:23:15 CST 2014


Author: file
Date: Sun Jan 12 16:23:12 2014
New Revision: 405325

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=405325
Log:
res_ari: Fix various memory leaks.

This change fixes a few memory leaks that were found based
on a mailing list post.

1. Some JSON response messages were never freed. This was
caused by the documentation stating that message references
were stolen when in reality they were not. The code now follows
the documentation and usage has been updated.

2. HTTP response headers were never freed.

3. The variable list for wildcards paths was never freed.

(closes issue ASTERISK-23128)
Reported by: Kenneth Watson (on list)

Review: https://reviewboard.asterisk.org/r/3119/

Modified:
    branches/12/include/asterisk/ari.h
    branches/12/res/ari/resource_applications.c
    branches/12/res/ari/resource_bridges.c
    branches/12/res/ari/resource_channels.c
    branches/12/res/ari/resource_device_states.c
    branches/12/res/ari/resource_endpoints.c
    branches/12/res/ari/resource_playbacks.c
    branches/12/res/ari/resource_recordings.c
    branches/12/res/res_ari.c

Modified: branches/12/include/asterisk/ari.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/ari.h?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/include/asterisk/ari.h (original)
+++ branches/12/include/asterisk/ari.h Sun Jan 12 16:23:12 2014
@@ -214,7 +214,7 @@
  * \brief Fill in an \c OK (200) \a ast_ari_response.
  * \param response Response to fill in.
  * \param message JSON response.  This reference is stolen, so just \ref
- *                ast_json_incref if you need to keep a reference to it.
+ *                ast_json_ref if you need to keep a reference to it.
  */
 void ast_ari_response_ok(struct ast_ari_response *response,
 			     struct ast_json *message);
@@ -226,6 +226,10 @@
 
 /*!
  * \brief Fill in a <tt>Created</tt> (201) \a ast_ari_response.
+ * \param response Response to fill in.
+ * \param url URL to the created resource.
+ * \param message JSON response.  This reference is stolen, so just \ref
+ *                ast_json_ref if you need to keep a reference to it.
  */
 void ast_ari_response_created(struct ast_ari_response *response,
 	const char *url, struct ast_json *message);

Modified: branches/12/res/ari/resource_applications.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_applications.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_applications.c (original)
+++ branches/12/res/ari/resource_applications.c Sun Jan 12 16:23:12 2014
@@ -68,15 +68,14 @@
 		return;
 	}
 
-
-	ast_ari_response_ok(response, json);
+	ast_ari_response_ok(response, ast_json_ref(json));
 }
 
 void ast_ari_applications_get(struct ast_variable *headers,
 	struct ast_ari_applications_get_args *args,
 	struct ast_ari_response *response)
 {
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 
 	json = stasis_app_to_json(args->application_name);
 
@@ -113,7 +112,7 @@
 
 	switch (res) {
 	case STASIS_ASR_OK:
-		ast_ari_response_ok(response, json);
+		ast_ari_response_ok(response, ast_json_ref(json));
 		break;
 	case STASIS_ASR_APP_NOT_FOUND:
 		ast_ari_response_error(response, 404, "Not Found",
@@ -152,7 +151,7 @@
 
 	switch (res) {
 	case STASIS_ASR_OK:
-		ast_ari_response_ok(response, json);
+		ast_ari_response_ok(response, ast_json_ref(json));
 		break;
 	case STASIS_ASR_APP_NOT_FOUND:
 		ast_ari_response_error(response, 404, "Not Found",

Modified: branches/12/res/ari/resource_bridges.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_bridges.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_bridges.c (original)
+++ branches/12/res/ari/resource_bridges.c Sun Jan 12 16:23:12 2014
@@ -411,7 +411,7 @@
 	play_channel = NULL;
 	control = NULL;
 
-	ast_ari_response_created(response, playback_url, json);
+	ast_ari_response_created(response, playback_url, ast_json_ref(json));
 }
 
 void ast_ari_bridges_record(struct ast_variable *headers,
@@ -566,7 +566,7 @@
 	record_channel = NULL;
 	control = NULL;
 
-	ast_ari_response_created(response, recording_url, json);
+	ast_ari_response_created(response, recording_url, ast_json_ref(json));
 }
 
 void ast_ari_bridges_start_moh(struct ast_variable *headers,

Modified: branches/12/res/ari/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_channels.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_channels.c (original)
+++ branches/12/res/ari/resource_channels.c Sun Jan 12 16:23:12 2014
@@ -362,7 +362,7 @@
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
 	RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
 	RAII_VAR(char *, playback_url, NULL, ast_free);
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 	const char *language;
 
 	ast_assert(response != NULL);
@@ -434,7 +434,7 @@
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
 	RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
 	RAII_VAR(char *, recording_url, NULL, ast_free);
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 	RAII_VAR(struct stasis_app_recording_options *, options, NULL,
 		ao2_cleanup);
 	RAII_VAR(char *, uri_encoded_name, NULL, ast_free);

Modified: branches/12/res/ari/resource_device_states.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_device_states.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_device_states.c (original)
+++ branches/12/res/ari/resource_device_states.c Sun Jan 12 16:23:12 2014
@@ -35,7 +35,7 @@
 	struct ast_ari_device_states_list_args *args,
 	struct ast_ari_response *response)
 {
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 
 	if (!(json = stasis_app_device_states_to_json())) {
 		ast_ari_response_error(response, 500,
@@ -50,7 +50,7 @@
 	struct ast_ari_device_states_get_args *args,
 	struct ast_ari_response *response)
 {
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 
 	if (!(json = stasis_app_device_state_to_json(
 		      args->device_name, ast_device_state(args->device_name)))) {

Modified: branches/12/res/ari/resource_endpoints.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_endpoints.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_endpoints.c (original)
+++ branches/12/res/ari/resource_endpoints.c Sun Jan 12 16:23:12 2014
@@ -158,7 +158,7 @@
 	struct ast_ari_endpoints_get_args *args,
 	struct ast_ari_response *response)
 {
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 	RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
 
 	snapshot = ast_endpoint_latest_snapshot(args->tech, args->resource);
@@ -174,5 +174,5 @@
 		return;
 	}
 
-	ast_ari_response_ok(response, ast_json_ref(json));
+	ast_ari_response_ok(response, json);
 }

Modified: branches/12/res/ari/resource_playbacks.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_playbacks.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_playbacks.c (original)
+++ branches/12/res/ari/resource_playbacks.c Sun Jan 12 16:23:12 2014
@@ -35,7 +35,7 @@
 	struct ast_ari_response *response)
 {
 	RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 
 	playback = stasis_app_playback_find_by_id(args->playback_id);
 	if (playback == NULL) {
@@ -51,7 +51,7 @@
 		return;
 	}
 
-	ast_ari_response_ok(response, ast_json_ref(json));
+	ast_ari_response_ok(response, json);
 }
 void ast_ari_playbacks_stop(struct ast_variable *headers,
 	struct ast_ari_playbacks_stop_args *args,

Modified: branches/12/res/ari/resource_recordings.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/resource_recordings.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/ari/resource_recordings.c (original)
+++ branches/12/res/ari/resource_recordings.c Sun Jan 12 16:23:12 2014
@@ -76,7 +76,7 @@
 {
 	RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
 		ao2_cleanup);
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 
 	recording = stasis_app_stored_recording_find_by_name(
 		args->recording_name);
@@ -93,7 +93,7 @@
 		return;
 	}
 
-	ast_ari_response_ok(response, ast_json_ref(json));
+	ast_ari_response_ok(response, json);
 }
 
 void ast_ari_recordings_delete_stored(struct ast_variable *headers,
@@ -142,7 +142,7 @@
 	struct ast_ari_response *response)
 {
 	RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
-	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *json;
 
 	recording = stasis_app_recording_find_by_name(args->recording_name);
 	if (recording == NULL) {
@@ -158,7 +158,7 @@
 		return;
 	}
 
-	ast_ari_response_ok(response, ast_json_ref(json));
+	ast_ari_response_ok(response, json);
 }
 
 static void control_recording(const char *name,

Modified: branches/12/res/res_ari.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_ari.c?view=diff&rev=405325&r1=405324&r2=405325
==============================================================================
--- branches/12/res/res_ari.c (original)
+++ branches/12/res/res_ari.c Sun Jan 12 16:23:12 2014
@@ -253,7 +253,7 @@
 void ast_ari_response_ok(struct ast_ari_response *response,
 			     struct ast_json *message)
 {
-	response->message = ast_json_ref(message);
+	response->message = message;
 	response->response_code = 200;
 	response->response_text = "OK";
 }
@@ -275,7 +275,7 @@
 void ast_ari_response_created(struct ast_ari_response *response,
 	const char *url, struct ast_json *message)
 {
-	response->message = ast_json_ref(message);
+	response->message = message;
 	response->response_code = 201;
 	response->response_text = "Created";
 	ast_str_append(&response->headers, 0, "Location: %s\r\n", url);
@@ -465,7 +465,7 @@
 	RAII_VAR(char *, response_text, NULL, ast_free);
 	RAII_VAR(struct stasis_rest_handlers *, root, NULL, ao2_cleanup);
 	struct stasis_rest_handlers *handler;
-	struct ast_variable *path_vars = NULL;
+	RAII_VAR(struct ast_variable *, path_vars, NULL, ast_variables_destroy);
 	char *path = ast_strdupa(uri);
 	char *path_segment;
 	stasis_rest_callback callback;
@@ -839,14 +839,13 @@
 				struct ast_variable *headers)
 {
 	RAII_VAR(struct ast_ari_conf *, conf, NULL, ao2_cleanup);
-	RAII_VAR(struct ast_str *, response_headers, ast_str_create(40), ast_free);
 	RAII_VAR(struct ast_str *, response_body, ast_str_create(256), ast_free);
 	RAII_VAR(struct ast_ari_conf_user *, user, NULL, ao2_cleanup);
 	struct ast_ari_response response = {};
 	int ret = 0;
 	RAII_VAR(struct ast_variable *, post_vars, NULL, ast_variables_destroy);
 
-	if (!response_headers || !response_body) {
+	if (!response_body) {
 		return -1;
 	}
 
@@ -857,6 +856,7 @@
 
 	conf = ast_ari_config_get();
 	if (!conf || !conf->general) {
+		ast_free(response.headers);
 		return -1;
 	}
 
@@ -955,6 +955,7 @@
 	if (response.no_response) {
 		/* The handler indicates no further response is necessary.
 		 * Probably because it already handled it */
+		ast_free(response.headers);
 		return 0;
 	}
 
@@ -964,13 +965,11 @@
 	ast_assert(response.message != NULL);
 	ast_assert(response.response_code > 0);
 
-	ast_str_append(&response_headers, 0, "%s", ast_str_buffer(response.headers));
-
 	/* response.message could be NULL, in which case the empty response_body
 	 * is correct
 	 */
 	if (response.message && !ast_json_is_null(response.message)) {
-		ast_str_append(&response_headers, 0,
+		ast_str_append(&response.headers, 0,
 			       "Content-type: application/json\r\n");
 		if (ast_json_dump_str_format(response.message, &response_body,
 				conf->general->format) != 0) {
@@ -978,16 +977,15 @@
 			response.response_code = 500;
 			response.response_text = "Internal Server Error";
 			ast_str_set(&response_body, 0, "%s", "");
-			ast_str_set(&response_headers, 0, "%s", "");
+			ast_str_set(&response.headers, 0, "%s", "");
 			ret = -1;
 		}
 	}
 
 	ast_http_send(ser, method, response.response_code,
-		      response.response_text, response_headers, response_body,
+		      response.response_text, response.headers, response_body,
 		      0, 0);
 	/* ast_http_send takes ownership, so we don't have to free them */
-	response_headers = NULL;
 	response_body = NULL;
 
 	ast_json_unref(response.message);




More information about the svn-commits mailing list