Repository navigation
Conversation
php_array_data_shuffle() first compacts the holes of a packed array and only then updates nNumUsed. When the engine threw in between, the stale tail slots were still counted, so the moved values were released twice when the copied array was destroyed. Update nNumUsed right after the compaction.
TimWolla
left a comment
There was a problem hiding this comment.
As a ext/random maintainer, the test case LGTM. I defer the review of the C code to someone else.
ndossche
left a comment
There was a problem hiding this comment.
I think it's better to update nNumUsed (and in my opinion also nInternalPointer and nNextFreeElement) to after the compaction happens. That means that lines 3346-3348 (pre-patch line numbers) will disappear from the end and will be duplicated 2 times: once after each compaction loop.
| } | ||
| j++; | ||
| } | ||
| /* The tail now holds stale copies; drop it before the engine can throw. */ |
There was a problem hiding this comment.
This comment is a bit misleading, it's not "dropping" it's rather "truncating" the stale slots
|
Thanks, done: the counters are now reset right after compaction in both branches (before the engine can throw), the trailing reset is gone, and the comment says "truncate". ext/random and ext/standard/tests/array pass. |
ndossche
left a comment
There was a problem hiding this comment.
This is good, thanks. Leaving open a bit to give other maintainers/codeowners some time to review too if they want
php_array_data_shuffle()first compacts the holes of a packed array by moving the remaining values down withZVAL_COPY_VALUE(), and only setsnNumUsedafter the shuffle loop:If the engine throws during the loop, the function returns with the old
nNumUsed. The tail slots still hold copies of values that were moved down, so those values are counted twice.Randomizer::shuffleArray()then returns withRETURN_THROWS()and its copy of the array is destroyed, which releases the moved values twice. The caller's original array is left pointing at a freed value.This needs a user-supplied
Random\Enginethat throws (or whosegenerate()returns an empty string, which also throws), and an array with holes.shuffle()uses the default engine, which does not throw, and arrays without holes are not affected.Reproducer
var_dump($a)while printing$a[2]. An ASan build withUSE_ZEND_ALLOC=0reportsheap-use-after-free ... READ of size 8inphp_var_dump(); the object was freed byzend_objects_store_del()fromzend_array_destroy()of the copy.done, no ASan report.Fix
Set
nNumUsedto the number of elements right after the compaction, in both the regular and the iterator branch, so the table is consistent before the engine is called. The success path is unchanged. It still setsnNumUsed,nInternalPointerandnNextFreeElementat the end.Test
ext/random/tests/03_randomizer/methods/shuffleArray_engine_throws_with_holes.phptshuffles an array with a hole, using an engine that throws on the first call and one that throws on the second call. It checks that the original array is intact afterwards.Termsig=11) in the release build, and ASan reports a heap-use-after-free.USE_ZEND_ALLOC=0).ext/random/testsandext/standard/tests/array: 909 passed, 14 skipped (release build). Under ASan, the same set passes except 4 stack-limit tests (count_recursive_stack_limit.phpt,gh23111.phpt,gh23113.phpt,gh23115.phpt). They fail the same way without this change.This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.
🤖 Generated with Claude Code