Re: [PATCH] shm: Remove silly double assignment

From: Andrew Morton
Date: Wed Jun 11 2008 - 06:46:03 EST


On Tue, 10 Jun 2008 08:53:39 -0400 Neil Horman <nhorman@xxxxxxxxxxxxx> wrote:

> Found a silly double assignment of err is do_shmat. Silly, but good to clean up
> the useless code.
>
> Signed-off-by: Neil Horman <nhorman@xxxxxxxxxxxxx>
>
> diff --git a/ipc/shm.c b/ipc/shm.c
> index 554429a..d05f6b5 100644
> --- a/ipc/shm.c
> +++ b/ipc/shm.c
> @@ -894,8 +894,6 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
> if (!sfd)
> goto out_put_dentry;
>
> - err = -ENOMEM;
> -
> file = alloc_file(path.mnt, path.dentry, f_mode, &shm_file_operations);
> if (!file)
> goto out_free;

Not silly, really. Look:

err = -ENOMEM;
if (expr1)
goto out;

err = -ENOMEM;
if (expr2)
goto out;

each of these two units is a separate, self-contained clause. Removing
the second assignment to `err' breaks that separation and will make one
clause undesirably dependent upon the other.

Example: if someone later comes up and does

err = -ENOMEM;
if (expr1)
goto out;

+ er = -EINVAL;
+ if (expr3)
+ goto out;

if (expr2)
goto out;

then whoops, it broke.

The compiler should optimise away the second assignment anyway.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/