mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lru_cache: Use correct type in sizeof for allocation
@ 2011-02-20 22:45 Ilia Mirkin
  2011-02-21  8:44 ` [Drbd-dev] " Lars Ellenberg
  2011-04-07 21:19 ` Andrew Morton
  0 siblings, 2 replies; 3+ messages in thread
From: Ilia Mirkin @ 2011-02-20 22:45 UTC (permalink / raw)
  To: drbd-dev; +Cc: linux-kernel, Ilia Mirkin

This has no actual effect, since sizeof(struct hlist_head) ==
sizeof(struct hlist_head *), but it's still the wrong type to use.

The semantic match that finds this problem:
// <smpl>
@@
type T;
identifier x;
@@
T *x;
...
* x = kzalloc(... * sizeof(T*) * ..., ...);
// </smpl>

Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu>

---
 lib/lru_cache.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

Note that in lc_reset, there is the line

memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements);

So this seems correct. But otherwise untested.

diff --git a/lib/lru_cache.c b/lib/lru_cache.c
index 270de9d..b312c8f 100644
--- a/lib/lru_cache.c
+++ b/lib/lru_cache.c
@@ -84,7 +84,7 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
 	if (e_count > LC_MAX_ACTIVE)
 		return NULL;
 
-	slot = kzalloc(e_count * sizeof(struct hlist_head*), GFP_KERNEL);
+	slot = kzalloc(e_count * sizeof(struct hlist_head), GFP_KERNEL);
 	if (!slot)
 		goto out_fail;
 	element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);
-- 
1.7.3.4


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

* Re: [Drbd-dev] [PATCH] lru_cache: Use correct type in sizeof for allocation
  2011-02-20 22:45 [PATCH] lru_cache: Use correct type in sizeof for allocation Ilia Mirkin
@ 2011-02-21  8:44 ` Lars Ellenberg
  2011-04-07 21:19 ` Andrew Morton
  1 sibling, 0 replies; 3+ messages in thread
From: Lars Ellenberg @ 2011-02-21  8:44 UTC (permalink / raw)
  To: Ilia Mirkin; +Cc: drbd-dev, linux-kernel

On Sun, Feb 20, 2011 at 05:45:14PM -0500, Ilia Mirkin wrote:
> This has no actual effect, since sizeof(struct hlist_head) ==
> sizeof(struct hlist_head *), but it's still the wrong type to use.
> 
> The semantic match that finds this problem:
> // <smpl>
> @@
> type T;
> identifier x;
> @@
> T *x;
> ...
> * x = kzalloc(... * sizeof(T*) * ..., ...);
> // </smpl>
> 
> Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu>

  Acked-by: Lars Ellenberg <lars@linbit.com>

> 
> ---
>  lib/lru_cache.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> Note that in lc_reset, there is the line
> 
> memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements);
> 
> So this seems correct. But otherwise untested.
> 
> diff --git a/lib/lru_cache.c b/lib/lru_cache.c
> index 270de9d..b312c8f 100644
> --- a/lib/lru_cache.c
> +++ b/lib/lru_cache.c
> @@ -84,7 +84,7 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
>  	if (e_count > LC_MAX_ACTIVE)
>  		return NULL;
>  
> -	slot = kzalloc(e_count * sizeof(struct hlist_head*), GFP_KERNEL);
> +	slot = kzalloc(e_count * sizeof(struct hlist_head), GFP_KERNEL);

Luckily sizeof(struct hlist_head*) == sizeof(struct hlist_head),
so this went unnoticed, as it did not have any effect.

Thanks for having this spotted by semantic matching.

	Lars

>  	if (!slot)
>  		goto out_fail;
>  	element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);
> -- 
> 1.7.3.4

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

* Re: [PATCH] lru_cache: Use correct type in sizeof for allocation
  2011-02-20 22:45 [PATCH] lru_cache: Use correct type in sizeof for allocation Ilia Mirkin
  2011-02-21  8:44 ` [Drbd-dev] " Lars Ellenberg
@ 2011-04-07 21:19 ` Andrew Morton
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2011-04-07 21:19 UTC (permalink / raw)
  To: Ilia Mirkin; +Cc: drbd-dev, linux-kernel

On Sun, 20 Feb 2011 17:45:14 -0500
Ilia Mirkin <imirkin@alum.mit.edu> wrote:

> This has no actual effect, since sizeof(struct hlist_head) ==
> sizeof(struct hlist_head *), but it's still the wrong type to use.
> 
> The semantic match that finds this problem:
> // <smpl>
> @@
> type T;
> identifier x;
> @@
> T *x;
> ...
> * x = kzalloc(... * sizeof(T*) * ..., ...);
> // </smpl>
> 
> ...
>
> --- a/lib/lru_cache.c
> +++ b/lib/lru_cache.c
> @@ -84,7 +84,7 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
>  	if (e_count > LC_MAX_ACTIVE)
>  		return NULL;
>  
> -	slot = kzalloc(e_count * sizeof(struct hlist_head*), GFP_KERNEL);
> +	slot = kzalloc(e_count * sizeof(struct hlist_head), GFP_KERNEL);
>  	if (!slot)
>  		goto out_fail;
>  	element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);

This is one of the reasons why I think it's better to use

	foo = kmalloc(sizeof(*foo));

Then, you just *know* it's correct by looking at the code and you don't
need to scroll up and double-check the type of foo.

The code as you have it is still vulnerable to multiplicative overflow.
So, to be really really correct,


--- a/lib/lru_cache.c~lru_cache-use-correct-type-in-sizeof-for-allocation-fix
+++ a/lib/lru_cache.c
@@ -84,7 +84,7 @@ struct lru_cache *lc_create(const char *
 	if (e_count > LC_MAX_ACTIVE)
 		return NULL;
 
-	slot = kzalloc(e_count * sizeof(struct hlist_head), GFP_KERNEL);
+	slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL);
 	if (!slot)
 		goto out_fail;
 	element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);
_


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

end of thread, other threads:[~2011-04-07 21:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-20 22:45 [PATCH] lru_cache: Use correct type in sizeof for allocation Ilia Mirkin
2011-02-21  8:44 ` [Drbd-dev] " Lars Ellenberg
2011-04-07 21:19 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®