[Asterisk-code-review] pbx.c: On error, ast_add_extension2_lockopt should always free 'data' (asterisk[13])

Friendly Automation asteriskteam at digium.com
Fri Oct 2 10:07:15 CDT 2020


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/15021 )

Change subject: pbx.c: On error, ast_add_extension2_lockopt should always free 'data'
......................................................................

pbx.c: On error, ast_add_extension2_lockopt should always free 'data'

In the event that the desired extension already exists,
ast_add_extension2_lockopt() will free the 'data' it is passed before
returning an error, so we should not be freeing it ourselves.

Additionally, there were two places where ast_add_extension2_lockopt()
could return an error without also freeing the 'data' pointer, so we
add that.

ASTERISK-29097 #close

Change-Id: I904707aae55169feda050a5ed7c6793b53fe6eae
---
M include/asterisk/pbx.h
M main/pbx.c
M res/parking/parking_bridge_features.c
M res/res_parking.c
M res/res_pjsip_config_wizard.c
5 files changed, 17 insertions(+), 6 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index 1cb05cf..46a7faa 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -483,9 +483,13 @@
  * \param callerid pattern to match CallerID, or NULL to match any CallerID
  * \param application application to run on the extension with that priority level
  * \param data data to pass to the application
- * \param datad
+ * \param datad a pointer to a function that will deallocate \c data when needed
+ *              or NULL if \c data does not need to be freed.
  * \param registrar who registered the extension
  *
+ * \note On any failure, the function pointed to by \c datap will be called and passed the
+ *       \c data pointer.
+ *
  * \retval 0 success
  * \retval -1 failure
  */
@@ -507,7 +511,7 @@
  * \since 12.0.0
  *
  * \note con must be write locked prior to calling. For details about the arguments,
- *       check ast_add_extension2()
+ *       check ast_add_extension()
  */
 int ast_add_extension2_nolock(struct ast_context *con, int replace, const char *extension,
 	int priority, const char *label, const char *callerid,
diff --git a/main/pbx.c b/main/pbx.c
index 8728f2c..8ab6000 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -7261,6 +7261,10 @@
 	if (ast_strlen_zero(extension)) {
 		ast_log(LOG_ERROR,"You have to be kidding-- add exten '' to context %s? Figure out a name and call me back. Action ignored.\n",
 				con->name);
+		/* We always need to deallocate 'data' on failure */
+		if (datad) {
+			datad(data);
+		}
 		return -1;
 	}
 
@@ -7313,8 +7317,14 @@
 	}
 
 	/* Be optimistic:  Build the extension structure first */
-	if (!(tmp = ast_calloc(1, length)))
+	tmp = ast_calloc(1, length);
+	if (!tmp) {
+		/* We always need to deallocate 'data' on failure */
+		if (datad) {
+			datad(data);
+		}
 		return -1;
+	}
 
 	if (ast_strlen_zero(label)) /* let's turn empty labels to a null ptr */
 		label = 0;
diff --git a/res/parking/parking_bridge_features.c b/res/parking/parking_bridge_features.c
index 30f4526..06300ea 100644
--- a/res/parking/parking_bridge_features.c
+++ b/res/parking/parking_bridge_features.c
@@ -643,7 +643,6 @@
 			dial_string_flat, PARK_DIAL_CONTEXT, ast_get_extension_registrar(existing_exten));
 	} else if (ast_add_extension2_nolock(park_dial_context, 1, dial_string_flat, 1, NULL, NULL,
 			"Dial", duplicate_returnexten, ast_free_ptr, BASE_REGISTRAR)) {
-			ast_free(duplicate_returnexten);
 		ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n",
 			dial_string_flat, PARK_DIAL_CONTEXT, returnexten);
 	}
diff --git a/res/res_parking.c b/res/res_parking.c
index f5279a8..4da7a15 100644
--- a/res/res_parking.c
+++ b/res/res_parking.c
@@ -726,7 +726,6 @@
 
 	if (ast_add_extension2_nolock(context, replace, extension, priority, NULL, NULL,
 			application, data_duplicate, ast_free_ptr, registrar)) {
-		ast_free(data_duplicate);
 		return -1;
 	}
 
diff --git a/res/res_pjsip_config_wizard.c b/res/res_pjsip_config_wizard.c
index 383a2d5..f719ff4 100644
--- a/res/res_pjsip_config_wizard.c
+++ b/res/res_pjsip_config_wizard.c
@@ -477,7 +477,6 @@
 
 	if (ast_add_extension2_nolock(context, 0, exten, priority, NULL, NULL,
 			app, data, free_ptr, BASE_REGISTRAR)) {
-		ast_free(data);
 		return -1;
 	}
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15021
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: I904707aae55169feda050a5ed7c6793b53fe6eae
Gerrit-Change-Number: 15021
Gerrit-PatchSet: 3
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20201002/e9a6fe7a/attachment-0001.html>


More information about the asterisk-code-review mailing list