Support \u0000 - add size_t string lengths to API, load and dump \u000, etc.

Also:
  Steal strings during parsing for half the mallocs!
  Change all input-caused assertions to errors.  No crashes please, we're programmers.
This commit is contained in:
Chip Salzenberg
2012-04-03 18:00:29 -07:00
parent e4d6a9f6f4
commit 9c259c07aa
15 changed files with 283 additions and 122 deletions

View File

@@ -83,7 +83,7 @@ static void run_tests()
fail("json_pack string refcount failed");
json_decref(value);
/* string and length */
/* string and length (int) */
value = json_pack("s#", "test asdf", 4);
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string and length failed");
@@ -91,14 +91,30 @@ static void run_tests()
fail("json_pack string and length refcount failed");
json_decref(value);
/* string and length, non-NUL terminated string */
value = json_pack("s#", buffer, 4);
/* string and length (size_t) */
value = json_pack("s%", "test asdf", (size_t)4);
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string and length failed");
if(value->refcount != (size_t)1)
fail("json_pack string and length refcount failed");
json_decref(value);
/* string and length (int), non-NUL terminated string */
value = json_pack("s#", buffer, 4);
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string and length (int) failed");
if(value->refcount != (size_t)1)
fail("json_pack string and length (int) refcount failed");
json_decref(value);
/* string and length (size_t), non-NUL terminated string */
value = json_pack("s%", buffer, (size_t)4);
if(!json_is_string(value) || strcmp("test", json_string_value(value)))
fail("json_pack string and length (size_t) failed");
if(value->refcount != (size_t)1)
fail("json_pack string and length (size_t) refcount failed");
json_decref(value);
/* string concatenation */
value = json_pack("s++", "te", "st", "ing");
if(!json_is_string(value) || strcmp("testing", json_string_value(value)))
@@ -107,12 +123,20 @@ static void run_tests()
fail("json_pack string concatenation refcount failed");
json_decref(value);
/* string concatenation and length */
/* string concatenation and length (int) */
value = json_pack("s#+#+", "test", 1, "test", 2, "test");
if(!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
fail("json_pack string concatenation and length failed");
fail("json_pack string concatenation and length (int) failed");
if(value->refcount != (size_t)1)
fail("json_pack string concatenation and length refcount failed");
fail("json_pack string concatenation and length (int) refcount failed");
json_decref(value);
/* string concatenation and length (size_t) */
value = json_pack("s%+%+", "test", (size_t)1, "test", (size_t)2, "test");
if(!json_is_string(value) || strcmp("ttetest", json_string_value(value)))
fail("json_pack string concatenation and length (size_t) failed");
if(value->refcount != (size_t)1)
fail("json_pack string concatenation and length (size_t) refcount failed");
json_decref(value);
/* empty object */

View File

@@ -17,6 +17,7 @@ static void run_tests()
int i1, i2, i3;
json_int_t I1;
int rv;
size_t z;
double f;
char *s;
@@ -81,6 +82,13 @@ static void run_tests()
fail("json_unpack string failed");
json_decref(j);
/* string with length (size_t) */
j = json_string("foo");
rv = json_unpack(j, "s%", &s, &z);
if(rv || strcmp(s, "foo") || z != 3)
fail("json_unpack string with length (size_t) failed");
json_decref(j);
/* empty object */
j = json_object();
if(json_unpack(j, "{}"))

View File

@@ -1,2 +0,0 @@
1 33 33
\u0000 is not allowed

View File

@@ -1 +0,0 @@
["\u0000 (null byte not allowed)"]

View File

@@ -0,0 +1 @@
["null char \u0000 in string"]

View File

@@ -0,0 +1 @@
["null char \u0000 in string"]