Commit ba18f8f1 authored by Ghislain MARY's avatar Ghislain MARY

Fix bug in body handler marshaling that could cause body to be truncated.

parent 0d11b6ec
......@@ -491,18 +491,19 @@ static belle_sip_error_code checked_marshal(belle_sip_object_vptr_t *vptr, belle
if (p[i]=='\0') break;
}
written=i-initial_offset;
if (error==BELLE_SIP_BUFFER_OVERFLOW){
if (error==BELLE_SIP_OK){
if (written!=(*offset-initial_offset) && written!=(buff_size-initial_offset-1)){ /*this is because snprintf won't allow you to write a non null character at the end of the buffer*/
belle_sip_fatal("Object of type %s marshalled %i bytes but said it marshalled %i bytes !",
vptr->type_name,(int)written,(int)(*offset-initial_offset));
}
memcpy(buff+initial_offset,p+initial_offset,*offset-initial_offset);
}else if (error==BELLE_SIP_BUFFER_OVERFLOW){
belle_sip_error("Object of type %s commited a buffer overflow by marshalling %i bytes",
vptr->type_name,(int)(*offset-initial_offset));
} else if (error!=BELLE_SIP_OK){
}else{
belle_sip_error("Object of type %s produced an error during marshalling: %i",
vptr->type_name,error);
}
if (written!=(*offset-initial_offset) && written!=(buff_size-initial_offset-1)){ /*this is because snprintf won't allow you to write a non null character at the end of the buffer*/
belle_sip_fatal("Object of type %s marshalled %i bytes but said it marshalled %i bytes !",
vptr->type_name,(int)written,(int)(*offset-initial_offset));
}
memcpy(buff+initial_offset,p+initial_offset,*offset-initial_offset);
}
belle_sip_free(p);
return error;
}
......
......@@ -60,7 +60,7 @@ static belle_sip_error_code belle_sip_body_handler_marshal(belle_sip_body_handle
ret = belle_sip_body_handler_send_chunk(obj, NULL, (uint8_t*)buff + *offset, &len);
*offset += len;
} while ((ret == BELLE_SIP_CONTINUE) && (len > 0));
if (ret == BELLE_SIP_CONTINUE) return BELLE_SIP_BUFFER_OVERFLOW;
if ((ret == BELLE_SIP_CONTINUE) || (ret == BELLE_SIP_BUFFER_OVERFLOW)) return BELLE_SIP_BUFFER_OVERFLOW;
if (ret == BELLE_SIP_STOP) belle_sip_body_handler_end_transfer(obj);
return BELLE_SIP_OK;
}
......@@ -138,11 +138,13 @@ void belle_sip_body_handler_recv_chunk(belle_sip_body_handler_t *obj, belle_sip_
int belle_sip_body_handler_send_chunk(belle_sip_body_handler_t *obj, belle_sip_message_t *msg, uint8_t *buf, size_t *size){
int ret;
size_t to_send = *size;
if (obj->expected_size!=0){
*size=MIN(*size,obj->expected_size-obj->transfered_size);
to_send=MIN(*size,obj->expected_size-obj->transfered_size);
}
ret=BELLE_SIP_OBJECT_VPTR(obj,belle_sip_body_handler_t)->chunk_send(obj,msg,obj->transfered_size,buf,size);
obj->transfered_size+=*size;
ret=BELLE_SIP_OBJECT_VPTR(obj,belle_sip_body_handler_t)->chunk_send(obj,msg,obj->transfered_size,buf,&to_send);
obj->transfered_size+=to_send;
*size=to_send;
update_progress(obj,msg);
if (obj->expected_size!=0){
if (obj->transfered_size==obj->expected_size)
......@@ -569,7 +571,6 @@ static void belle_sip_multipart_body_handler_recv_chunk(belle_sip_body_handler_t
static int belle_sip_multipart_body_handler_send_chunk(belle_sip_body_handler_t *obj, belle_sip_message_t *msg, size_t offset,
uint8_t *buffer, size_t *size){
belle_sip_multipart_body_handler_t *obj_multipart=(belle_sip_multipart_body_handler_t*)obj;
if (obj_multipart->transfer_current_part->data) { /* we have a part, get its content from handler */
......
......@@ -202,7 +202,6 @@ static void on_progress(belle_sip_body_handler_t *bh, belle_sip_message_t *msg,
}else belle_sip_message("%i bytes transfered",(int)offset);
}
#define MULTIPART_BOUNDARY "---------------------------14737809831466499882746641449"
#define MULTIPART_BEGIN "somehash.jpg\r\n" \
"--" MULTIPART_BOUNDARY "\r\n" \
"Content-Disposition: form-data; name=\"userfile\"; filename=\"belle_http_sip_tester.jpg\"\r\n" \
......
......@@ -186,10 +186,134 @@ static void test_dictionary(void)
belle_sip_object_unref(obj);
}
static const char *parts_id[] = {
"6h-Yqv3@sip.linphonecyberattack.fr",
"Azgwg6Q@sip.linphonecyberattack.fr",
"0NKY7am@sip.linphonecyberattack.fr",
"p8AxOjL@sip.linphonecyberattack.fr",
"HqquRnL@sip.linphonecyberattack.fr",
"ftVeEXj@sip.linphonecyberattack.fr",
"3OQQ~t4@sip.linphonecyberattack.fr"
};
static const char *parts_content[] = {
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<list xmlns=\"urn:ietf:params:xml:ns:rlmi\" fullState=\"false\" uri=\"sip:rls@sip.linphonecyberattack.fr\" version=\"1\">\n"
" <resource uri=\"sip:francois@sip.linphonecyberattack.fr\">\n"
" <instance cid=\"Azgwg6Q@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
" </resource>\n"
" <resource uri=\"sip:simon@sip.linphonecyberattack.fr\">\n"
" <instance cid=\"0NKY7am@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
" </resource>\n"
" <resource uri=\"sip:gautier@sip.linphonecyberattack.fr\">\n"
" <instance cid=\"p8AxOjL@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
" </resource>\n"
" <resource uri=\"sip:margaux@sip.linphonecyberattack.fr\">\n"
" <instance cid=\"HqquRnL@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
" </resource>\n"
" <resource uri=\"sip:marielle@sip.linphonecyberattack.fr\">\n"
" <instance cid=\"ftVeEXj@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
" </resource>\n"
" <resource uri=\"sip:sylvain@sip.linphonecyberattack.fr\">\n"
" <instance cid=\"3OQQ~t4@sip.linphonecyberattack.fr\" id=\"1\" state=\"active\"/>\n"
" </resource>\n"
"</list>\n",
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:francois@sip.linphonecyberattack.fr\">\n"
" <tuple id=\"pfaa79\">\n"
" <status>\n"
" <basic>open</basic>\n"
" </status>\n"
" <contact>sip:francois@sip.linphonecyberattack.fr</contact>\n"
" <timestamp>2016-06-13T16:12:48</timestamp>\n"
" </tuple>\n"
"</presence>",
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:simon@sip.linphonecyberattack.fr\">\n"
" <tuple id=\"d-0p-1\">\n"
" <status>\n"
" <basic>open</basic>\n"
" </status>\n"
" <contact>sip:simon@sip.linphonecyberattack.fr</contact>\n"
" <timestamp>2016-06-13T16:12:48</timestamp>\n"
" </tuple>\n"
"</presence>",
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:gautier@sip.linphonecyberattack.fr\">\n"
" <tuple id=\"fz3ywt\">\n"
" <status>\n"
" <basic>open</basic>\n"
" </status>\n"
" <contact>sip:gautier@sip.linphonecyberattack.fr</contact>\n"
" <timestamp>2016-06-13T16:12:48</timestamp>\n"
" </tuple>\n"
"</presence>",
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:margaux@sip.linphonecyberattack.fr\">\n"
" <tuple id=\"_p7ttd\">\n"
" <status>\n"
" <basic>open</basic>\n"
" </status>\n"
" <contact>sip:margaux@sip.linphonecyberattack.fr</contact>\n"
" <timestamp>2016-06-13T16:12:48</timestamp>\n"
" </tuple>\n"
"</presence>",
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:marielle@sip.linphonecyberattack.fr\">\n"
" <tuple id=\"bro6yr\">\n"
" <status>\n"
" <basic>open</basic>\n"
" </status>\n"
" <contact>sip:marielle@sip.linphonecyberattack.fr</contact>\n"
" <timestamp>2016-06-13T16:12:48</timestamp>\n"
" </tuple>\n"
"</presence>",
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n"
"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" entity=\"sip:sylvain@sip.linphonecyberattack.fr\">\n"
" <tuple id=\"bhpk9z\">\n"
" <status>\n"
" <basic>open</basic>\n"
" </status>\n"
" <contact>sip:sylvain@sip.linphonecyberattack.fr</contact>\n"
" <timestamp>2016-06-13T16:12:48</timestamp>\n"
" </tuple>\n"
"</presence>"
};
static void test_presence_marshal(void) {
char *desc;
unsigned int i;
belle_sip_memory_body_handler_t *mbh;
belle_sip_multipart_body_handler_t *mpbh = belle_sip_multipart_body_handler_new(NULL, NULL, NULL, MULTIPART_BOUNDARY);
belle_sip_object_ref(mpbh);
for (i = 0; i < 7; i++) {
belle_sip_header_t *content_transfer_encoding = belle_sip_header_create("Content-Transfer-Encoding", "binary");
belle_sip_header_t *content_id = belle_sip_header_create("Content-Id", parts_id[i]);
belle_sip_header_t *content_type;
if (i == 0) {
content_type = BELLE_SIP_HEADER(belle_sip_header_content_type_create("application", "rlmi+xml; charset=\"UTF-8\""));
} else {
content_type = BELLE_SIP_HEADER(belle_sip_header_content_type_create("application", "pidf+xml; charset=\"UTF-8\""));
}
mbh = belle_sip_memory_body_handler_new_copy_from_buffer((void *)parts_content[i], strlen(parts_content[i]), NULL, NULL);
belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_transfer_encoding);
belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_id);
belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(mbh), content_type);
belle_sip_multipart_body_handler_add_part(mpbh, BELLE_SIP_BODY_HANDLER(mbh));
}
desc = belle_sip_object_to_string(mpbh);
BC_ASSERT_PTR_NOT_NULL(desc);
if (desc != NULL) {
BC_ASSERT_EQUAL((unsigned int)strlen(desc), 4676, unsigned int, "%u");
}
belle_sip_object_unref(mpbh);
}
test_t core_tests[] = {
{ "Object Data", test_object_data },
{ "Dictionary", test_dictionary }
{ "Dictionary", test_dictionary },
{ "Presence marshal", test_presence_marshal }
};
test_suite_t core_test_suite = {"Core", NULL, NULL, belle_sip_tester_before_each, belle_sip_tester_after_each,
......
......@@ -21,6 +21,10 @@
#include <bctoolbox/tester.h>
#define MULTIPART_BOUNDARY "---------------------------14737809831466499882746641449"
#ifdef __cplusplus
extern "C" {
#endif
......
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