Browse files

Fix issue #53 - ensure explicit length string are still NUL terminate…

…d, 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
  • Loading branch information...
1 parent 7a4506d commit 4e4af93d667ae0d3cb9779f5a3c3f964cc9d7d81 @hawicz hawicz committed Dec 9, 2012
Showing with 23 additions and 2 deletions.
  1. +2 −1 json_object.c
  2. +1 −1 json_tokener.c
  3. +20 −0 tests/test_null.c
View
3 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;
}
View
2 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;
View
20 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;
}

0 comments on commit 4e4af93

Please sign in to comment.