media: v4l2-subdev: fix some NULL vs IS_ERR() checks
Subsystems (6)
PATCH 1/1 media: v4l2-subdev: fix some NULL vs IS_ERR() checks
The v4l2_subdev_alloc_state() function returns error pointers, it doesn't return NULL.
--- drivers/media/platform/vsp1/vsp1_entity.c | 4 ++-- drivers/staging/media/tegra-video/vi.c | 4 ++-- drivers/media/platform/rcar-vin/rcar-v4l2.c | 4 ++-- 3 file changed, 6 insertions(+), 6 deletions(-)
drivers/media/platform/vsp1/vsp1_entity.c +2 -2
@@ -676,9 +676,9 @@ int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,* rectangles. */ entity->config =v4l2_subdev_alloc_state (&entity->subdev); -if (entity->config ==NULL ) { +if (IS_ERR (entity->config)) {media_entity_cleanup (&entity->subdev.entity); -return -ENOMEM ; +return PTR_ERR (entity->config); }return 0 ;
drivers/staging/media/tegra-video/vi.c +2 -2
@@ -508,8 +508,8 @@ static int __tegra_channel_try_format(struct tegra_vi_channel *chan,return -ENODEV ; sd_state =v4l2_subdev_alloc_state (subdev); -if (!sd_state) -return -ENOMEM ; +if (IS_ERR (sd_state)) +return PTR_ERR (sd_state);/* * Retrieve the format information and if requested format isn't * supported, keep the current format.
drivers/media/platform/rcar-vin/rcar-v4l2.c +2 -2
@@ -253,8 +253,8 @@ static int rvin_try_format(struct rvin_dev *vin, u32 which,int ret; sd_state =v4l2_subdev_alloc_state (sd); -if (sd_state ==NULL ) -return -ENOMEM ; +if (IS_ERR (sd_state)) +return PTR_ERR (sd_state);if (!rvin_format_from_pixel (vin, pix->pixelformat)) pix->pixelformat =RVIN_DEFAULT_FORMAT ;
Signature · 1 line
2.30.2
Hi Dan,
Thank you for the patch.
Quoted 2 lines · Dan Carpenter
The
v4l2_subdev_alloc_state()function returns error pointers, it doesn't return NULL.
It's funny you send this patch today, I've been thinking about the exact same issue yesterday, albeit more globally, when trying to figure out if a function I called returned NULL or an error pointer on error.
Would it make to create an __err_ptr annotation to mark functions that return an error pointer ? This would both give a simple indication to the user and allow tools such as smatch to detect errors.
Quoted 2 lines
Fixes 0d346d2a6f54 ("media: v4l2-subdev: add subdev-wide state struct")Signed-off-by Dan Carpenter <[email protected]>
Mauro, can you pick this patch up ?
Quoted 52 lines
--- drivers/media/platform/vsp1/vsp1_entity.c | 4 ++-- drivers/staging/media/tegra-video/vi.c | 4 ++-- drivers/media/platform/rcar-vin/rcar-v4l2.c | 4 ++-- 3 file changed, 6 insertions(+), 6 deletions(-)drivers/media/platform/vsp1/vsp1_entity.c +2
-2@@ -676,9 +676,9 @@ int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,* rectangles. */ entity->config =v4l2_subdev_alloc_state (&entity->subdev); -if (entity->config ==NULL ) { +if (IS_ERR (entity->config)) {media_entity_cleanup (&entity->subdev.entity); -return -ENOMEM ; +return PTR_ERR (entity->config); }return 0 ;drivers/staging/media/tegra-video/vi.c +2
-2@@ -508,8 +508,8 @@ static int __tegra_channel_try_format(struct tegra_vi_channel *chan,return -ENODEV ; sd_state =v4l2_subdev_alloc_state (subdev); -if (!sd_state) -return -ENOMEM ; +if (IS_ERR (sd_state)) +return PTR_ERR (sd_state);/* * Retrieve the format information and if requested format isn't * supported, keep the current format. drivers/media/platform/rcar-vin/rcar-v4l2.c +2
-2@@ -253,8 +253,8 @@ static int rvin_try_format(struct rvin_dev *vin, u32 which,int ret; sd_state =v4l2_subdev_alloc_state (sd); -if (sd_state ==NULL ) -return -ENOMEM ; +if (IS_ERR (sd_state)) +return PTR_ERR (sd_state);if (!rvin_format_from_pixel (vin, pix->pixelformat)) pix->pixelformat =RVIN_DEFAULT_FORMAT ;
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 8 lines · Laurent Pinchart
Hi Dan,
Thank you for the patch.
Quoted 2 lines · Dan Carpenter
The
v4l2_subdev_alloc_state()function returns error pointers, it doesn't return NULL.
Quoted 8 lines
It's funny you send this patch today, I've been thinking about the exact same issue yesterday, albeit more globally, when trying to figure out if a function I called returned NULL or an error pointer on error.
Would it make to create an
__err_ptrannotation to mark functions that return an error pointer ? This would both give a simple indication to the user and allow tools such as smatch to detect errors.
If you have the cross function DB enabled then Smatch can figure out if it returns error pointers or NULL. The big problem is that Smatch works on the precompiled code and doesn't understand ifdeffed code.
I haven't pushed all the Smatch checks. I told someone last month, I'd give them a month to fix any bugs since it was their idea. But I'll push it soon.
#if IS_ENABLED(CONFIG)
function returns error pointer or valid
#else
struct foo *function() { return NULL; }
#endif
I believe that there are also people who use a two pass Coccinelle system where they make a list of functions that return error pointers and then check the callers.
The Huawei devs find a bunch of these bugs through static analysis but I don't know which tools they are using.
Today, I accidentally introduced a bug by converting a call that can "in theory/the future return error pointers" but also returns NULL at the end of a list. I thought it was only supposed to be checked for NULLs. Fortunately Colin King found it right away. That was just sloppiness on my part :/ and it's pretty rare to find code like that.
regards,
dan carpenter
Quoted 30 lines · Dan Carpenter
Quoted 7 lines · Laurent Pinchart
Hi Dan,
Thank you for the patch.
Quoted 2 lines · Dan Carpenter
The
v4l2_subdev_alloc_state()function returns error pointers, it doesn't return NULL.Quoted 8 lines
It's funny you send this patch today, I've been thinking about the exact same issue yesterday, albeit more globally, when trying to figure out if a function I called returned NULL or an error pointer on error.
Would it make to create an
__err_ptrannotation to mark functions that return an error pointer ? This would both give a simple indication to the user and allow tools such as smatch to detect errors.If you have the cross function DB enabled then Smatch can figure out if it returns error pointers or NULL. The big problem is that Smatch works on the precompiled code and doesn't understand ifdeffed code.
I haven't pushed all the Smatch checks. I told someone last month, I'd give them a month to fix any bugs since it was their idea. But I'll push it soon.
#if
IS_ENABLED(CONFIG)
function returns error pointer or valid
#else
struct foo*function(){ return NULL; }
#endif
Quoted 6 lines
I believe that there are also people who use a two pass Coccinelle system where they make a list of functions that return error pointers and then check the callers.
The Huawei devs find a bunch of these bugs through static analysis but I don't know which tools they are using.
Hi Dan,
We are using Coccinelle script to found them.
First step we using coccinelle script to found all the functions return
ERR_PTR or NULL, and do filter by checking all the users: at least we
found at least 5 callers, and all the caller check only NULL or ERR_PTR,
then we add them to function list.
Then using coccinelle script do analysis base on the function list give
in step 1. Just do the same thing like smatch.
Regards,
Wei Yongjun
Hi Dan,
Quoted 21 lines · Dan Carpenter
Quoted 11 lines · Laurent Pinchart
Quoted 2 lines · Dan Carpenter
The
v4l2_subdev_alloc_state()function returns error pointers, it doesn't return NULL.It's funny you send this patch today, I've been thinking about the exact same issue yesterday, albeit more globally, when trying to figure out if a function I called returned NULL or an error pointer on error.
Would it make to create an
__err_ptrannotation to mark functions that return an error pointer ? This would both give a simple indication to the user and allow tools such as smatch to detect errors.If you have the cross function DB enabled then Smatch can figure out if it returns error pointers or NULL. The big problem is that Smatch works on the precompiled code and doesn't understand ifdeffed code.
I haven't pushed all the Smatch checks. I told someone last month, I'd give them a month to fix any bugs since it was their idea. But I'll push it soon.
Quoted 5 lines
#if
IS_ENABLED(CONFIG)
function returns error pointer or valid
#else
struct foo*function(){ return NULL; }
#endif
Ouch, that hurts.
Quoted 4 lines
I believe that there are also people who use a two pass Coccinelle system where they make a list of functions that return error pointers and then check the callers.
Quoted 8 lines
The Huawei devs find a bunch of these bugs through static analysis but I don't know which tools they are using.
Today, I accidentally introduced a bug by converting a call that can "in theory/the future return error pointers" but also returns NULL at the end of a list. I thought it was only supposed to be checked for NULLs. Fortunately Colin King found it right away. That was just sloppiness on my part :/ and it's pretty rare to find code like that.
Do you think an annotation could still help, by making it explicit in headers whether a function returns NULL or an error pointer, thus helping developers get it right in the first place ?
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 4 lines · Laurent Pinchart
Do you think an annotation could still help, by making it explicit in headers whether a function returns NULL or an error pointer, thus helping developers get it right in the first place ?
Not really. It wouldn't help with Smatch. I really think error pointer bugs are handled pretty well currently. Sometimes I have seen syzbot find them before the static checkers but I don't see them affecting users and production kernels.
There are few other things that Smatch looks for like passing positives, valid pointers or NULLs to PTR_ERR(). I do wish that when functions return a mix of negative error codes, 0 and 1 that they had comment explaining what the 1 means.
regards,
dan carpenter
Quoted 10 lines · Dan Carpenter
Quoted 4 lines · Laurent Pinchart
Do you think an annotation could still help, by making it explicit in headers whether a function returns NULL or an error pointer, thus helping developers get it right in the first place ?
Not really. It wouldn't help with Smatch. I really think error pointer bugs are handled pretty well currently. Sometimes I have seen syzbot find them before the static checkers but I don't see them affecting users and production kernels.
I meant to ask if it would be useful for developers, not for smatch. When I use a function and have to figure out whether to use IS_ERR() or != NULL, I first look at the header, and most of the time I then need to find the corresponding implementation, wherever it may be. If we had an annotation, the second step could be skipped. Of course the annotation would need to match the implementation, and that's an area where smatch could help.
Quoted 4 lines
There are few other things that Smatch looks for like passing positives, valid pointers or NULLs to
PTR_ERR(). I do wish that when functions return a mix of negative error codes, 0 and 1 that they had comment explaining what the 1 means.
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 5 lines · Dan Carpenter
The
v4l2_subdev_alloc_state()function returns error pointers, it doesn't return NULL.Fixes 0d346d2a6f54 ("media: v4l2-subdev: add subdev-wide state struct")Signed-off-by Dan Carpenter <[email protected]>
Thanks, Dan!
Signature · 1 line
Sakari Ailus