pmmp / ext-pmmpthread

Fork of https://github.com/krakjoe/pthreads with a revamped API and PHP 8.1+ support
Other
81 stars 17 forks source link

Multidimensional array writes don't work on ThreadSafeArray when the target sub-array doesn't exist #44

Open dktapps opened 3 years ago

dktapps commented 3 years ago
<?php

declare(strict_types=1);

$t = [];
$t[0][0] = 1;
$t[0][1] = 2;

var_dump($t);

produces

array(1) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(2)
  }
}

vs

<?php

declare(strict_types=1);

$t = new \Threaded();
$t[0][0] = 1;
$t[0][1] = 2;

var_dump($t);

which produces

Notice: Indirect modification of overloaded element of Threaded has no effect in C:\Users\dylan-work\Documents\projects\pocketmine-mp\test2.php on line 25

Notice: Indirect modification of overloaded element of Threaded has no effect in C:\Users\dylan-work\Documents\projects\pocketmine-mp\test2.php on line 26
object(Threaded)#1 (0) {
}
dktapps commented 3 years ago

Seems like the real bug is that this check is not firing, because the offset doesn't exist: https://github.com/pmmp/pthreads/blob/fork/src/store.c#L311

On closer inspection this problem could be hard to solve, because BP_VAR_W writes could also be generated by something like threaded->nonExisting[0] = 1, which should not result in automatic property creation. pthreads_store_read() doesn't make any differentiation between property reads and array offset accesses.