From: chenha0 Date: Thu, 21 Jan 2016 12:11:40 +0000 (+0800) Subject: Fix issue #221: JSON_C_TO_STRING_NOSLASHESCAPE works incorrectly X-Git-Tag: json-c-0.13-20171207~172^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F222%2Fhead;p=thirdparty%2Fjson-c.git Fix issue #221: JSON_C_TO_STRING_NOSLASHESCAPE works incorrectly Tests added. --- diff --git a/json_object.c b/json_object.c index e611103e..3ee0817f 100644 --- a/json_object.c +++ b/json_object.c @@ -123,6 +123,12 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl case '"': case '\\': case '/': + if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/') + { + pos++; + break; + } + if(pos - start_offset > 0) printbuf_memappend(pb, str + start_offset, pos - start_offset); @@ -133,12 +139,6 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl else if(c == '\f') printbuf_memappend(pb, "\\f", 2); else if(c == '"') printbuf_memappend(pb, "\\\"", 2); else if(c == '\\') printbuf_memappend(pb, "\\\\", 2); - else if(c == '/' && - (flags & JSON_C_TO_STRING_NOSLASHESCAPE)) - { - pos++; - break; - } else if(c == '/') printbuf_memappend(pb, "\\/", 2); start_offset = ++pos; diff --git a/tests/test1.c b/tests/test1.c index 180dedb8..c047c30a 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -63,6 +63,12 @@ int main(int argc, char **argv) printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE)); json_object_put(my_string); + my_string = json_object_new_string("/foo/bar/baz"); + printf("my_string=%s\n", json_object_get_string(my_string)); + printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); + printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE)); + json_object_put(my_string); + my_string = json_object_new_string("foo"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); diff --git a/tests/test1.expected b/tests/test1.expected index c126a475..e9be1f28 100644 --- a/tests/test1.expected +++ b/tests/test1.expected @@ -5,6 +5,9 @@ my_string.to_string()="\\" my_string=/ my_string.to_string()="\/" my_string.to_string(NOSLASHESCAPE)="/" +my_string=/foo/bar/baz +my_string.to_string()="\/foo\/bar\/baz" +my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test1Formatted_plain.expected b/tests/test1Formatted_plain.expected index c011d373..6b93eb72 100644 --- a/tests/test1Formatted_plain.expected +++ b/tests/test1Formatted_plain.expected @@ -5,6 +5,9 @@ my_string.to_string()="\\" my_string=/ my_string.to_string()="\/" my_string.to_string(NOSLASHESCAPE)="/" +my_string=/foo/bar/baz +my_string.to_string()="\/foo\/bar\/baz" +my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test1Formatted_pretty.expected b/tests/test1Formatted_pretty.expected index b57a8e2f..0f94b4c1 100644 --- a/tests/test1Formatted_pretty.expected +++ b/tests/test1Formatted_pretty.expected @@ -5,6 +5,9 @@ my_string.to_string()="\\" my_string=/ my_string.to_string()="\/" my_string.to_string(NOSLASHESCAPE)="/" +my_string=/foo/bar/baz +my_string.to_string()="\/foo\/bar\/baz" +my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz" my_string=foo my_string.to_string()="foo" my_int=9 diff --git a/tests/test1Formatted_spaced.expected b/tests/test1Formatted_spaced.expected index c126a475..e9be1f28 100644 --- a/tests/test1Formatted_spaced.expected +++ b/tests/test1Formatted_spaced.expected @@ -5,6 +5,9 @@ my_string.to_string()="\\" my_string=/ my_string.to_string()="\/" my_string.to_string(NOSLASHESCAPE)="/" +my_string=/foo/bar/baz +my_string.to_string()="\/foo\/bar\/baz" +my_string.to_string(NOSLASHESCAPE)="/foo/bar/baz" my_string=foo my_string.to_string()="foo" my_int=9