2021-10-25 21:49:37

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/5] tty: rpmsg: Assign returned id to a local variable

Instead of putting garbage in the data structure, assign allocated id
or an error code to a temporary variable. This makes code cleaner.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/tty/rpmsg_tty.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/rpmsg_tty.c b/drivers/tty/rpmsg_tty.c
index 813076341ffd..8c17ddbf371d 100644
--- a/drivers/tty/rpmsg_tty.c
+++ b/drivers/tty/rpmsg_tty.c
@@ -121,15 +121,16 @@ static struct rpmsg_tty_port *rpmsg_tty_alloc_cport(void)
return ERR_PTR(-ENOMEM);

mutex_lock(&idr_lock);
- cport->id = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
+ err = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
mutex_unlock(&idr_lock);

- if (cport->id < 0) {
- err = cport->id;
+ if (err < 0) {
kfree(cport);
return ERR_PTR(err);
}

+ cport->id = err;
+
return cport;
}

--
2.33.0


2021-11-02 13:22:46

by Arnaud Pouliquen

[permalink] [raw]
Subject: Re: [PATCH v1 1/5] tty: rpmsg: Assign returned id to a local variable

Hi Andy,

On 10/25/21 3:51 PM, Andy Shevchenko wrote:
> Instead of putting garbage in the data structure, assign allocated id
> or an error code to a temporary variable. This makes code cleaner.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
> drivers/tty/rpmsg_tty.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/tty/rpmsg_tty.c b/drivers/tty/rpmsg_tty.c
> index 813076341ffd..8c17ddbf371d 100644
> --- a/drivers/tty/rpmsg_tty.c
> +++ b/drivers/tty/rpmsg_tty.c
> @@ -121,15 +121,16 @@ static struct rpmsg_tty_port *rpmsg_tty_alloc_cport(void)
> return ERR_PTR(-ENOMEM);
>
> mutex_lock(&idr_lock);
> - cport->id = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
> + err = idr_alloc(&tty_idr, cport, 0, MAX_TTY_RPMSG, GFP_KERNEL);
> mutex_unlock(&idr_lock);
>
> - if (cport->id < 0) {
> - err = cport->id;
> + if (err < 0) {
> kfree(cport);
> return ERR_PTR(err);
> }
>
> + cport->id = err;

Set the cport->id to the err variable on success doesn't seem completely clean
to me either.
What about renaming "err" by "id" as done in [1]?

[1]
https://elixir.bootlin.com/linux/latest/source/drivers/tty/serial/mps2-uart.c#L526

Regards,
Arnaud

> +
> return cport;
> }
>
>

2021-11-02 14:44:45

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 1/5] tty: rpmsg: Assign returned id to a local variable

On Tue, Nov 2, 2021 at 3:21 PM Arnaud POULIQUEN
<[email protected]> wrote:
> On 10/25/21 3:51 PM, Andy Shevchenko wrote:

...

> > + cport->id = err;
>
> Set the cport->id to the err variable on success doesn't seem completely clean
> to me either.

I totally agree with you, that's why the
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git/commit/?id=408a507996e4
changed it.

--
With Best Regards,
Andy Shevchenko