Commit dc3bdd6b authored by Ronan's avatar Ronan

fix(message): avoid one leak in belle_sip_message_get_body when multipart is used

parent cbb4ab4c
......@@ -629,6 +629,8 @@ struct _belle_sip_message {
belle_sip_object_t base;
belle_sip_list_t* header_list;
belle_sip_body_handler_t *body_handler;
char *multipart_body_cache;
};
struct _belle_sip_request {
......
/*
belle-sip - SIP (RFC3261) library.
Copyright (C) 2010 Belledonne Communications SARL
Copyright (C) 2010-2018 Belledonne Communications SARL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "grammars/belle_sip_messageLexer.h"
#include "grammars/belle_sip_messageParser.h"
......@@ -82,7 +82,7 @@ static const char * expand_name(const char *name){
static headers_container_t* belle_sip_message_headers_container_new(const char* name) {
headers_container_t* headers_container = belle_sip_new0(headers_container_t);
headers_container->name = belle_sip_strdup(expand_name(name));
return headers_container;
return headers_container;
}
static void belle_sip_headers_container_delete(headers_container_t *obj){
......@@ -95,6 +95,8 @@ static void belle_sip_message_destroy(belle_sip_message_t *msg){
belle_sip_list_free_with_data(msg->header_list,(void (*)(void*))belle_sip_headers_container_delete);
if (msg->body_handler)
belle_sip_object_unref(msg->body_handler);
if (msg->multipart_body_cache)
bctbx_free(msg->multipart_body_cache);
}
/*very sub-optimal clone method */
......@@ -502,9 +504,11 @@ void belle_sip_message_set_body_handler(belle_sip_message_t *msg, belle_sip_body
}
}
if (first_part_content_type) {
char *type_slash_subtype = belle_sip_strdup_printf("%s/%s"
, belle_sip_header_content_type_get_type(first_part_content_type)
, belle_sip_header_content_type_get_subtype(first_part_content_type));
char *type_slash_subtype = belle_sip_strdup_printf(
"%s/%s",
belle_sip_header_content_type_get_type(first_part_content_type),
belle_sip_header_content_type_get_subtype(first_part_content_type)
);
belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(content_type), "type", type_slash_subtype);
belle_sip_free(type_slash_subtype);
} else {
......@@ -549,17 +553,35 @@ void belle_sip_message_set_body_handler(belle_sip_message_t *msg, belle_sip_body
SET_OBJECT_PROPERTY(msg,body_handler,body_handler);
}
const char* belle_sip_message_get_body(belle_sip_message_t *msg) {
if (msg->body_handler==NULL) return NULL;
if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(msg->body_handler, belle_sip_memory_body_handler_t)){
return (const char*)belle_sip_memory_body_handler_get_buffer(
static const char *get_multipart_body (belle_sip_message_t *msg) {
if (!BELLE_SIP_OBJECT_IS_INSTANCE_OF(msg->body_handler, belle_sip_multipart_body_handler_t))
return NULL;
char *old_body = msg->multipart_body_cache;
char *body = belle_sip_object_to_string(msg->body_handler);
if (old_body) {
// Returns the same pointer if no changes.
if (!strcmp(body, old_body)) {
bctbx_free(body);
return old_body;
}
bctbx_free(msg->multipart_body_cache);
}
msg->multipart_body_cache = body;
return body;
}
const char *belle_sip_message_get_body(belle_sip_message_t *msg) {
if (!msg->body_handler) return NULL;
if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(msg->body_handler, belle_sip_memory_body_handler_t)) {
return (const char *)belle_sip_memory_body_handler_get_buffer(
BELLE_SIP_MEMORY_BODY_HANDLER(msg->body_handler)
);
} else if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(msg->body_handler, belle_sip_multipart_body_handler_t)) {
return belle_sip_object_to_string(msg->body_handler);
}
belle_sip_error("belle_sip_message_get_body(): body cannot be returned as pointer.");
return NULL;
const char *body = get_multipart_body(msg);
if (!body)
belle_sip_error("belle_sip_message_get_body(): body cannot be returned as pointer.");
return body;
}
size_t belle_sip_message_get_body_size(const belle_sip_message_t *msg){
......@@ -709,14 +731,16 @@ static int is_authorized_uri_header(const char* header_name) {
}
belle_sip_request_t* belle_sip_request_create(belle_sip_uri_t *requri, const char* method,
belle_sip_header_call_id_t *callid,
belle_sip_header_cseq_t * cseq,
belle_sip_header_from_t *from,
belle_sip_header_to_t *to,
belle_sip_header_via_t *via,
int max_forward)
{
belle_sip_request_t* belle_sip_request_create (
belle_sip_uri_t *requri,
const char* method,
belle_sip_header_call_id_t *callid,
belle_sip_header_cseq_t * cseq,
belle_sip_header_from_t *from,
belle_sip_header_to_t *to,
belle_sip_header_via_t *via,
int max_forward
) {
belle_sip_request_t *ret=belle_sip_request_new();
belle_sip_header_max_forwards_t *mf=belle_sip_header_max_forwards_new();
belle_sip_list_t* iterator;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment