[PATCH] fix memleak in fs/jffs2/scan.c (was: re: [CHECKER] 32 Memory Leaks on Error Paths)

From: Jörn Engel
Date: Tue Sep 16 2003 - 02:25:14 EST


On Tue, 16 September 2003 08:55:53 +0200, Jörn Engel wrote:
>
> > [FILE: 2.6.0-test5/fs/jffs2/scan.c]
> > [FUNC: jffs2_scan_medium]
> > [LINES: 98-109]
> > [VAR: flashbuf]
> > 93: buf_size = c->sector_size;
> > 94: else
> > 95: buf_size = PAGE_SIZE;
> > 96:
> > 97: D1(printk(KERN_DEBUG "Allocating readbuf of %d bytes\n", buf_size));
> > START -->
> > 98: flashbuf = kmalloc(buf_size, GFP_KERNEL);
> > 99: if (!flashbuf)
> > 100: return -ENOMEM;
> > 101: }
> > 102:
> > 103: for (i=0; i<c->nr_blocks; i++) {
> > 104: struct jffs2_eraseblock *jeb = &c->blocks[i];
> > 105:
> > 106: ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset), buf_size);
> > 107:
> > 108: if (ret < 0)
> > END -->
> > 109: return ret;
> > 110:
> > 111: ACCT_PARANOIA_CHECK(jeb);
> > 112:
> > 113: /* Now decide which list to put it on */
> > 114: switch(ret) {
>
> Valid. And not trivial to fix.

But at least trivial to band-aid around it. This doesn't make the
function any nicer, but it should get rid of the leaks.

David, consider this to be public domain. :)

Jörn

--
Mundie uses a textbook tactic of manipulation: start with some
reasonable talk, and lead the audience to an unreasonable conclusion.
-- Bruce Perens

--- linux-2.6.0-test3/fs/jffs2/scan.c~jffs2_memleak 2003-07-05 23:59:33.000000000 +0200
+++ linux-2.6.0-test3/fs/jffs2/scan.c 2003-09-16 09:16:30.000000000 +0200
@@ -106,7 +106,7 @@
ret = jffs2_scan_eraseblock(c, jeb, buf_size?flashbuf:(flashbuf+jeb->offset), buf_size);

if (ret < 0)
- return ret;
+ goto out;

ACCT_PARANOIA_CHECK(jeb);

@@ -230,7 +230,8 @@
if ( !c->used_size && ((empty_blocks+bad_blocks)!= c->nr_blocks || bad_blocks == c->nr_blocks) ) {
printk(KERN_NOTICE "Cowardly refusing to erase blocks on filesystem with no valid JFFS2 nodes\n");
printk(KERN_NOTICE "empty_blocks %d, bad_blocks %d, c->nr_blocks %d\n",empty_blocks,bad_blocks,c->nr_blocks);
- return -EIO;
+ ret = -EIO;
+ goto out;
}
jffs2_erase_pending_trigger(c);
}
@@ -241,6 +242,10 @@
c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
#endif
return 0;
+out:
+ if (buf_size)
+ kfree(flashbuf);
+ return ret;
}

static int jffs2_fill_scan_buf (struct jffs2_sb_info *c, unsigned char *buf,
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/