pinctrl: imx: fix memory leak
Subsystems (4)
PATCH v2 1/1 pinctrl: imx: fix memory leak
Each time set_state is called, a new piece memory will be allocated for pin_data, but not freed, this will incur memory leak.
When error, the devm API could not free memory automatically. So need call devm_kfree when error.
Issue reported by Coverity
--- drivers/pinctrl/nxp/pinctrl-imx.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/pinctrl/nxp/pinctrl-imx.c +4 -0
@@ -53,6 +53,7 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)if (fdtdec_get_int_array (gd->fdt_blob, node,"fsl,pins" , pin_data, size >>2 )) {dev_err (dev,"Error reading pin data.\n" ); +devm_kfree (dev, pin_data);return -EINVAL ; }
@@ -78,6 +79,7 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)if ((mux_reg ==-1 ) || (conf_reg ==-1 )) {dev_err (dev,"Error mux_reg or conf_reg\n" ); +devm_kfree (dev, pin_data);return -EINVAL ; }
@@ -166,6 +168,8 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) } } +devm_kfree (dev, pin_data); +return 0 ; }
Signature · 1 line
2.6.2
PATCH v2 1/1 pinctrl: imx: fix memory leak
Quoted 39 lines · Peng Fan
Each time
set_stateis called, a new piece memory will be allocated forpin_data, but not freed, this will incur memory leak.When error, the devm API could not free memory automatically. So need call
devm_kfreewhen error.Issue reported by Coverity
Signed-off-by Peng Fan <[email protected]>Cc Simon Glass <[email protected]>Cc Stefan Agner <[email protected]>Cc Stefano Babic <[email protected]>--- drivers/pinctrl/nxp/pinctrl-imx.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/pinctrl/nxp/pinctrl-imx.c +2
-0@@ -53,6 +53,7 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)if (fdtdec_get_int_array (gd->fdt_blob, node,"fsl,pins" , pin_data, size >>2 )) {dev_err (dev,"Error reading pin data.\n" ); +devm_kfree (dev, pin_data);return -EINVAL ; }@@ -78,6 +79,7 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)if ((mux_reg ==-1 ) || (conf_reg ==-1 )) {dev_err (dev,"Error mux_reg or conf_reg\n" ); +devm_kfree (dev, pin_data);return -EINVAL ; }
drivers/pinctrl/nxp/pinctrl-imx.c static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) · line 175
Quoted 8 lines
drivers/pinctrl/nxp/pinctrl-imx.c +2
-0@@ -166,6 +168,8 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) } } +devm_kfree (dev, pin_data); +return 0 ; }
Applied to u-boot-imx, thanks !
Best regards,
Stefano Babic
Signature · 5 lines
===================================================================== DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic at denx.de =====================================================================
PATCH v2 1/1 pinctrl: imx: fix memory leak
Hi,
Quoted 42 lines · On Thu, 18 May 2017 11:15:08 +0200 Stefano Babic wrote:
Quoted 41 lines · Peng Fan
Each time
set_stateis called, a new piece memory will be allocated forpin_data, but not freed, this will incur memory leak.When error, the devm API could not free memory automatically. So need call
devm_kfreewhen error.Issue reported by Coverity
Signed-off-by Peng Fan <[email protected]>Cc Simon Glass <[email protected]>Cc Stefan Agner <[email protected]>Cc Stefano Babic <[email protected]>--- drivers/pinctrl/nxp/pinctrl-imx.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/pinctrl/nxp/pinctrl-imx.c +2
-0@@ -53,6 +53,7 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)if (fdtdec_get_int_array (gd->fdt_blob, node,"fsl,pins" , pin_data, size >>2 )) {dev_err (dev,"Error reading pin data.\n" ); +devm_kfree (dev, pin_data);return -EINVAL ; }@@ -78,6 +79,7 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)if ((mux_reg ==-1 ) || (conf_reg ==-1 )) {dev_err (dev,"Error mux_reg or conf_reg\n" ); +devm_kfree (dev, pin_data);return -EINVAL ; }@@ -166,6 +168,8 @@ static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) } }
Quoted 9 lines
drivers/pinctrl/nxp/pinctrl-imx.c static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) · line 175
Quoted 6 lines
drivers/pinctrl/nxp/pinctrl-imx.c +2
-0+devm_kfree (dev, pin_data); +return 0 ; }Applied to u-boot-imx, thanks !
I had commented on this patch, but unfortunately my mails were rejected by the mailing list server, so the only reached the individuals on CC.
IMO the use of the 'devm_' functions is inappropriate here, since the allocated buffer is only used temporarily inside the function and unconditionally freed at function exit.
Lothar Waßmann