pinctrl: sunxi: Fix a memory leak in 'sunxi_pinctrl_build_state()'
authorChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Tue, 16 Oct 2018 06:22:28 +0000 (08:22 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 1 Dec 2019 08:13:25 +0000 (09:13 +0100)
[ Upstream commit a93a676b079144009f55fff2ab0e34c3b7258c8a ]

If 'krealloc()' fails, 'pctl->functions' is set to NULL.
We should instead use a temp variable in order to be able to free the
previously allocated memeory, in case of OOM.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Acked-by: Maxime Ripard <maxime.ripard@bootlin.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/pinctrl/sunxi/pinctrl-sunxi.c

index 52edf3b5988d516b2d7e9c9ae0661c7947c38f57..cc8b86a16da0d8e9339c93e2e9f08dd6949ce1cf 100644 (file)
@@ -1039,6 +1039,7 @@ static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
 static int sunxi_pinctrl_build_state(struct platform_device *pdev)
 {
        struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
+       void *ptr;
        int i;
 
        /*
@@ -1105,13 +1106,15 @@ static int sunxi_pinctrl_build_state(struct platform_device *pdev)
        }
 
        /* And now allocated and fill the array for real */
-       pctl->functions = krealloc(pctl->functions,
-                                  pctl->nfunctions * sizeof(*pctl->functions),
-                                  GFP_KERNEL);
-       if (!pctl->functions) {
+       ptr = krealloc(pctl->functions,
+                      pctl->nfunctions * sizeof(*pctl->functions),
+                      GFP_KERNEL);
+       if (!ptr) {
                kfree(pctl->functions);
+               pctl->functions = NULL;
                return -ENOMEM;
        }
+       pctl->functions = ptr;
 
        for (i = 0; i < pctl->desc->npins; i++) {
                const struct sunxi_desc_pin *pin = pctl->desc->pins + i;