Skip to content

Fix array copy-on-write: keep source after mutating copy (#5340) - #5371

Merged
PurHur merged 1 commit into
masterfrom
agent/issue-5340-array-cow
Jun 4, 2026
Merged

PurHur merged 1 commit into
masterfrom
agent/issue-5340-array-cow

Conversation

@PurHur

@PurHur PurHur commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Fixes array copy-on-write regression where $c = $a followed by $c[0] = 99 left $a as NULL instead of [1].
  • Root cause: TYPE_ASSIGN in lib/VM.php nulled arg3 after every assignment (added for list-destructuring by-ref in Fix list destructuring by-ref semantics (#4433) #4438), including named variables like $a.
  • Added Block::isNamedVariableSlot() and only release compiler temporaries after assign; named locals are preserved.

php-src reference

  • Zend/zend_array.czend_array_separate() COW split on write
  • Zend/zend_variables.c — zval separation; source zval must survive shared HashTable until refcount drops naturally

Verification

./script/docker-exec.sh -- bash -lc 'php bin/vm.php test/repro/issue5340_array_cow.php'

Output (matches Zend semantics):

array(1) { [0]=> int(1) }
array(1) { [0]=> int(99) }
array(1) { [0]=> int(1) }
array(1) { [0]=> int(1) }
array(1) { [0]=> int(99) }
./script/docker-exec.sh -- bash -lc 'php vendor/bin/phpunit test/unit/ArrayCowAssignTest.php test/unit/ListDestructKeyedByRefNestedTest.php test/compliance/VMTest.php --filter array_cow'

All green (2 ArrayCowAssign unit tests, list destructuring regression, 2 compliance phpts).

Closes #5340

Made with Cursor

After list-destructuring by-ref work (#4438), TYPE_ASSIGN cleared arg3
even when it was a live named variable like $a in `$c = $a`, breaking
copy-on-write so mutating the copy left the source NULL. Only release
compiler temporaries; keep named scope slots intact.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Language: array copy-on-write — mutating copy must not NULL the source (zend_array.c)

1 participant