From 0a010a59eb9a93da5bb64cd5ba1c69f86ae3f099 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sun, 18 Dec 2016 14:33:41 -0500 Subject: [PATCH] Change a memcpy that should be a memmove within json_pointer_get, and fix memory leaks in of one the json_pointer tests. --- json_pointer.c | 2 +- tests/test_json_pointer.c | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/json_pointer.c b/json_pointer.c index 3648f8d665..380d6e52f8 100644 --- a/json_pointer.c +++ b/json_pointer.c @@ -33,7 +33,7 @@ static void string_replace_all_occurrences_with_char(char *s, const char *occur, *p = repl_char; p++; slen -= skip; - memcpy(p, (p + skip), slen - (p - s) + 1); /* includes null char too */ + memmove(p, (p + skip), slen - (p - s) + 1); /* includes null char too */ } } diff --git a/tests/test_json_pointer.c b/tests/test_json_pointer.c index f42d010402..c3733dea33 100644 --- a/tests/test_json_pointer.c +++ b/tests/test_json_pointer.c @@ -261,15 +261,21 @@ static void test_wrong_inputs_set() printf("PASSED - SET - failed 'cod' with path 'foo/bar'\n"); json_object_put(jo2); - assert(0 != json_pointer_set(&jo1, "/fud/gaw", (jo2 = json_object_new_string("whatever")))); + jo2 = json_object_new_string("whatever"); + assert(0 != json_pointer_set(&jo1, "/fud/gaw", jo2)); assert(0 == json_pointer_set(&jo1, "/fud", json_object_new_object())); assert(0 == json_pointer_set(&jo1, "/fud/gaw", jo2)); /* re-using jo2 from above */ - assert(0 != json_pointer_set(&jo1, "/fud/gaw/0", json_object_new_int(0))); - assert(0 != json_pointer_set(&jo1, "/fud/gaw/", json_object_new_int(0))); + // ownership of jo2 transferred into jo1 + + jo2 = json_object_new_int(0); + assert(0 != json_pointer_set(&jo1, "/fud/gaw/0", jo2)); + json_object_put(jo2); + jo2 = json_object_new_int(0); + assert(0 != json_pointer_set(&jo1, "/fud/gaw/", jo2)); + json_object_put(jo2); printf("PASSED - SET - failed to set index to non-array\n"); json_object_put(jo1); - json_object_put(jo2); } int main(int argc, char **argv)