Skip to content

Commit dddca3b

Browse files
joehattoribroonie
authored andcommitted
regulator: of: Implement the unwind path of of_regulator_match()
of_regulator_match() does not release the OF node reference in the error path, resulting in an OF node leak. Therefore, call of_node_put() on the obtained nodes before returning the EINVAL error. Since it is possible that some drivers call this function and do not exit on failure, such as s2mps11_pmic_driver, clear the init_data and of_node in the error path. This was reported by an experimental verification tool that I am developing. As I do not have access to actual devices nor the QEMU board configuration to test drivers that call this function, no runtime test was able to be performed. Fixes: 1c8fa58 ("regulator: Add generic DT parsing for regulators") Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp> Link: https://patch.msgid.link/20250104080453.2153592-1-joe@pf.is.s.u-tokyo.ac.jp Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 64a6b57 commit dddca3b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

drivers/regulator/of_regulator.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ int of_regulator_match(struct device *dev, struct device_node *node,
446446
"failed to parse DT for regulator %pOFn\n",
447447
child);
448448
of_node_put(child);
449-
return -EINVAL;
449+
goto err_put;
450450
}
451451
match->of_node = of_node_get(child);
452452
count++;
@@ -455,6 +455,18 @@ int of_regulator_match(struct device *dev, struct device_node *node,
455455
}
456456

457457
return count;
458+
459+
err_put:
460+
for (i = 0; i < num_matches; i++) {
461+
struct of_regulator_match *match = &matches[i];
462+
463+
match->init_data = NULL;
464+
if (match->of_node) {
465+
of_node_put(match->of_node);
466+
match->of_node = NULL;
467+
}
468+
}
469+
return -EINVAL;
458470
}
459471
EXPORT_SYMBOL_GPL(of_regulator_match);
460472

0 commit comments

Comments
 (0)