|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78906 static $array variable memory corruption and segfaults from array_shift()
Submitted: 2019-12-04 06:35 UTC Modified: 2019-12-15 04:22 UTC
From: h dot kocznar at compunect dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 7.2.25 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-12-04 06:35 UTC] h dot kocznar at compunect dot com
I fear I can't supply a full test script but I've a reproduceable case here with PHP 7.2.7. 
I've a variable type corruption and randomized segfaults of php itself.

segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a03fea60 error 14 in[7f58ba07e000+3000]
 segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a15fea60 error 14 in[7f58bb546000+40000]
segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a0bfea60 error 14 in[7f58baaaa000+b000]
segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a0bfea60 error 14 in[7f58bacb7000+19000]

I've a function circular() which keeps a static variable that's used to store up to n (100) entries in a script that's processing coordinates.  

The function uses array_shift() to remove the oldest element from the static array when the size reaches 100.

The variable normally contains elements like this:
'2baa468ffd1672f235a35b55dbbbde80' =>
    array(6) {
      'country_name' =>
      string(13) "United States"
      'state_name' =>
      string(4) "Ohio"
      'city_name' =>
      string(10) "Youngstown"
      'zip_code' =>
      'distance_km' =>
      'returncode' =>

However after running for a 102 cycles (2nd array_shift()) I'm getting a memory consumption error (256MB used, though only 100 entries are kept).
In addition the array content suddenly changes from an array into a string type.
 string(300668) "Anited States                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "...

It works fine without array_shift()

Test script:
    public static function circular(string $identifier, string $key=NULL,$value=NULL, int $size=100)
        static $storage=[];
        if ($identifier == 'GET') var_dump($storage);
        if (empty($key) && empty($value))
            return $storage[$identifier]??[];
        if (!empty($key) && empty($value))
            return $storage[$identifier][$key]??NULL;
        // storage
        if (empty($key))

        if (count($storage[$identifier]) > $size) $storage[$identifier] = array_shift($storage[$identifier]); // this here causes the segfaults/corruption

        return true;



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-04 06:39 UTC] h dot kocznar at compunect dot com
I just realized that this was triggered by me using array_shift() wrong, I didn't realized it's a pointer/reference parameter.
Though the bugreport itself is still valid.
 [2019-12-04 07:20 UTC]
-Type: Security +Type: Bug
 [2019-12-04 07:50 UTC]
PHP 7.2.7 is very old, does this still reproduce in a recent PHP 7.2 version?
 [2019-12-04 07:50 UTC]
-Status: Open +Status: Feedback
 [2019-12-15 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 18:01:28 2024 UTC