From: Andrew Morton <akpm@linux-foundation.org>
To: Ilia Mirkin <imirkin@alum.mit.edu>
Cc: drbd-dev@lists.linbit.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] lru_cache: Use correct type in sizeof for allocation
Date: Thu, 7 Apr 2011 14:19:42 -0700 [thread overview]
Message-ID: <20110407141942.9d303e8c.akpm@linux-foundation.org> (raw)
In-Reply-To: <1298241914-1400-1-git-send-email-imirkin@alum.mit.edu>
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);
_
prev parent reply other threads:[~2011-04-07 21:19 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-20 22:45 Ilia Mirkin
2011-02-21 8:44 ` [Drbd-dev] " Lars Ellenberg
2011-04-07 21:19 ` Andrew Morton [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110407141942.9d303e8c.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=drbd-dev@lists.linbit.com \
--cc=imirkin@alum.mit.edu \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®