Skip to content

Commit

Permalink
lib/lru_cache: fix error free handing in lc_create
Browse files Browse the repository at this point in the history
When kmem_cache_alloc in function lc_create returns null, we will
free the memory already allocated. The loop of kmem_cache_free
is wrong, especially:
  i = 0  ==> do wrong loop
  i > 0  ==> do not free element[0]

Link: https://lkml.kernel.org/r/20220618082521.7082-1-wuchi.zero@gmail.com
Signed-off-by: wuchi <wuchi.zero@gmail.com>
Cc: Philipp Reisner <philipp.reisner@linbit.com>
Cc: Lars Ellenberg <lars.ellenberg@linbit.com>
Cc: Christoph Bhmwalder <christoph.boehmwalder@linbit.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
wuchi authored and akpm committed Jul 18, 2022
1 parent 5a70462 commit 5a66fce
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/lru_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
return lc;

/* else: could not allocate all elements, give up */
for (i--; i; i--) {
void *p = element[i];
while (i) {
void *p = element[--i];
kmem_cache_free(cache, p - e_off);
}
kfree(lc);
Expand Down

0 comments on commit 5a66fce

Please sign in to comment.