Skip to content

Commit 3f3ad30

Browse files
author
Adam Harvey
committed
Fix bug #61537 (json_encode() incorrectly truncates/discards information) and
remove a test case that's now mooted by this fix.
1 parent 1c8fccd commit 3f3ad30

File tree

6 files changed

+49
-25
lines changed

6 files changed

+49
-25
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2012, PHP 5.3.12
44

5+
- JSON
6+
. Fixed bug #61537 (json_encode() incorrectly truncates/discards
7+
information). (Adam)
8+
59
?? ??? 2012, PHP 5.3.11
610
- Iconv extension:
711
. Fixed a bug that iconv extension fails to link to the correct library

ext/json/json.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static PHP_MINIT_FUNCTION(json)
7373
REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | CONST_PERSISTENT);
7474
REGISTER_LONG_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT);
7575
REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, CONST_CS | CONST_PERSISTENT);
76+
REGISTER_LONG_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT);
7677

7778
REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
7879
REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
@@ -320,9 +321,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
320321
}
321322
if (len < 0) {
322323
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
323-
if (!PG(display_errors)) {
324-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
325-
}
324+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
326325
smart_str_appendl(buf, "null", 4);
327326
} else {
328327
smart_str_appendl(buf, "\"\"", 2);
@@ -571,7 +570,11 @@ static PHP_FUNCTION(json_encode)
571570

572571
php_json_encode(&buf, parameter, options TSRMLS_CC);
573572

574-
ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
573+
if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && options ^ PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
574+
ZVAL_FALSE(return_value);
575+
} else {
576+
ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
577+
}
575578

576579
smart_str_free(&buf);
577580
}

ext/json/php_json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, ze
5656
#define PHP_JSON_HEX_QUOT (1<<3)
5757
#define PHP_JSON_FORCE_OBJECT (1<<4)
5858
#define PHP_JSON_NUMERIC_CHECK (1<<5)
59+
#define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR (1<<9)
5960

6061
#define PHP_JSON_OUTPUT_ARRAY 0
6162
#define PHP_JSON_OUTPUT_OBJECT 1

ext/json/tests/bug43941.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

ext/json/tests/bug54058.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ json_encode($c);
2929
var_dump(json_last_error());
3030
?>
3131
--EXPECTF--
32+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3233
int(5)
34+
35+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3336
int(5)
37+
38+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3439
int(5)
40+
41+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3542
int(5)

ext/json/tests/bug61537.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #61537 (json_encode() incorrectly truncates/discards information)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$invalid_utf8 = "\x9f";
8+
var_dump(json_encode($invalid_utf8), json_last_error());
9+
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
10+
11+
$invalid_utf8 = "an invalid sequen\xce in the middle of a string";
12+
var_dump(json_encode($invalid_utf8), json_last_error());
13+
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
14+
?>
15+
--EXPECTF--
16+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
17+
bool(false)
18+
int(5)
19+
20+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
21+
string(4) "null"
22+
int(5)
23+
24+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
25+
bool(false)
26+
int(5)
27+
28+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
29+
string(4) "null"
30+
int(5)

0 commit comments

Comments
 (0)