freemyipod r654 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r653‎ | r654 | r655 >
Date:01:34, 24 March 2011
Author:theseven
Status:new
Tags:
Comment:
libmkfat32: Remove memory leak if disk access fails
Modified paths:
  • /emcore/trunk/util.h (modified) (history)
  • /libs/mkfat32/main.c (modified) (history)

Diff [purge]

Index: libs/mkfat32/main.c
@@ -72,7 +72,7 @@
7373 memcpy(&((uint8_t*)buf)[0x47], label, 0xb);
7474 memcpy(&((uint8_t*)buf)[0x52], "FAT32 ", 8);
7575 ((uint16_t*)buf)[0xff] = 0xaa55;
76 - PASS_RC(storage_write_sectors_md(volume, startsector, 1, buf), 2, 0);
 76+ PASS_RC_FREE(storage_write_sectors_md(volume, startsector, 1, buf), 2, 0, buf);
7777 memset(buf, 0, sectorsize);
7878 buf[0] = 0x41615252;
7979 buf[0x79] = 0x61417272;
@@ -79,7 +79,7 @@
8080 buf[0x7a] = clustercount - 1;
8181 buf[0x7b] = 2;
8282 buf[0x7f] = 0xaa550000;
83 - PASS_RC(storage_write_sectors_md(volume, startsector + 1, 1, buf), 2, 1);
 83+ PASS_RC_FREE(storage_write_sectors_md(volume, startsector + 1, 1, buf), 2, 1, buf);
8484 statusinit(statususer, fatsectors);
8585 uint32_t cursect = 0;
8686 for (i = 0; i < fatsectors; i += 32)
@@ -86,14 +86,15 @@
8787 {
8888 memset(buf, 0, 32 * sectorsize);
8989 if (!i) memcpy(buf, "\xf8\xff\xff\x0f\xff\xff\xff\xff\xff\xff\xff\x0f", 12);
90 - PASS_RC(storage_write_sectors_md(volume, startsector + reserved + i,
91 - MIN(fatsectors - i, 32), buf), 2, 2);
 90+ PASS_RC_FREE(storage_write_sectors_md(volume, startsector + reserved + i,
 91+ MIN(fatsectors - i, 32), buf), 2, 2, buf);
9292 statuscallback(statususer, i);
9393 }
9494 memset(buf, 0, secperclus * sectorsize);
9595 memcpy(buf, label, 11);
9696 ((uint8_t*)buf)[0xc] = 0x80;
97 - PASS_RC(storage_write_sectors_md(volume, startsector + database, secperclus, buf), 2, 3);
 97+ PASS_RC_FREE(storage_write_sectors_md(volume, startsector + database,
 98+ secperclus, buf), 2, 3, buf);
9899 free(buf);
99100 disk_mount(volume);
100101 }
Index: emcore/trunk/util.h
@@ -60,6 +60,11 @@
6161 mutex_unlock(mutex); \
6262 return ERR_RC(val); \
6363 }
 64+#define RET_ERR_FREE(val, ptr) \
 65+{ \
 66+ free(ptr); \
 67+ return ERR_RC(val); \
 68+}
6469 #define PASS_RC(expr, bits, val) \
6570 { \
6671 int PASS_RC_rc = (expr); \
@@ -75,6 +80,15 @@
7681 return ERR_RC((PASS_RC_MTX_rc << (bits)) | (val)); \
7782 } \
7883 }
 84+#define PASS_RC_FREE(expr, bits, val, ptr) \
 85+{ \
 86+ int PASS_RC_FREE_rc = (expr); \
 87+ if (IS_ERR(PASS_RC_FREE_rc)) \
 88+ { \
 89+ free(ptr); \
 90+ return ERR_RC((PASS_RC_FREE_rc << (bits)) | (val)); \
 91+ } \
 92+}
7993
8094 #define P2_M1(p2) ((1 << (p2))-1)
8195