Skip to content

Commit

Permalink
Core: improved JSON escaping.
Browse files Browse the repository at this point in the history
Two-character representations are now used for \b, \f, \n, \r, and \t.
  • Loading branch information
VBart committed Apr 12, 2017
1 parent cac3617 commit 97cb303
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/core/ngx_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,19 @@ ngx_escape_json(u_char *dst, u_char *src, size_t size)
len++;

} else if (ch <= 0x1f) {
len += sizeof("\\u001F") - 2;

switch (ch) {
case '\n':
case '\r':
case '\t':
case '\b':
case '\f':
len++;
break;

default:
len += sizeof("\\u001F") - 2;
}
}

size--;
Expand All @@ -1829,12 +1841,37 @@ ngx_escape_json(u_char *dst, u_char *src, size_t size)
*dst++ = ch;

} else {
*dst++ = '\\'; *dst++ = 'u'; *dst++ = '0'; *dst++ = '0';
*dst++ = '0' + (ch >> 4);
*dst++ = '\\';

switch (ch) {
case '\n':
*dst++ = 'n';
break;

case '\r':
*dst++ = 'r';
break;

ch &= 0xf;
case '\t':
*dst++ = 't';
break;

case '\b':
*dst++ = 'b';
break;

case '\f':
*dst++ = 'f';
break;

*dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10);
default:
*dst++ = 'u'; *dst++ = '0'; *dst++ = '0';
*dst++ = '0' + (ch >> 4);

ch &= 0xf;

*dst++ = (ch < 10) ? ('0' + ch) : ('A' + ch - 10);
}
}

size--;
Expand Down

0 comments on commit 97cb303

Please sign in to comment.