From ee209023b86b80bc2b70ff4877e85f5f544e3086 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sat, 4 May 2013 19:17:27 +0200 Subject: [PATCH] fix memleaks avoid memleak if memory is full fix "memleak" at the end of mbus_parse_hex --- mbus/mbus-protocol-aux.c | 10 ++++++++-- mbus/mbus-protocol.c | 42 ++++++++++++++++++++++++++++------------ test/mbus_parse_hex.c | 2 +- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 04f0894..d28659b 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1319,10 +1319,16 @@ mbus_data_variable_xml_normalized(mbus_data_variable *data) if ((buff_size - len) < 1024) { buff_size *= 2; - buff = (char*) realloc(buff,buff_size); + new_buff = (char*) realloc(buff,buff_size); - if (buff == NULL) + if (new_buff == NULL) + { + mbus_record_free(norm_record); + free(buff); return NULL; + } + + buff = new_buff; } len += snprintf(&buff[len], buff_size - len, " \n", i); diff --git a/mbus/mbus-protocol.c b/mbus/mbus-protocol.c index d137259..bd63cc9 100755 --- a/mbus/mbus-protocol.c +++ b/mbus/mbus-protocol.c @@ -2658,7 +2658,7 @@ mbus_data_fixed_parse(mbus_frame *frame, mbus_data_fixed *data) int mbus_data_variable_parse(mbus_frame *frame, mbus_data_variable *data) { - mbus_data_record *record; + mbus_data_record *record = NULL; size_t i, j; if (frame && data) @@ -3520,7 +3520,7 @@ char * mbus_data_variable_xml(mbus_data_variable *data) { mbus_data_record *record; - char *buff = NULL; + char *buff = NULL, *new_buff; size_t len = 0, buff_size = 8192; int i; @@ -3541,10 +3541,15 @@ mbus_data_variable_xml(mbus_data_variable *data) if ((buff_size - len) < 1024) { buff_size *= 2; - buff = (char*) realloc(buff,buff_size); + new_buff = (char*) realloc(buff,buff_size); - if (buff == NULL) + if (new_buff == NULL) + { + free(buff); return NULL; + } + + buff = new_buff; } len += snprintf(&buff[len], buff_size - len, "%s", @@ -3696,7 +3701,7 @@ mbus_frame_xml(mbus_frame *frame) mbus_frame *iter; mbus_data_record *record; - char *buff = NULL; + char *buff = NULL, *new_buff; size_t len = 0, buff_size = 8192; int record_cnt = 0, frame_cnt; @@ -3736,7 +3741,10 @@ mbus_frame_xml(mbus_frame *frame) buff = (char*) malloc(buff_size); if (buff == NULL) - return NULL; + { + mbus_data_record_free(frame_data.data_var.record); + return NULL; + } // include frame counter in XML output if more than one frame // is available (frame_cnt = -1 => not included in output) @@ -3757,10 +3765,16 @@ mbus_frame_xml(mbus_frame *frame) if ((buff_size - len) < 1024) { buff_size *= 2; - buff = (char*) realloc(buff,buff_size); - - if (buff == NULL) + new_buff = (char*) realloc(buff,buff_size); + + if (new_buff == NULL) + { + free(buff); + mbus_data_record_free(frame_data.data_var.record); return NULL; + } + + buff = new_buff; } len += snprintf(&buff[len], buff_size - len, "%s", @@ -3790,10 +3804,14 @@ mbus_frame_xml(mbus_frame *frame) if ((buff_size - len) < 1024) { buff_size *= 2; - buff = (char*) realloc(buff,buff_size); - - if (buff == NULL) + if (new_buff == NULL) + { + free(buff); + mbus_data_record_free(frame_data.data_var.record); return NULL; + } + + buff = new_buff; } len += snprintf(&buff[len], buff_size - len, "%s", diff --git a/test/mbus_parse_hex.c b/test/mbus_parse_hex.c index fcaa0b5..2bf6180 100644 --- a/test/mbus_parse_hex.c +++ b/test/mbus_parse_hex.c @@ -80,7 +80,7 @@ main(int argc, char *argv[]) } printf("%s", xml_result); free(xml_result); - + mbus_data_record_free(frame_data.data_var.record); return 0; }