From 94bb66e168692bb920f276532f71114083ac770b Mon Sep 17 00:00:00 2001 From: Mehmet Akif TASOVA Date: Thu, 21 Feb 2019 18:24:03 +0300 Subject: [PATCH] fix possible leaks in tests/test_string_noalloc.c --- tests/test_string_noalloc.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/test_string_noalloc.c b/tests/test_string_noalloc.c index 6be7458..96640e0 100644 --- a/tests/test_string_noalloc.c +++ b/tests/test_string_noalloc.c @@ -2,6 +2,7 @@ * Tests if binary strings are supported. */ +#include #include #include #include @@ -14,30 +15,38 @@ int main(void) { /* this test has a space after the null character. check that it's still included */ - char *str1 = strdup("This string should be longer than 32 characters"); - char *str2 = strdup("this string is short"); + char *str = strdup("This string should be longer than 32 characters"); struct json_object *jso_str = NULL; - if (!str1 || !str2) + if (!str) { puts("FAIL: strdup() returned NULL"); return 1; } - jso_str = json_object_new_string_noalloc(str1); + jso_str = json_object_new_string_noalloc(str); if (!jso_str) { - puts("FAIL: json_object_new_string_noalloc(str1) returned NULL"); + puts("FAIL: json_object_new_string_noalloc(str) returned NULL"); + free(str); return 2; } json_object_put(jso_str); jso_str = NULL; - jso_str = json_object_new_string_noalloc(str2); + str = strdup("this string is short"); + if (!str) + { + puts("FAIL: strdup() returned NULL"); + return 1; + } + + jso_str = json_object_new_string_noalloc(str); if (!jso_str) { - puts("FAIL: json_object_new_string_noalloc(str2) returned NULL"); + puts("FAIL: json_object_new_string_noalloc(str) returned NULL"); + free(str); return 3; }