]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
RTP Engine: Deal with errors returned from AST_VECTOR_REPLACE.
authorCorey Farrell <git@cfware.com>
Mon, 6 Nov 2017 21:30:10 +0000 (16:30 -0500)
committerCorey Farrell <git@cfware.com>
Mon, 6 Nov 2017 21:30:10 +0000 (16:30 -0500)
Check for errors from AST_VECTOR_REPLACE and clean memory if needed.

Change-Id: I124d15cc1d645f85a72a1279f623c1993b304b0b

main/rtp_engine.c

index d82bc498014cdb02baff7ed5f9027bcbe40eebdc..e7032724b1e8a4ebf5124f22834f34c0057c6337 100644 (file)
@@ -728,10 +728,11 @@ void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_cod
                        ao2_t_cleanup(AST_VECTOR_GET(&dest->payloads, i), "cleaning up vector element about to be replaced");
                }
                ast_debug(2, "Copying payload %d (%p) from %p to %p\n", i, type, src, dest);
-               ao2_bump(type);
-               AST_VECTOR_REPLACE(&dest->payloads, i, type);
 
-               if (instance && instance->engine && instance->engine->payload_set) {
+               ao2_bump(type);
+               if (AST_VECTOR_REPLACE(&dest->payloads, i, type)) {
+                       ao2_cleanup(type);
+               } else if (instance && instance->engine && instance->engine->payload_set) {
                        ao2_lock(instance);
                        instance->engine->payload_set(instance, i, type->asterisk_format, type->format, type->rtp_code);
                        ao2_unlock(instance);
@@ -767,9 +768,10 @@ void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct as
        if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
                ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, payload), "cleaning up replaced payload type");
        }
-       AST_VECTOR_REPLACE(&codecs->payloads, payload, new_type);
 
-       if (instance && instance->engine && instance->engine->payload_set) {
+       if (AST_VECTOR_REPLACE(&codecs->payloads, payload, new_type)) {
+               ao2_ref(new_type, -1);
+       } else if (instance && instance->engine && instance->engine->payload_set) {
                ao2_lock(instance);
                instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
                ao2_unlock(instance);
@@ -837,9 +839,10 @@ int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs,
                        /* SDP parsing automatically increases the reference count */
                        new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
                }
-               AST_VECTOR_REPLACE(&codecs->payloads, pt, new_type);
 
-               if (instance && instance->engine && instance->engine->payload_set) {
+               if (AST_VECTOR_REPLACE(&codecs->payloads, pt, new_type)) {
+                       ao2_ref(new_type, -1);
+               } else if (instance && instance->engine && instance->engine->payload_set) {
                        ao2_lock(instance);
                        instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
                        ao2_unlock(instance);
@@ -929,7 +932,11 @@ int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int pay
        if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
                ao2_cleanup(AST_VECTOR_GET(&codecs->payloads, payload));
        }
-       AST_VECTOR_REPLACE(&codecs->payloads, payload, type);
+       if (AST_VECTOR_REPLACE(&codecs->payloads, payload, type)) {
+               ao2_ref(type, -1);
+               ast_rwlock_unlock(&codecs->codecs_lock);
+               return -1;
+       }
        ast_rwlock_unlock(&codecs->codecs_lock);
 
        return 0;