linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch] hw_random: cleanup in hwrng_register()
@ 2014-01-30 11:49 Dan Carpenter
  2014-01-31  5:48 ` Rusty Russell
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2014-01-30 11:49 UTC (permalink / raw)
  To: Matt Mackall
  Cc: Herbert Xu, Rusty Russell, Satoru Takeuchi, Paul Gortmaker,
	linux-kernel, kernel-janitors

My static checker complains that:

	drivers/char/hw_random/core.c:341 hwrng_register()
	warn: we tested 'old_rng' before and it was 'false'

The problem is that sometimes we test "if (!old_rng)" and sometimes we
test "if (must_register_misc)".  The static checker knows they are
equivalent but a human being reading the code could easily be confused.

I have simplified the code by removing the "must_register_misc" variable
and I have removed the redundant check on "if (!old_rng)".

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index b9495a8c05c6..463382036a01 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -301,7 +301,6 @@ err_misc_dereg:
 
 int hwrng_register(struct hwrng *rng)
 {
-	int must_register_misc;
 	int err = -EINVAL;
 	struct hwrng *old_rng, *tmp;
 
@@ -326,7 +325,6 @@ int hwrng_register(struct hwrng *rng)
 			goto out_unlock;
 	}
 
-	must_register_misc = (current_rng == NULL);
 	old_rng = current_rng;
 	if (!old_rng) {
 		err = hwrng_init(rng);
@@ -335,13 +333,11 @@ int hwrng_register(struct hwrng *rng)
 		current_rng = rng;
 	}
 	err = 0;
-	if (must_register_misc) {
+	if (!old_rng) {
 		err = register_miscdev();
 		if (err) {
-			if (!old_rng) {
-				hwrng_cleanup(rng);
-				current_rng = NULL;
-			}
+			hwrng_cleanup(rng);
+			current_rng = NULL;
 			goto out_unlock;
 		}
 	}

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [patch] hw_random: cleanup in hwrng_register()
  2014-01-30 11:49 [patch] hw_random: cleanup in hwrng_register() Dan Carpenter
@ 2014-01-31  5:48 ` Rusty Russell
  2014-02-09  9:22   ` Herbert Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Rusty Russell @ 2014-01-31  5:48 UTC (permalink / raw)
  To: Dan Carpenter, Matt Mackall
  Cc: Herbert Xu, Satoru Takeuchi, Paul Gortmaker, linux-kernel,
	kernel-janitors

Dan Carpenter <dan.carpenter@oracle.com> writes:
> My static checker complains that:
>
> 	drivers/char/hw_random/core.c:341 hwrng_register()
> 	warn: we tested 'old_rng' before and it was 'false'
>
> The problem is that sometimes we test "if (!old_rng)" and sometimes we
> test "if (must_register_misc)".  The static checker knows they are
> equivalent but a human being reading the code could easily be confused.
>
> I have simplified the code by removing the "must_register_misc" variable
> and I have removed the redundant check on "if (!old_rng)".
>
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Yeah, clearer too.

Reviewed-by: Rusty Russell <rusty@rustcorp.com.au>

Thanks,
Rusty.

>
> diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
> index b9495a8c05c6..463382036a01 100644
> --- a/drivers/char/hw_random/core.c
> +++ b/drivers/char/hw_random/core.c
> @@ -301,7 +301,6 @@ err_misc_dereg:
>  
>  int hwrng_register(struct hwrng *rng)
>  {
> -	int must_register_misc;
>  	int err = -EINVAL;
>  	struct hwrng *old_rng, *tmp;
>  
> @@ -326,7 +325,6 @@ int hwrng_register(struct hwrng *rng)
>  			goto out_unlock;
>  	}
>  
> -	must_register_misc = (current_rng == NULL);
>  	old_rng = current_rng;
>  	if (!old_rng) {
>  		err = hwrng_init(rng);
> @@ -335,13 +333,11 @@ int hwrng_register(struct hwrng *rng)
>  		current_rng = rng;
>  	}
>  	err = 0;
> -	if (must_register_misc) {
> +	if (!old_rng) {
>  		err = register_miscdev();
>  		if (err) {
> -			if (!old_rng) {
> -				hwrng_cleanup(rng);
> -				current_rng = NULL;
> -			}
> +			hwrng_cleanup(rng);
> +			current_rng = NULL;
>  			goto out_unlock;
>  		}
>  	}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [patch] hw_random: cleanup in hwrng_register()
  2014-01-31  5:48 ` Rusty Russell
@ 2014-02-09  9:22   ` Herbert Xu
  0 siblings, 0 replies; 3+ messages in thread
From: Herbert Xu @ 2014-02-09  9:22 UTC (permalink / raw)
  To: Rusty Russell
  Cc: Dan Carpenter, Matt Mackall, Satoru Takeuchi, Paul Gortmaker,
	linux-kernel, kernel-janitors

On Fri, Jan 31, 2014 at 04:18:51PM +1030, Rusty Russell wrote:
> Dan Carpenter <dan.carpenter@oracle.com> writes:
> > My static checker complains that:
> >
> > 	drivers/char/hw_random/core.c:341 hwrng_register()
> > 	warn: we tested 'old_rng' before and it was 'false'
> >
> > The problem is that sometimes we test "if (!old_rng)" and sometimes we
> > test "if (must_register_misc)".  The static checker knows they are
> > equivalent but a human being reading the code could easily be confused.
> >
> > I have simplified the code by removing the "must_register_misc" variable
> > and I have removed the redundant check on "if (!old_rng)".
> >
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> Yeah, clearer too.
> 
> Reviewed-by: Rusty Russell <rusty@rustcorp.com.au>

Patch applied.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-02-09  9:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-30 11:49 [patch] hw_random: cleanup in hwrng_register() Dan Carpenter
2014-01-31  5:48 ` Rusty Russell
2014-02-09  9:22   ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).