From 4e4af93d667ae0d3cb9779f5a3c3f964cc9d7d81 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sun, 9 Dec 2012 16:32:11 -0600 Subject: [PATCH] Fix issue #53 - ensure explicit length string are still NUL terminated, and fix json_tokener_parse() to work properly with embedded unicode \u0000 values in strings. Adjust test_null to check for this case. See also http://bugs.debian.org/687269 --- json_object.c | 3 ++- json_tokener.c | 2 +- tests/test_null.c | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/json_object.c b/json_object.c index 5b60a06..a84785c 100644 --- a/json_object.c +++ b/json_object.c @@ -620,8 +620,9 @@ struct json_object* json_object_new_string_len(const char *s, int len) if(!jso) return NULL; jso->_delete = &json_object_string_delete; jso->_to_json_string = &json_object_string_to_json_string; - jso->o.c_string.str = (char*)malloc(len); + jso->o.c_string.str = (char*)malloc(len + 1); memcpy(jso->o.c_string.str, (void *)s, len); + jso->o.c_string.str[len] = '\0'; jso->o.c_string.len = len; return jso; } diff --git a/json_tokener.c b/json_tokener.c index f5fa8d6..05357fb 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -393,7 +393,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok, while(1) { if(c == tok->quote_char) { printbuf_memappend_fast(tok->pb, case_start, str-case_start); - current = json_object_new_string(tok->pb->buf); + current = json_object_new_string_len(tok->pb->buf, tok->pb->bpos); saved_state = json_tokener_state_finish; state = json_tokener_state_eatws; break; diff --git a/tests/test_null.c b/tests/test_null.c index 675eab5..1f07910 100644 --- a/tests/test_null.c +++ b/tests/test_null.c @@ -8,6 +8,7 @@ #include "json_inttypes.h" #include "json_object.h" +#include "json_tokener.h" int main() { @@ -33,5 +34,24 @@ int main() retval=1; } json_object_put(string); + + struct json_object *parsed_str = json_tokener_parse(expected); + if (parsed_str) + { + int parsed_len = json_object_get_string_len(parsed_str); + const char *parsed_cstr = json_object_get_string(parsed_str); + int ii; + printf("Re-parsed object string len=%d, chars=[", parsed_len); + for (ii = 0; ii < parsed_len ; ii++) + { + printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]); + } + printf("]\n"); + json_object_put(parsed_str); + } + else + { + printf("ERROR: failed to parse\n"); + } return retval; } -- 2.7.4