[php-src] master: Merge branch 'PHP-8.6'
| From: | Weilin Du | Date: | Thu, 26 Nov 2026 16:16:26 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144228@lists.php.net to get a copy of this message | ||
Author: Weilin Du (LamentXU123)
Date: 2026-11-27T00:13:37+08:00
Commit: https://github.com/php/php-src/commit/b890258ab055bc3bf5f9fa6e56c3fc1ea74aaa3f
Raw diff: https://github.com/php/php-src/commit/b890258ab055bc3bf5f9fa6e56c3fc1ea74aaa3f.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
Fix stale string metadata in sodium_add() and sodium_increment() (#25775)
Changed paths:
A ext/sodium/tests/in_place_cached_string_state.phpt
M ext/sodium/libsodium.c
Diff:
diff --git a/ext/sodium/libsodium.c b/ext/sodium/libsodium.c
index 907709ba9e72..37220cd8c069 100644
--- a/ext/sodium/libsodium.c
+++ b/ext/sodium/libsodium.c
@@ -298,6 +298,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)