]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
pbx.c: On error, ast_add_extension2_lockopt should always free 'data'
authorSean Bright <sean.bright@gmail.com>
Tue, 29 Sep 2020 18:04:48 +0000 (14:04 -0400)
committerFriendly Automation <jenkins2@gerrit.asterisk.org>
Fri, 2 Oct 2020 15:07:13 +0000 (10:07 -0500)
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

include/asterisk/pbx.h
main/pbx.c
res/parking/parking_bridge_features.c
res/res_parking.c
res/res_pjsip_config_wizard.c

index 1cb05cf728b1fc56913ee95b4cdaea0ca4c52c62..46a7faa03eac32b2d56bebdf8329cbb285de196b 100644 (file)
@@ -483,9 +483,13 @@ void ast_pbx_hangup_handler_push(struct ast_channel *chan, const char *handler);
  * \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 @@ int ast_add_extension2(struct ast_context *con, int replace, const char *extensi
  * \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,
index 8728f2c86b47a5f2ff27a0d0872888f145ade7aa..8ab6000b404f4a6fee33e7fbd58387562b3d3ea2 100644 (file)
@@ -7261,6 +7261,10 @@ static int ast_add_extension2_lockopt(struct ast_context *con,
        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 @@ static int ast_add_extension2_lockopt(struct ast_context *con,
        }
 
        /* 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;
index 30f45268cd3c0924b947c1b7f1accb53398ea238..06300ead845e8bc660232eee2ad3a0ace53d5aeb 100644 (file)
@@ -643,7 +643,6 @@ static int parking_duration_callback(struct ast_bridge_channel *bridge_channel,
                        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);
        }
index f5279a8674045d2e04b2b0a3a8328c4fdf7d2954..4da7a159ab5d80dc0e21eff42c524a3efc7ebf61 100644 (file)
@@ -726,7 +726,6 @@ static int parking_add_extension(struct ast_context *context, int replace, const
 
        if (ast_add_extension2_nolock(context, replace, extension, priority, NULL, NULL,
                        application, data_duplicate, ast_free_ptr, registrar)) {
-               ast_free(data_duplicate);
                return -1;
        }
 
index 383a2d5a2305619629de8c738f242ea112d3c018..f719ff4b672aa47e88d1705e0ecacae8aa131e1e 100644 (file)
@@ -477,7 +477,6 @@ static int add_extension(struct ast_context *context, const char *exten,
 
        if (ast_add_extension2_nolock(context, 0, exten, priority, NULL, NULL,
                        app, data, free_ptr, BASE_REGISTRAR)) {
-               ast_free(data);
                return -1;
        }