php.net |  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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: h dot kocznar at compunect dot com
New email:
PHP Version: OS:

 

 [2019-12-04 06:35 UTC] h dot kocznar at compunect dot com
Description:
------------
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 ctype.so[7f58ba07e000+3000]
 segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a15fea60 error 14 in xdebug.so[7f58bb546000+40000]
segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a0bfea60 error 14 in xml.so[7f58baaaa000+b000]
segfault at 7f58b9c366f7 ip 00007f58b9c366f7 sp 00007f58a0bfea60 error 14 in pdo.so[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' =>
      NULL
      'distance_km' =>
      double(1.7982744310941)
      'returncode' =>
      bool(true)
    }

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))
        {
            $key=(string)md5($key);
            return $storage[$identifier][$key]??NULL;
        }
        // storage
        if (empty($key))
        {
            $storage[$identifier][]=$value;
        }
        {
            $key=(string)md5($key);
            $storage[$identifier][$key]=$value;
        }

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

        return true;

    }


Patches

Pull Requests

History

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] stas@php.net
-Type: Security +Type: Bug
 [2019-12-04 07:50 UTC] nikic@php.net
PHP 7.2.7 is very old, does this still reproduce in a recent PHP 7.2 version?
 [2019-12-04 07:50 UTC] nikic@php.net
-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: Sun Dec 22 06:01:30 2024 UTC