Skip to content

Commit 4877a2c

Browse files
committed
#53 - fix
1 parent 623794c commit 4877a2c

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

src/json_encoders.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

test/test.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ function issue_44(request)
8888
return c.a.b
8989
end
9090
-- ]]
91+
92+
function four_empty_strings()
93+
return {"", "", "", "", {a = ""}}
94+
end

test/v23_features.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,10 @@
117117
result = post_success(preset_method_location, {
118118
'method':'echo_2', 'params':[data], 'id': 1}, {})
119119
assert(data == result[0]), 'not equal'
120+
121+
#===========
122+
#
123+
print('[+] Empty string')
124+
preset_method_location = BASE_URL + '/tnt'
125+
post_success(preset_method_location, {
126+
'method':'four_empty_strings', 'params':[], 'id': 1}, {})

0 commit comments

Comments
 (0)