# Array_merge, safety and race condition?

- Thread: https://externals.io/message/19967
- Original archive: https://news-web.php.net/php.internals/19967
- Started by Ezra Nugroho on 2005-11-11T17:29:00+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #19967 — Ezra Nugroho — 2005-11-11T17:29:00+00:00

URL: https://externals.io/message/19967#19967

I sent this to the general list, but I didn't get definitive answer.
Maybe internals is the better forum to talk about it.
If you have a test code, or other pointers towards a definitive answer,
I'd love to try it.

Thanks




Php experts everywhere,

I want to merge two arrays, and I want to store the result as the first
array. Something like the following:


$array1 = array_merge ($array1, $array2);


So far the code gives me what I want. However, suppose if $array1 is
extremely huge, am I introducing a bug here because of possible race
condition? It's possible that array_merge has two write something to
$array1 (left hand side) before it even finishes reading it (argument)
in the first place. Let alone merging the two.

Should I just go conservative and do:

$tmp = array_merge($array1, $array2);
$array1 = $tmp; 


Thank you,
Ezra

---

## #19970 — Ron Korving — 2005-11-14T07:33:08+00:00

URL: https://externals.io/message/19967#19970 · In reply to: #19967

Now I'm no core developer, but I think what you fear is impossible. If I'm 
not mistaken: `array_merge()` will write it's result to a piece of data and 
when it's finished, it will make $array1 point to it, as I expect this to 
work in every function that returns something.

Ron


"Ezra Nugroho" <enugroho@spikesource.com> schreef in bericht 
news:1131730140.30983.2.camel@ganges.spikesource.com...
>I sent this to the general list, but I didn't get definitive answer.
> Maybe internals is the better forum to talk about it.
> If you have a test code, or other pointers towards a definitive answer,
> I'd love to try it.
>
> Thanks
>
>
>
>
> Php experts everywhere,
>
> I want to merge two arrays, and I want to store the result as the first
> array. Something like the following:
>
>
> $array1 = array_merge ($array1, $array2);
>
>
> So far the code gives me what I want. However, suppose if $array1 is
> extremely huge, am I introducing a bug here because of possible race
> condition? It's possible that array_merge has two write something to
> $array1 (left hand side) before it even finishes reading it (argument)
> in the first place. Let alone merging the two.
>
> Should I just go conservative and do:
>
> $tmp = array_merge($array1, $array2);
> $array1 = $tmp;
>
>
> Thank you,
> Ezra
