diff --git a/ext/json/json.c b/ext/json/json.c index 5b62c2feeaf19..cfda77bf5e0dd 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -571,9 +571,14 @@ static PHP_FUNCTION(json_encode) php_json_encode(&buf, parameter, options TSRMLS_CC); - ZVAL_STRINGL(return_value, buf.c, buf.len, 1); - - smart_str_free(&buf); + /* If an error is still present we must return false as per the documented behavior */ + if (JSON_G(error_code) != PHP_JSON_ERROR_NONE) { + smart_str_free(&buf); + RETURN_FALSE; + } else { /* Otherwise we can safely return the json */ + RETVAL_STRINGL(buf.c, buf.len, 1); + smart_str_free(&buf); + } } /* }}} */ diff --git a/ext/json/tests/bug61537.phpt b/ext/json/tests/bug61537.phpt new file mode 100644 index 0000000000000..1ebd09c5d263e --- /dev/null +++ b/ext/json/tests/bug61537.phpt @@ -0,0 +1,16 @@ +--TEST-- +Bug #61537 (json_encode() incorrectly truncates/discards information) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +string(4) "null" +int(0) +bool(false) +int(5)