[PATCH 1/2] regulator: core: Add a sanity check on the regulator_ enable/disable functions

From: Gregory CLEMENT
Date: Fri Dec 26 2014 - 12:27:13 EST


These two functions use the pointer passed in parameter without any
check. By adding a NULL pointer check, it allows using those functions
from a driver in a more generic way. It is useful especially for the
disable case if the regulator is optional.

Signed-off-by: Gregory CLEMENT <gregory.clement@xxxxxxxxxxxxxxxxxx>
---
drivers/regulator/core.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index e225711bb8bc..de29399b5430 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1911,12 +1911,16 @@ static int _regulator_enable(struct regulator_dev *rdev)
*/
int regulator_enable(struct regulator *regulator)
{
- struct regulator_dev *rdev = regulator->rdev;
+ struct regulator_dev *rdev;
int ret = 0;

+ if (!regulator)
+ return 0;
+
if (regulator->always_on)
return 0;

+ rdev = regulator->rdev;
if (rdev->supply) {
ret = regulator_enable(rdev->supply);
if (ret != 0)
@@ -2024,12 +2028,17 @@ static int _regulator_disable(struct regulator_dev *rdev)
*/
int regulator_disable(struct regulator *regulator)
{
- struct regulator_dev *rdev = regulator->rdev;
+ struct regulator_dev *rdev;
int ret = 0;

+ if (!regulator)
+ return 0;
+
if (regulator->always_on)
return 0;

+ rdev = regulator->rdev;
+
mutex_lock(&rdev->mutex);
ret = _regulator_disable(rdev);
mutex_unlock(&rdev->mutex);
--
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/