Skip to content

Commit

Permalink
Adding checks to force limit to always be a positive integer. Fixes p…
Browse files Browse the repository at this point in the history
…otential out of bounds type queries with paginate(). Fixes #418
  • Loading branch information
markstory committed Mar 11, 2010
1 parent d8a757c commit 4c668c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cake/libs/controller/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1168,8 +1168,13 @@ function paginate($object = null, $scope = array(), $whitelist = array()) {
$type = $defaults[0];
unset($defaults[0]);
}

$options = array_merge(array('page' => 1, 'limit' => 20), $defaults, $options);
$options['limit'] = (empty($options['limit']) || !is_numeric($options['limit'])) ? 1 : $options['limit'];
$options['limit'] = (int) $options['limit'];
if (empty($options['limit']) || $options['limit'] < 1) {
$options['limit'] = 1;
}

extract($options);

if (is_array($scope) && !empty($scope)) {
Expand Down
8 changes: 8 additions & 0 deletions cake/tests/cases/libs/controller/controller.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,14 @@ function testPaginate() {
$this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
$this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
$this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);

$Controller->passedArgs = array();
$Controller->paginate = array('limit' => '-1');
$Controller->paginate('ControllerPost');
$this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
$this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
$this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
$this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
}

/**
Expand Down

0 comments on commit 4c668c0

Please sign in to comment.