From 5154c447726519c21c3998b5ab027e28008d77d9 Mon Sep 17 00:00:00 2001 From: Botond Baranyi <botond.baranyi@ericsson.com> Date: Thu, 22 Mar 2018 14:37:44 +0100 Subject: [PATCH] Fixed memory leaks in pre-defined functions Change-Id: Ic3a0c183b604b44e7fe652ab77d77d85154c30ba Signed-off-by: Botond Baranyi <botond.baranyi@ericsson.com> --- compiler2/union.c | 8 ++- core/Addfunc.cc | 70 +++++++++++++++------- regression_test/predefFunction2/.gitignore | 5 ++ 3 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 regression_test/predefFunction2/.gitignore diff --git a/compiler2/union.c b/compiler2/union.c index e4f0603e0..d7de465a1 100644 --- a/compiler2/union.c +++ b/compiler2/union.c @@ -2257,7 +2257,13 @@ void defUnionClass(struct_def const *sdef, output_struct *output) src = mputstr(src, " char* literal_str = mprintf(\"literal (%s)\",\n" " (JSON_TOKEN_LITERAL_TRUE == j_token) ? \"true\" : \"false\");\n" - " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_AS_VALUE_ERROR, literal_str);\n" + " try {\n" + " JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG, JSON_DEC_AS_VALUE_ERROR, literal_str);\n" + " }\n" + " catch (const TC_Error&) {\n" + " Free(literal_str);\n" + " throw;\n" + " }\n" " Free(literal_str);\n" " clean_up();\n" " return JSON_ERROR_FATAL;\n" diff --git a/core/Addfunc.cc b/core/Addfunc.cc index 8e3eaade1..c41f59b49 100644 --- a/core/Addfunc.cc +++ b/core/Addfunc.cc @@ -269,9 +269,14 @@ CHARSTRING int2char(const INTEGER& value) const int_val_t& ivt = value.get_val(); if (ivt < 0 || ivt > 127) { char *value_str = ivt.as_string(); - TTCN_error("The argument of function int2char() is %s, " - "which is outside the allowed range 0 .. 127.", value_str); - Free(value_str); + try { + TTCN_error("The argument of function int2char() is %s, " + "which is outside the allowed range 0 .. 127.", value_str); + } + catch (const TC_Error&) { + Free(value_str); + throw; + } } return CHARSTRING((char)((int)value)); } @@ -294,9 +299,14 @@ UNIVERSAL_CHARSTRING int2unichar(const INTEGER& value) const int_val_t& ivt = value.get_val(); if (ivt < 0 || ivt > 2147483647) { char *value_str = ivt.as_string(); - TTCN_error("The argument of function int2unichar() is %s, " - "which outside the allowed range 0 .. 2147483647.", value_str); - Free(value_str); + try { + TTCN_error("The argument of function int2unichar() is %s, " + "which outside the allowed range 0 .. 2147483647.", value_str); + } + catch (const TC_Error&) { + Free(value_str); + throw; + } } return int2unichar((int)value); } @@ -310,9 +320,14 @@ BITSTRING int2bit(const INTEGER& value, int length) int_val_t tmp_value(value.get_val()); if (tmp_value < 0) { char *value_str = tmp_value.as_string(); - TTCN_error("The first argument (value) of function " - "int2bit() is a negative integer value: %s.", value_str); - Free(value_str); + try { + TTCN_error("The first argument (value) of function " + "int2bit() is a negative integer value: %s.", value_str); + } + catch (const TC_Error&) { + Free(value_str); + throw; + } } if (length < 0) TTCN_error("The second argument (length) of function " "int2bit() is a negative integer value: %d.", length); @@ -333,10 +348,15 @@ BITSTRING int2bit(const INTEGER& value, int length) i++; } char *value_str = value.get_val().as_string(); // not tmp_value! - TTCN_error("The first argument of function int2bit(), which is %s, " - "does not fit in %d bit%s, needs at least %d.", value_str, length, - length > 1 ? "s" :"", length + i); - Free(value_str); + try { + TTCN_error("The first argument of function int2bit(), which is %s, " + "does not fit in %d bit%s, needs at least %d.", value_str, length, + length > 1 ? "s" :"", length + i); + } + catch (const TC_Error&) { + Free(value_str); + throw; + } } return ret_val; } @@ -371,9 +391,14 @@ HEXSTRING int2hex(const INTEGER& value, int length) int_val_t tmp_value(value.get_val()); if (value < 0) { char *value_str = tmp_value.as_string(); - TTCN_error("The first argument (value) of function int2hex() is a " - "negative integer value: %s.", value_str); - Free(value_str); + try { + TTCN_error("The first argument (value) of function int2hex() is a " + "negative integer value: %s.", value_str); + } + catch (const TC_Error&) { + Free(value_str); + throw; + } } if (length < 0) TTCN_error("The second argument (length) of function " "int2hex() is a negative integer value: %d.", length); @@ -388,10 +413,15 @@ HEXSTRING int2hex(const INTEGER& value, int length) } if (tmp_value != 0) { char *value_str = value.get_val().as_string(); // not tmp_value! - TTCN_error("The first argument of function int2hex(), which is %s, " - "does not fit in %d hexadecimal digit%s.", value_str, length, - length > 1 ? "s" :""); - Free(value_str); // ??? + try { + TTCN_error("The first argument of function int2hex(), which is %s, " + "does not fit in %d hexadecimal digit%s.", value_str, length, + length > 1 ? "s" :""); + } + catch (const TC_Error&) { + Free(value_str); + throw; + } } return ret_val; } diff --git a/regression_test/predefFunction2/.gitignore b/regression_test/predefFunction2/.gitignore new file mode 100644 index 000000000..b904ff4e3 --- /dev/null +++ b/regression_test/predefFunction2/.gitignore @@ -0,0 +1,5 @@ +predefFunctTest +predefFunctTest.exe +predefFunctTest*.cc +predefFunctTest*.hh +predefFunctTest*.log -- GitLab