Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ PHP NEWS
- OpenSSL:
. Fix a bunch of memory leaks and crashes on edge cases. (ndossche)

- Random:
. Fixed bug GH-21730 (Mt19937::__debugInfo() leaks state HashTable when
the serialize callback fails). (iliaal)

- SPL:
. Fixed bug GH-21499 (RecursiveArrayIterator getChildren UAF after parent
free). (Girgias)
Expand Down
2 changes: 1 addition & 1 deletion ext/random/engine_mt19937.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ PHP_METHOD(Random_Engine_Mt19937, __debugInfo)

if (engine->engine.algo->serialize) {
array_init(&t);
zend_hash_str_add(Z_ARR_P(return_value), "__states", strlen("__states"), &t);
if (!engine->engine.algo->serialize(engine->engine.state, Z_ARRVAL(t))) {
zend_throw_exception(NULL, "Engine serialize failed", 0);
RETURN_THROWS();
}
zend_hash_str_add(Z_ARR_P(return_value), "__states", strlen("__states"), &t);
}
}
/* }}} */
41 changes: 41 additions & 0 deletions ext/random/tests/02_engine/debuginfo_states.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
GH-21730: __debugInfo() exposes the engine state under __states
--FILE--
<?php declare(strict_types = 1);

use Random\Engine\Mt19937;
use Random\Engine\PcgOneseq128XslRr64;
use Random\Engine\Xoshiro256StarStar;

$mt = new Mt19937(1234);
$mtInfo = $mt->__debugInfo();
var_dump(array_key_exists('__states', $mtInfo));
var_dump(count($mtInfo['__states']));

$pcg = new PcgOneseq128XslRr64(1234);
$pcgInfo = $pcg->__debugInfo();
var_dump(array_key_exists('__states', $pcgInfo));
var_dump(count($pcgInfo['__states']));

$xo = new Xoshiro256StarStar(1234);
$xoInfo = $xo->__debugInfo();
var_dump(array_key_exists('__states', $xoInfo));
var_dump(count($xoInfo['__states']));

// The serialized form and __debugInfo both round-trip through the engine's
// serialize callback, so they must produce identical state data.
var_dump($mt->__serialize()[1] === $mtInfo['__states']);
var_dump($pcg->__serialize()[1] === $pcgInfo['__states']);
var_dump($xo->__serialize()[1] === $xoInfo['__states']);

?>
--EXPECT--
bool(true)
int(626)
bool(true)
int(2)
bool(true)
int(4)
bool(true)
bool(true)
bool(true)
Loading