2021-08-05 20:39:37

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/1] wwan: core: Avoid returning error pointer from wwan_create_dev()

wwan_create_dev() is expected to return either valid pointer or NULL,
In some cases it might return the error pointer. Prevent this by converting
it to NULL after wwan_dev_get_by_parent().

Fixes: 9a44c1cc6388 ("net: Add a WWAN subsystem")
Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/net/wwan/wwan_core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 674a81d79db3..35e10a98e774 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -160,7 +160,9 @@ static struct wwan_device *wwan_create_dev(struct device *parent)

/* If wwandev already exists, return it */
wwandev = wwan_dev_get_by_parent(parent);
- if (!IS_ERR(wwandev))
+ if (IS_ERR(wwandev))
+ wwandev = NULL;
+ else
goto done_unlock;

id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
--
2.30.2


2021-08-05 21:28:45

by Loic Poulain

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] wwan: core: Avoid returning error pointer from wwan_create_dev()

On Thu, 5 Aug 2021 at 20:38, Andy Shevchenko
<[email protected]> wrote:
>
> wwan_create_dev() is expected to return either valid pointer or NULL,
> In some cases it might return the error pointer. Prevent this by converting
> it to NULL after wwan_dev_get_by_parent().

wwan_create_dev is called both from wwan_register_ops() and
wwan_create_port(), one using IS_ERR and the other using NULL testing,
they should be aligned as well.

Regards,
Loic

2021-08-06 09:13:05

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] wwan: core: Avoid returning error pointer from wwan_create_dev()

On Thu, Aug 05, 2021 at 09:53:57PM +0200, Loic Poulain wrote:
> On Thu, 5 Aug 2021 at 20:38, Andy Shevchenko
> <[email protected]> wrote:
> >
> > wwan_create_dev() is expected to return either valid pointer or NULL,
> > In some cases it might return the error pointer. Prevent this by converting
> > it to NULL after wwan_dev_get_by_parent().
>
> wwan_create_dev is called both from wwan_register_ops() and
> wwan_create_port(), one using IS_ERR and the other using NULL testing,
> they should be aligned as well.

Ah, good catch!

I just sent v2, but eventually I have decided to switch to error pointer since
it seems the most used pattern in the code.

--
With Best Regards,
Andy Shevchenko


2021-08-06 09:15:54

by Sergey Ryazanov

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] wwan: core: Avoid returning error pointer from wwan_create_dev()

Hello Andy,

On Fri, Aug 6, 2021 at 12:08 PM Andy Shevchenko
<[email protected]> wrote:
> On Thu, Aug 05, 2021 at 09:53:57PM +0200, Loic Poulain wrote:
>> On Thu, 5 Aug 2021 at 20:38, Andy Shevchenko
>> <[email protected]> wrote:
>>>
>>> wwan_create_dev() is expected to return either valid pointer or NULL,
>>> In some cases it might return the error pointer. Prevent this by converting
>>> it to NULL after wwan_dev_get_by_parent().
>>
>> wwan_create_dev is called both from wwan_register_ops() and
>> wwan_create_port(), one using IS_ERR and the other using NULL testing,
>> they should be aligned as well.
>
> Ah, good catch!
>
> I just sent v2, but eventually I have decided to switch to error pointer since
> it seems the most used pattern in the code.

I agree that returning the error pointer is a good solution here.
Thank you for the fix.

--
Sergey