@@ -59,7 +59,7 @@ json_encode_string(
5959 const char * str , size_t str_len ,
6060 bool escape_solidus )
6161{
62- if (!buf || !* buf || ! buf_len || ! str || ! str_len )
62+ if (!buf || !buf_len )
6363 return "json_encode_string: invalid arguments" ;
6464
6565 char hex_buf [7 ];
@@ -69,41 +69,43 @@ json_encode_string(
6969 if (!append_ch (buf , & buf_len , '\"' ))
7070 goto oom ;
7171
72- for (;str_len != 0 ; -- str_len , ++ str ) {
73-
74- const char ch = * str ;
75- const char * escaped = NULL ;
76-
77- switch (ch ) {
78- case '\r' : escaped = "\\r" ; break ;
79- case '\n' : escaped = "\\n" ; break ;
80- case '\\' : escaped = "\\\\" ; break ;
81- /* it is not required to escape a solidus in JSON:
82- * read sec. 2.5: http://www.ietf.org/rfc/rfc4627.txt
83- * specifically, this production from the grammar:
84- * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
85- */
86- case '/' : if (escape_solidus ) escaped = "\\/" ; break ;
87- case '"' : escaped = "\\\"" ; break ;
88- case '\f' : escaped = "\\f" ; break ;
89- case '\b' : escaped = "\\b" ; break ;
90- case '\t' : escaped = "\\t" ; break ;
91- default :
92- if ((unsigned char ) ch < 32 ) {
93- char_to_hex (hex_buf + 4 , ch );
94- escaped = hex_buf ;
72+ if (str ) {
73+ for (;str_len != 0 ; -- str_len , ++ str ) {
74+
75+ const char ch = * str ;
76+ const char * escaped = NULL ;
77+
78+ switch (ch ) {
79+ case '\r' : escaped = "\\r" ; break ;
80+ case '\n' : escaped = "\\n" ; break ;
81+ case '\\' : escaped = "\\\\" ; break ;
82+ /* it is not required to escape a solidus in JSON:
83+ * read sec. 2.5: http://www.ietf.org/rfc/rfc4627.txt
84+ * specifically, this production from the grammar:
85+ * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
86+ */
87+ case '/' : if (escape_solidus ) escaped = "\\/" ; break ;
88+ case '"' : escaped = "\\\"" ; break ;
89+ case '\f' : escaped = "\\f" ; break ;
90+ case '\b' : escaped = "\\b" ; break ;
91+ case '\t' : escaped = "\\t" ; break ;
92+ default :
93+ if ((unsigned char ) ch < 32 ) {
94+ char_to_hex (hex_buf + 4 , ch );
95+ escaped = hex_buf ;
96+ }
97+ break ;
98+ } /* switch */
99+
100+ if (escaped ) {
101+ if (!append_str (buf , & buf_len , escaped , strlen (escaped )))
102+ goto oom ;
103+ } else {
104+ if (!append_ch (buf , & buf_len , ch ))
105+ goto oom ;
95106 }
96- break ;
97- } /* switch */
98-
99- if (escaped ) {
100- if (!append_str (buf , & buf_len , escaped , strlen (escaped )))
101- goto oom ;
102- } else {
103- if (!append_ch (buf , & buf_len , ch ))
104- goto oom ;
105- }
106- } /* for */
107+ } /* for */
108+ } /* if */
107109
108110 if (!append_ch (buf , & buf_len , '\"' ))
109111 goto oom ;
0 commit comments