php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14749 array_reduce() causes segmentation faults
Submitted: 2001-12-28 17:27 UTC Modified: 2002-07-04 18:14 UTC
From: dave at ovumdesign dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 4.1.1 OS: RedHat 7.1
Private report: No CVE-ID: None
 [2001-12-28 17:27 UTC] dave at ovumdesign dot com
This drove me crazy. array_reduce() causes segmentation faults when used in a complex, recursive manner. I'm pretty sure it's array_reduce() and not create_function() that's the culprit here. I'm sorry I can't provide a simpler example, but I've been unable to reprodce the crash otherwise. I'll try to describe what I'm doing to the best of my abilities.

debug.php is an include that I use for dumping out nicely-formatted data structures - it works like print_r() but cleaner. It's written using functional techniques, so I'm using create_function() as a lambda and array_reduce() as a reduction.

tree.xml is an XML file that I'm parsing into a data structure using wddx_deserialize(). I thought that it was my deserialization that was the problem when I was using XML-RPC, and switched to WDDX in hopes that that would solve the crash problem. It didn't. It took a lot of monkeying around before I figured out where the real problem was.

I don't know how to use GDB, and I don't really have any more time to dedicate to this, so no backtrace. Sorry. I've experienced this problem with PHP 4.1.0, 4.1.1, and 4.2.0-dev under RedHat 7.1 and Apache 1.3.22.

Here are my source files:

crash.php (this is the one to run)

<pre>
<?php
require_once 'debug.php';

$data = wddx_deserialize(join('', file('tree.xml')));

// This works.
print_r($data);

// This doesn't.
//debug($data);
?>
</pre>

debug.php:

<?php

function array_items($array) {
    $result = array();
    foreach ($array as $key => $val) $result[] = array($key, $val);
    return $result;
}

function debug_node($key, $val) {
    return "-+ <font color=\"#ad0000\"><b>$key</b></font> "
         . "<font color=\"#505050\">(" . gettype($val) . ")</font><br>\n";
}

function debug_leaf($key, $val) {
    return "-> <b>$key</b> = <font color=\"#0000ad\">$val</font> "
         . "<font color=\"#505050\">(" . gettype($val) . ")</font><br>\n";
}

function debug_reduce($prep, $accum, $key_val) {
    list($key, $val) = $key_val;
    return "$accum$prep" . (is_array($val) ? debug_node($key,      $val)
                                           . debug_tree("$prep |", $val)
                                           : debug_leaf($key,      $val));
}

function debug_tree($prep, $array) {
    return array_reduce(array_items($array),
                        create_function('$a, $x',
                            "return debug_reduce('$prep', \$a, \$x);"), '');
}

function debug_array($array) {
    return "<font face=\"arial\" size=3 color=\"black\"><code>\n"
         . debug_tree('|', $array) . '</code></font>';
}

function debug_string($string) {
    return "<font face=\"arial\" size=2 color=\"black\">\n"
         . nl2br($string) . "</font>\n";
}

function debug($msg) {
    ?>
<table width="100%" border=0 cellpadding=5 cellspacing=0 bgcolor="#dedede" bordercolor="black"><td>
<?=(is_array($msg) or is_object($msg)) ? debug_array($msg) : debug_string($msg)?>
</td></table>
<table width="100%" height=1 cellpadding=0 cellspacing=0 bgcolor="black"><td></td></table>
    <?

    return $msg;
}

?>

tree.xml:

<?xml version="1.0"?><!DOCTYPE wddxPacket SYSTEM "../static/wddx_0090.dtd"><wddxPacket version="0.9"><header/><data><array length="
4"><struct><var name="label"><string>About Us</string></var><var name="children"><array length="2"><struct><var name="label"><strin
g>Who We Are</string></var><var name="children"><array length="0"></array></var><var name="path"><string>about/who</string></var><v
ar name="content"><array length="2"><number>23</number><number>42</number></array></var></struct><struct><var name="label"><string>
What We Doodoo</string></var><var name="children"><array length="0"></array></var><var name="path"><string>about/what</string></var
><var name="content"><array length="1"><number>69</number></array></var></struct></array></var><var name="path"><string>about</stri
ng></var><var name="content"><array length="0"></array></var></struct><struct><var name="label"><string>Products</string></var><var
 name="children"><array length="0"></array></var><var name="path"><string>products</string></var><var name="content"><array length=
"2"><number>86</number><number>99</number></array></var></struct><struct><var name="label"><string>Services</string></var><var name
="children"><array length="0"></array></var><var name="path"><string>services</string></var><var name="content"><array length="0"><
/array></var></struct><struct><var name="label"><string>Contact Us</string></var><var name="children"><array length="0"></array></v
ar><var name="path"><string>contact</string></var><var name="content"><array length="1"><number>1</number></array></var></struct></
array></data></wddxPacket>

That's it. Good luck.

Peace,
Dave

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-11 23:12 UTC] elixer@php.net
Can you attach a copy of tree.xml to an email and send it to elixer@php.net?  Also, please update this bug report to include your configure line.

Sean
 [2002-02-12 13:53 UTC] dave at ovumdesign dot com
Configure line:

'./configure' '--with-apxs' '--with-mysql=/usr' '--with-xml' '--enable-wddx' '--with-imap' '--with-gdbm'
 [2002-07-04 18:14 UTC] eru@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately your are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 06:01:30 2024 UTC