From 3d4ebc038cc3336a11a263eca5f465b05cfce896 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 12 Mar 2013 21:56:08 -0400 Subject: [PATCH] Throw exceptions when non Datasource classes are used as Datasources. Using models as datasources can cause segmentation faults. Guard against that by checking types and raising exceptions early. Fixes #3694 --- lib/Cake/Error/exceptions.php | 2 +- lib/Cake/Model/ConnectionManager.php | 12 ++++++++++-- lib/Cake/View/Errors/missing_datasource.ctp | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Error/exceptions.php b/lib/Cake/Error/exceptions.php index 3a7b46912..992aacf60 100644 --- a/lib/Cake/Error/exceptions.php +++ b/lib/Cake/Error/exceptions.php @@ -441,7 +441,7 @@ class MissingDatasourceConfigException extends CakeException { */ class MissingDatasourceException extends CakeException { - protected $_messageTemplate = 'Datasource class %s could not be found.'; + protected $_messageTemplate = 'Datasource class %s could not be found. %s'; } diff --git a/lib/Cake/Model/ConnectionManager.php b/lib/Cake/Model/ConnectionManager.php index 4619c2ade..a756c75bb 100644 --- a/lib/Cake/Model/ConnectionManager.php +++ b/lib/Cake/Model/ConnectionManager.php @@ -99,9 +99,17 @@ class ConnectionManager { $conn = self::$_connectionsEnum[$name]; $class = $conn['classname']; - self::$_dataSources[$name] = new $class(self::$config->{$name}); - self::$_dataSources[$name]->configKeyName = $name; + $instance = new $class(self::$config->{$name}); + $instance->configKeyName = $name; + if (!$instance instanceof Datasource) { + throw new MissingDatasourceException(array( + 'class' => $class, + 'plugin' => null, + 'message' => 'Only classes extending Datasource can be used as datasources.' + )); + } + self::$_dataSources[$name] = $instance; return self::$_dataSources[$name]; } diff --git a/lib/Cake/View/Errors/missing_datasource.ctp b/lib/Cake/View/Errors/missing_datasource.ctp index 25cebf68e..c6d712e18 100644 --- a/lib/Cake/View/Errors/missing_datasource.ctp +++ b/lib/Cake/View/Errors/missing_datasource.ctp @@ -22,6 +22,9 @@ $pluginDot = empty($plugin) ? null : $plugin . '.';

: ' . $pluginDot . $class . ''); ?> + + +

: