Skip to content

Fix sodium_add() and sodium_increment() keeping the cached hash and UTF-8 flag of the string they write - #25775

Closed
lacatoire wants to merge 1 commit into
php:PHP-8.4from
lacatoire:fix/sodium-separate-string-hash-PHP-8.4
Closed

lacatoire wants to merge 1 commit into
php:PHP-8.4from
lacatoire:fix/sodium-separate-string-hash-PHP-8.4

Conversation

@lacatoire

@lacatoire lacatoire commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

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

$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)

LamentXU123 added a commit that referenced this pull request Nov 26, 2026
* PHP-8.6:
  Fix stale string metadata in sodium_add() and sodium_increment() (#25775)
@lacatoire
lacatoire deleted the fix/sodium-separate-string-hash-PHP-8.4 branch November 26, 2026 16:19
arnaud-lb pushed a commit to hohphu8/php-src that referenced this pull request Nov 26, 2026
* PHP-8.4:
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
arnaud-lb pushed a commit to hohphu8/php-src that referenced this pull request Nov 26, 2026
* PHP-8.5:
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
arnaud-lb added a commit to hohphu8/php-src that referenced this pull request Nov 26, 2026
* PHP-8.6:
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  Fix phar_set_inode() using the zend_string header instead of the filename (php#25768)
  Fix phpGH-25360: ZipArchive assertion when error handler throws at shutdown (php#25361)
arnaud-lb added a commit to pavimus/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (91 commits)
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  [skip ci] Use USE_TRACKED_ALLOC=1 for Symfony Preloading test
  [skip ci] Fix Symfony test without USE_TRACKED_ALLOC
  [skip ci] Skip assign_dim_undef_op_data_008.phpt when JIT is unavailable
  ...
arnaud-lb added a commit to lazerg/php-src that referenced this pull request Nov 27, 2026
* PHP-8.4: (262 commits)
  ext/pdo_sqlite: crash when a collation callback closes the statement cursor
  Admit NTS requests to OPcache SHM only in RINIT (php#25509)
  ext/pgsql: pg_meta_data() numeric column names not integer keys with opcache
  [skip ci] Add NEWS entry for php#25674
  ext/bz2: Fix NULL dereference when compress.bzip2:// open fails in write mode
  ext/dom: Reject DTD declaration nodes in adoptNode()
  Fix iconv_mime_encode() overflowing on a negative line-length (php#25485)
  ext/pgsql: pg_get_notify() result is never === false with opcache enabled
  ext/zlib: fix gzwrite() not reporting write errors
  Fix phpGH-25779: mysqli_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded (php#25784)
  Fix stale string metadata in sodium_add() and sodium_increment() (php#25775)
  Sync `session.upload_progress.enabled` default in `php.ini-{development,production}` (php#25743)
  ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this
  Fix hash() and hash_file() hanging on invalid xxh3/xxh128 options (php#25777)
  ext/dom: fix Dom\Notation publicId and systemId property handlers
  Fix FFI::string() reading out of bounds for a negative size
  [ci skip] Update test title
  Fix phpGH-25558: FFI: struct with an enum field passed by value is described to libffi with a void element
  Fix OSS-Fuzz #588264417
  Fix FPM log overflow when errno text exceeds the buffer (phpGH-25648)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants