All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
To: linux-kernel@vger.kernel.org
Subject: [2.4.19] [2.4.20-pre6] loop.c memory allocation & freeing problem
Date: Thu, 12 Sep 2002 22:28:29 +0200	[thread overview]
Message-ID: <20020912201951.1CAA7446BA@kraid.nerim.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 1795 bytes --]

Hi All!

I think I've spotted a bug in the initialisation procedure in the loop driver,
@line 1027-1060, when there is allocation failure:

----BEGIN CODE----
        loop_dev = kmalloc(max_loop * sizeof(struct loop_device), GFP_KERNEL);
        if (!loop_dev)
                return -ENOMEM;

        loop_sizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
        if (!loop_sizes)
                goto out_sizes;

        loop_blksizes = kmalloc(max_loop * sizeof(int), GFP_KERNEL);
        if (!loop_blksizes)
                goto out_blksizes;

[...SNIP...]

out_sizes:
        kfree(loop_dev);
out_blksizes:
        kfree(loop_sizes);
----END CODE----

Two problems arise there:
1) Should allocation of loop_sizes fail, we'll try to free loop_sizes that
   was NOT allocated (no problem because kfree does not free a NULL pointer).
   But not very clean...

2) Should allocation of loop_blksizes fail, loop_dev will NOT be freed.
   That one looks more problematic.

Am I wrong? Have I missed something? Anyway it's been there for ages, I
suppose.

Patch is very straigthforward, and is attached : swap the last 2 pairs of
lines, so that out_blksizes is before out_sizes, and kfree(loop_sizes is
before kfree(loop_dev).

Please CC: answers and comments as I'm no longer subscribed.

Cheers,
Yann.

-- 
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
|  0 662 376 056  | Software  Designer | \ / CAMPAIGN     |  ___               |
| --==< °_° >==-- °---.----------------:  X  AGAINST      |  \e/  There is no  |
| web: ymorin.free.fr | SETI@home  433 | / \ HTML MAIL    |   v   conspiracy.  |
°---------------------°----------------°------------------°--------------------°

[-- Attachment #2: loop.c.diff --]
[-- Type: text/plain, Size: 346 bytes --]

--- loop.c.old	Thu Sep 12 21:19:40 2002
+++ loop.c	Thu Sep 12 21:19:56 2002
@@ -1054,10 +1054,10 @@
 	printk(KERN_INFO "loop: loaded (max %d devices)\n", max_loop);
 	return 0;
 
-out_sizes:
-	kfree(loop_dev);
 out_blksizes:
 	kfree(loop_sizes);
+out_sizes:
+	kfree(loop_dev);
 	printk(KERN_ERR "loop: ran out of memory\n");
 	return -ENOMEM;
 }

                 reply	other threads:[~2002-09-13  3:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20020912201951.1CAA7446BA@kraid.nerim.net \
    --to=yann.morin.1998@anciens.enib.fr \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.