[php-src] master: Merge branch 'PHP-8.6'
| From: | Ilia Alshanetsky | Date: | Sat, 31 Oct 2026 22:05:03 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143089@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-10-31T18:02:54-04:00
Commit: https://github.com/php/php-src/commit/9e7da079645e19b169f2446613bd31bbdb6caa24
Raw diff: https://github.com/php/php-src/commit/9e7da079645e19b169f2446613bd31bbdb6caa24.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
ext/json: Stop encoding after a key fails to encode
Changed paths:
A ext/json/tests/json_encode_invalid_key_aborts.phpt
M ext/json/json_encoder.c
Diff:
diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c
index 93e4730e130f..87d8bc559581 100644
--- a/ext/json/json_encoder.c
+++ b/ext/json/json_encoder.c
@@ -164,11 +164,11 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int
options,
if (php_json_escape_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name),
options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE) {
- if (ZEND_HAS_OOM()) {
+ if (ZEND_HAS_OOM() || !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
return FAILURE;
}
- if ((options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) && buf->s) {
+ if (buf->s) {
ZEND_ASSERT(ZSTR_LEN(buf->s) >= 4);
ZSTR_LEN(buf->s) -= 4;
smart_str_appendl(buf, "\"\"", 2);
@@ -285,13 +285,13 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int
options,
if (php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key),
options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE) {
- if (ZEND_HAS_OOM()) {
+ if (ZEND_HAS_OOM() || !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
zend_release_properties(prop_ht);
zval_ptr_dtor(&tmp);
return FAILURE;
}
- if ((options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) && buf->s) {
+ if (buf->s) {
ZEND_ASSERT(ZSTR_LEN(buf->s) >= 4);
ZSTR_LEN(buf->s) -= 4;
smart_str_appendl(buf, "\"\"", 2);
diff --git a/ext/json/tests/json_encode_invalid_key_aborts.phpt
b/ext/json/tests/json_encode_invalid_key_aborts.phpt
new file mode 100644
index 000000000000..76afc5be43f3
--- /dev/null
+++ b/ext/json/tests/json_encode_invalid_key_aborts.phpt
@@ -0,0 +1,55 @@
+--TEST--
+json_encode() stops encoding after an object or array key fails to encode
+--FILE--
+<?php
+
+class Value implements JsonSerializable
+{
+ public function jsonSerialize(): mixed
+ {
+ echo "jsonSerialize() called\n";
+ return 1;
+ }
+}
+
+eval('class Declared { public $' . "\xff" . ' = 1; public $next; }');
+
+$dynamic = new stdClass;
+$dynamic->{"\xff"} = 1;
+$dynamic->next = new Value;
+
+$declared = new Declared;
+$declared->next = new Value;
+
+$cases = [
+ 'array' => ["\xff" => 1, 'next' => new Value],
+ 'array, later NAN' => ["\xff" => 1, 'next' => NAN],
+ 'dynamic property' => $dynamic,
+ 'declared property' => $declared,
+];
+
+foreach ($cases as $name => $value) {
+ echo "$name:\n";
+ var_dump(json_encode($value), json_last_error_msg());
+}
+
+echo "partial output:\n";
+var_dump(json_encode(["\xff" => 1, 'next' => 2],
JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error_msg());
+
+?>
+--EXPECT--
+array:
+bool(false)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+array, later NAN:
+bool(false)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+dynamic property:
+bool(false)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+declared property:
+bool(false)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+partial output:
+string(15) "{"":1,"next":2}"
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"