Skip to content

Instantly share code, notes, and snippets.

@vierbergenlars
Created November 20, 2012 16:49
Show Gist options
  • Save vierbergenlars/4119190 to your computer and use it in GitHub Desktop.
Save vierbergenlars/4119190 to your computer and use it in GitHub Desktop.
Get a PHP segfault

First, clone git clone git@github.com:vierbergenlars/gihp.git Check out git checkout 08dc6abfdbf90b1e1a8b10cd7064601fb820d950 And then insert the file above and run with php segv.php

<?php
use gihp\Defer\Object as Defer;
use gihp\Defer\Deferrable;
use gihp\Defer\Reference;
require('vendor/autoload.php');
class Node {
protected $data;
protected $parent;
protected $hash;
function __construct($data, Node $parent=null) {
$this->data = $data;
$this->{'parent'} = $parent;
$this->hash = sha1($data.($parent?$parent->getHash():''));
}
function getHash() {
return $this->hash;
}
function getData() {
return $this->data;
}
function getParent() {
return $this->{'parent'};
}
static function import(NodeStore $store, $encoded) {
$parts = explode("\0", $encoded);
$data = $parts[0];
$parent_hash = $parts[1];
$defer= new Defer(array('data'=>$data, 'parent'=>($parent_hash?new Reference($store, $parent_hash): null), 'hash'=>sha1($data.($parent_hash?$parent_hash:''))), __CLASS__, $store);
return $defer->getClass();
}
function __toString() {
return $this->data."\0".($this->{'parent'}?$this->{'parent'}->getHash():'');
}
}
class NodeStore implements \gihp\Defer\Loader {
private $db;
function __construct() {
$this->db = array();
}
function addNode(Node $node) {
$this->db[$node->getHash()] = $node->__toString();
}
function getNode($hash) {
return Node::import($this, $this->db[$hash]);
}
function load($hash) {
return $this->getNode($hash);
}
}
echo 'Starting';
$store = new NodeStore;
$root = new Node('root');
$xnode = $root;
echo 'Loop init';
for($i=0; $i <100000; $i++) { //
// Add an extra 0 above to get a segv earlier.
//
$xnode = new Node($i, $xnode);
$store->addNode($xnode);
}
echo 'Loop exit';
$store->addNode($root);
$node_str = $xnode->__toString();
echo 'Done adding nodes';
$xnode_bis = Node::import($store, $node_str);
echo 'Getting data';
var_dump($xnode_bis->getData());
echo 'Done';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment