Skip to content

Commit

Permalink
BUGFIX: Looping over a PaginatedList in the template caused a seg fau…
Browse files Browse the repository at this point in the history
…lt/bus error.
  • Loading branch information
simonwelsh committed May 11, 2012
1 parent 1e6a526 commit ab34688
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 6 additions & 0 deletions tests/view/SSViewerTest.php
Expand Up @@ -967,6 +967,12 @@ function testRenderWithSourceFileComments() {
SSViewer::set_source_file_comments(false);
}

function testLoopIteratorIterator() {
$list = new PaginatedList(new ArrayList());
$viewer = new SSViewer_FromString('<% loop List %>$ID - $FirstName<br /><% end_loop %>');
$result = $viewer->process(new ArrayData(array('List' => $list)));
$this->assertEquals($result, '');
}
}

/**
Expand Down
16 changes: 8 additions & 8 deletions view/SSViewer.php
Expand Up @@ -23,7 +23,7 @@
class SSViewer_Scope {

// The stack of previous "global" items
// And array of item, itemIterator, pop_index, up_index, current_index
// And array of item, itemIterator, itemIteratorTotal, pop_index, up_index, current_index
private $itemStack = array();

protected $item; // The current "global" item (the one any lookup starts from)
Expand All @@ -40,17 +40,16 @@ class SSViewer_Scope {
function __construct($item){
$this->item = $item;
$this->localIndex=0;
$this->itemStack[] = array($this->item, null, null, null, 0);
$this->itemStack[] = array($this->item, null, 0, null, null, 0);
}

function getItem(){
return $this->itemIterator ? $this->itemIterator->current() : $this->item;
}

function resetLocalScope(){
list($this->item, $this->itemIterator, $this->popIndex, $this->upIndex, $this->currentIndex) = $this->itemStack[$this->localIndex];
list($this->item, $this->itemIterator, $this->itemIteratorTotal, $this->popIndex, $this->upIndex, $this->currentIndex) = $this->itemStack[$this->localIndex];
array_splice($this->itemStack, $this->localIndex+1);
$this->itemIteratorTotal = $this->itemIterator ? $this->itemIterator->count() : 0;
}

function obj($name){
Expand All @@ -59,11 +58,11 @@ function obj($name){
case 'Up':
if ($this->upIndex === null) user_error('Up called when we\'re already at the top of the scope', E_USER_ERROR);

list($this->item, $this->itemIterator, $unused2, $this->upIndex, $this->currentIndex) = $this->itemStack[$this->upIndex];
list($this->item, $this->itemIterator, $this->itemIteratorTotal, $unused2, $this->upIndex, $this->currentIndex) = $this->itemStack[$this->upIndex];
break;

case 'Top':
list($this->item, $this->itemIterator, $unused2, $this->upIndex, $this->currentIndex) = $this->itemStack[0];
list($this->item, $this->itemIterator, $this->itemIteratorTotal, $unused2, $this->upIndex, $this->currentIndex) = $this->itemStack[0];
break;

default:
Expand All @@ -78,14 +77,14 @@ function obj($name){
break;
}

$this->itemStack[] = array($this->item, $this->itemIterator, null, $this->upIndex, $this->currentIndex);
$this->itemStack[] = array($this->item, $this->itemIterator, $this->itemIteratorTotal, null, $this->upIndex, $this->currentIndex);
return $this;
}

function pushScope(){
$newLocalIndex = count($this->itemStack)-1;

$this->popIndex = $this->itemStack[$newLocalIndex][2] = $this->localIndex;
$this->popIndex = $this->itemStack[$newLocalIndex][3] = $this->localIndex;
$this->localIndex = $newLocalIndex;

// We normally keep any previous itemIterator around, so local $Up calls reference the right element. But
Expand All @@ -111,6 +110,7 @@ function next(){

$this->itemStack[$this->localIndex][1] = $this->itemIterator;
$this->itemIteratorTotal = iterator_count($this->itemIterator); //count the total number of items
$this->itemStack[$this->localIndex][2] = $this->itemIteratorTotal;
$this->itemIterator->rewind();
}
else {
Expand Down

0 comments on commit ab34688

Please sign in to comment.