Skip to content

If a string is not valid UTF-8, treat it as ASCII rather than giving up and serializing 'null' #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions ext/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)

#define REVERSE16(us) (((us & 0xf) << 12) | (((us >> 4) & 0xf) << 8) | (((us >> 8) & 0xf) << 4) | ((us >> 12) & 0xf))

static int ascii_to_utf16(unsigned short* w, char* s, int len)
{
int i;
for (i = 0; i < len; i++) {
w[i] = s[i] & 0xff;
}
return len;
}

static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
{
int pos = 0, ulen = 0;
Expand Down Expand Up @@ -383,19 +392,18 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR

utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
ulen = utf8_to_utf16(utf16, s, len);
if (ulen <= 0) {
if (ulen < 0) {
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
if (!PG(display_errors)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
}
ulen = ascii_to_utf16(utf16, s, len);
}
if (ulen == 0) {
if (utf16) {
efree(utf16);
}
if (ulen < 0) {
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
if (!PG(display_errors)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
}
smart_str_appendl(buf, "null", 4);
} else {
smart_str_appendl(buf, "\"\"", 2);
}
smart_str_appendl(buf, "\"\"", 2);
return;
}
if (!(options & PHP_JSON_UNESCAPED_UNICODE)) {
Expand Down