[php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5
| From: | Weilin Du | Date: | Thu, 26 Nov 2026 16:16:14 +0000 |
| Subject: | [php-src] PHP-8.6: Merge branch 'PHP-8.4' into PHP-8.5 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144226@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Date: 2026-11-27T00:12:37+08:00
Commit: https://github.com/php/php-src/commit/131adb5aa12c9c00ed5e6f46adfbd31349ab739e
Raw diff: https://github.com/php/php-src/commit/131adb5aa12c9c00ed5e6f46adfbd31349ab739e.diff
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
Fix stale string metadata in sodium_add() and sodium_increment() (#25775)
Changed paths:
A ext/sodium/tests/in_place_cached_string_state.phpt
M NEWS
M ext/sodium/libsodium.c
Diff:
diff --git a/NEWS b/NEWS
index 1acf398d1c3f..3b3e0159984c 100644
--- a/NEWS
+++ b/NEWS
@@ -225,6 +225,10 @@ PHP NEWS
. Fixed socket_addrinfo_lookup() rejecting an empty packed hints array.
(lacatoire)
+- Sodium:
+ . Fixed sodium_add() and sodium_increment() retaining stale string hashes
+ and UTF-8 validity flags. (lacatoire)
+
- SPL:
. Fixed memory leak of the static variables of a file loaded by
spl_autoload(). (lacatoire)
diff --git a/ext/sodium/libsodium.c b/ext/sodium/libsodium.c
index 208ea5a69f6a..9bc71e0a8ca9 100644
--- a/ext/sodium/libsodium.c
+++ b/ext/sodium/libsodium.c
@@ -151,6 +151,9 @@ static void sodium_separate_string(zval *zv) {
zend_string *copy = zend_string_init(Z_STRVAL_P(zv), Z_STRLEN_P(zv), 0);
Z_TRY_DELREF_P(zv);
ZVAL_STR(zv, copy);
+ } else {
+ /* The caller writes into the string: drop its cached hash and UTF-8 flag */
+ zend_string_forget_hash_val(Z_STR_P(zv));
}
}
diff --git a/ext/sodium/tests/in_place_cached_string_state.phpt
b/ext/sodium/tests/in_place_cached_string_state.phpt
new file mode 100644
index 000000000000..1ae014d27582
--- /dev/null
+++ b/ext/sodium/tests/in_place_cached_string_state.phpt
@@ -0,0 +1,35 @@
+--TEST--
+sodium_add() and sodium_increment() reset the cached hash and the UTF-8 flag of the string they
write
+--EXTENSIONS--
+sodium
+--FILE--
+<?php
+
+$x = 'c';
+$array = ['abc' => 1, 'bbc' => 2];
+
+foreach (['sodium_add', 'sodium_increment'] as $function) {
+ // The hash of "abc" is cached by the lookup, then the bytes change
+ $string = 'ab' . $x;
+ var_dump(isset($array[$string]));
+ $function === 'sodium_add' ? sodium_add($string, "\x01\x00\x00") :
sodium_increment($string);
+ var_dump($string === 'bbc', isset($array[$string]));
+}
+
+// The valid UTF-8 flag is set by the match, then the bytes become invalid
+$string = 'ab' . $x;
+var_dump(preg_match('/./u', $string));
+sodium_add($string, "\x00\x00\x8e");
+var_dump(bin2hex($string), preg_match('/./u', $string));
+
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+int(1)
+string(6) "6162f1"
+bool(false)