stm class: fix device leak in open error path
Subsystems (3)
PATCH 1/1 stm class: fix device leak in open error path
Make sure to drop the reference taken by class_find_device() also on allocation errors in open().
--- drivers/hwtracing/stm/core.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/hwtracing/stm/core.c +6 -3
@@ -368,8 +368,10 @@ static int stm_char_open(struct inode *inode, struct file *file)return -ENODEV ; stmf =kzalloc (sizeof (*stmf),GFP_KERNEL ); -if (!stmf) -return -ENOMEM ; +if (!stmf) { + err = -ENOMEM ; + goto err_put_device; + }stm_output_init (&stmf->output); stmf->stm =to_stm_device (dev);
@@ -382,9 +384,10 @@ static int stm_char_open(struct inode *inode, struct file *file)return nonseekable_open (inode, file); err_free: +kfree (stmf); +err_put_device:/* matches class_find_device() above */ put_device (dev); -kfree (stmf);return err; }
Signature · 1 line
2.7.3
Quoted 28 lines · Johan Hovold
Make sure to drop the reference taken by
class_find_device()also on allocation errors inopen().Fixes 7bd1d4093c2f ("stm class: Introduce an abstraction for...")Signed-off-by Johan Hovold <[email protected]>--- drivers/hwtracing/stm/core.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)drivers/hwtracing/stm/core.c +4
-2@@ -368,8 +368,10 @@ static int stm_char_open(struct inode *inode, struct file *file)return -ENODEV ; stmf =kzalloc (sizeof (*stmf),GFP_KERNEL ); -if (!stmf) -return -ENOMEM ; +if (!stmf) { + err = -ENOMEM ; + goto err_put_device; + }stm_output_init (&stmf->output); stmf->stm =to_stm_device (dev);@@ -382,9 +384,10 @@ static int stm_char_open(struct inode *inode, struct file *file)return nonseekable_open (inode, file);
drivers/hwtracing/stm/core.c static int stm_char_open(struct inode *inode, struct file *file) · line 393
Quoted 10 lines
drivers/hwtracing/stm/core.c +2
-1err_free: +kfree (stmf); +err_put_device:/* matches class_find_device() above */ put_device (dev); -kfree (stmf);return err; }
Any comments to this one, Alexander?
Thanks,
Johan
Alexander Shishkin
maintainerQuoted 5 lines · Johan Hovold
Make sure to drop the reference taken by
class_find_device()also on allocation errors inopen().Fixes 7bd1d4093c2f ("stm class: Introduce an abstraction for...")Signed-off-by Johan Hovold <[email protected]>
Good catch, thanks! I'm going to change it a bit to look like this if you don't mind:
drivers/hwtracing/stm/core.c +5 -3
@@ -361,7 +361,7 @@ static int stm_char_open(struct inode *inode, struct file *file)struct stm_file *stmf;struct device *dev;unsigned int major =imajor (inode); -int err = -ENODEV ; +int err = -ENOMEM ; dev =class_find_device (&stm_class,NULL , &major, major_match);if (!dev)
@@ -369,8 +369,9 @@ static int stm_char_open(struct inode *inode, struct file *file) stmf =kzalloc (sizeof (*stmf),GFP_KERNEL );if (!stmf) -return -ENOMEM ; + goto err_put_device; + err = -ENODEV ;stm_output_init (&stmf->output); stmf->stm =to_stm_device (dev);
@@ -382,9 +383,10 @@ static int stm_char_open(struct inode *inode, struct file *file)return nonseekable_open (inode, file); err_free: +kfree (stmf); +err_put_device:/* matches class_find_device() above */ put_device (dev); -kfree (stmf);return err; }
Regards,
--
Alex
Quoted 25 lines · Alexander Shishkin
Quoted 5 lines · Johan Hovold
Make sure to drop the reference taken by
class_find_device()also on allocation errors inopen().Fixes 7bd1d4093c2f ("stm class: Introduce an abstraction for...")Signed-off-by Johan Hovold <[email protected]>Good catch, thanks! I'm going to change it a bit to look like this if you don't mind:
drivers/hwtracing/stm/core.c +1
-1@@ -361,7 +361,7 @@ static int stm_char_open(struct inode *inode, struct file *file)struct stm_file *stmf;struct device *dev;unsigned int major =imajor (inode); -int err = -ENODEV ; +int err = -ENOMEM ; dev =class_find_device (&stm_class,NULL , &major, major_match);if (!dev)
drivers/hwtracing/stm/core.c static int stm_char_open(struct inode *inode, struct file *file) · line 376
Quoted 9 lines
drivers/hwtracing/stm/core.c +2
-1@@ -369,8 +369,9 @@ static int stm_char_open(struct inode *inode, struct file *file) stmf =kzalloc (sizeof (*stmf),GFP_KERNEL );if (!stmf) -return -ENOMEM ; + goto err_put_device; + err = -ENODEV ;stm_output_init (&stmf->output); stmf->stm =to_stm_device (dev);
Sure. I find it more readable to set the errno in the error path, but I don't mind.
Thanks,
Johan