[php-src] PR #25775: Fix sodium_add() and sodium_increment() keeping the cached hash and UTF-8 flag of the string they write
| From: | lacatoire | Date: | Thu, 26 Nov 2026 10:30:37 +0000 |
| Subject: | [php-src] PR #25775: Fix sodium_add() and sodium_increment() keeping the cached hash and UTF-8 flag of the string they write | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41039@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25775
Author: lacatoire
sodium_add() and sodium_increment() write into their first argument in
place when it is not shared, but do not reset the cached hash or the valid UTF-8 flag of the string,
as other in-place writers do. A later array lookup then uses the hash of the old content, and PCRE
skips the UTF-8 check on bytes that are no longer valid. sodium_separate_string() now
calls zend_string_forget_hash_val() in that case.
### To reproduce
```php
<?php
$x = 'c';
$arr = ['abc' => 1, 'bbc' => 2];
$s = 'ab' . $x;
isset($arr[$s]);
sodium_add($s, "\x01\x00\x00");
var_dump($s === 'bbc', isset($arr[$s]));
$u = 'ab' . $x;
preg_match('/./u', $u);
sodium_add($u, "\x00\x00\x8e");
var_dump(bin2hex($u), preg_match('/./u', $u));
```
Before (PHP 8.4.28):
```
bool(true)
bool(false)
string(6) "6162f1"
int(1)
```
After:
```
bool(true)
bool(true)
string(6) "6162f1"
bool(false)
```