unvram: get rid of some memory leaks
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 27 Apr 2009 01:38:15 +0000 (01:38 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 27 Apr 2009 01:38:15 +0000 (01:38 +0000)
SVN-Revision: 15430

package/unvram/Makefile
package/unvram/src/nvram.c
package/unvram/src/nvram.h

index 2fab89a63414062200f9ea88a53f842edc743067..c62873e6fb20da6f0bf45dcda8921d51f9639490 100644 (file)
@@ -8,7 +8,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=unvram
-PKG_RELEASE:=2
+PKG_RELEASE:=3
 
 PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
 
index a5f1d43508984c25d2171b00c571997ea3635da2..78baa9431d3ae569fdd56b2523ba1f319a5d235c 100644 (file)
@@ -338,6 +338,7 @@ int nvram_commit(nvram_handle_t *h)
 nvram_handle_t * nvram_open(const char *file, int rdonly)
 {
        int fd;
+       char *mtd = NULL;
        nvram_handle_t *h;
        nvram_header_t *header;
 
@@ -345,16 +346,14 @@ nvram_handle_t * nvram_open(const char *file, int rdonly)
        if( (nvram_erase_size == 0) || (file == NULL) )
        {
                /* Finding the mtd will set the appropriate erase size */
-               if( file == NULL )
-                       file = nvram_find_mtd();
-               else
-                       (void) nvram_find_mtd();
-
-               if( nvram_erase_size == 0 )
+               if( (mtd = nvram_find_mtd()) == NULL || nvram_erase_size == 0 )
+               {
+                       free(mtd);
                        return NULL;
+               }
        }
 
-       if( (fd = open(file, O_RDWR)) > -1 )
+       if( (fd = open(file ? file : mtd, O_RDWR)) > -1 )
        {
                char *mmap_area = (char *) mmap(
                        NULL, nvram_erase_size, PROT_READ | PROT_WRITE,
@@ -377,6 +376,7 @@ nvram_handle_t * nvram_open(const char *file, int rdonly)
                                if( header->magic == NVRAM_MAGIC )
                                {
                                        _nvram_rehash(h);
+                                       free(mtd);
                                        return h;
                                }
                                else
@@ -388,6 +388,7 @@ nvram_handle_t * nvram_open(const char *file, int rdonly)
                }
        }
 
+       free(mtd);
        return NULL;
 }
 
@@ -403,7 +404,7 @@ int nvram_close(nvram_handle_t *h)
 }
 
 /* Determine NVRAM device node. */
-const char * nvram_find_mtd(void)
+char * nvram_find_mtd(void)
 {
        FILE *fp;
        int i, esz;
@@ -411,13 +412,11 @@ const char * nvram_find_mtd(void)
        char *path = NULL;
        struct stat s;
 
-       // "/dev/mtdblock/" + ( 0 < x < 99 ) + \0 = 19
-       if( (path = (char *) malloc(19)) == NULL )
-               return NULL;
-
-       if ((fp = fopen("/proc/mtd", "r"))) {
-               while (fgets(dev, sizeof(dev), fp)) {
-                       if (strstr(dev, "nvram") && sscanf(dev, "mtd%d: %08x", &i, &esz))
+       if( (fp = fopen("/proc/mtd", "r")) )
+       {
+               while( fgets(dev, sizeof(dev), fp) )
+               {
+                       if( strstr(dev, "nvram") && sscanf(dev, "mtd%d: %08x", &i, &esz) )
                        {
                                nvram_erase_size = esz;
 
@@ -451,7 +450,7 @@ const char * nvram_find_mtd(void)
 }
 
 /* Check NVRAM staging file. */
-const char * nvram_find_staging(void)
+char * nvram_find_staging(void)
 {
        struct stat s;
 
@@ -467,7 +466,7 @@ const char * nvram_find_staging(void)
 int nvram_to_staging(void)
 {
        int fdmtd, fdstg, stat;
-       const char *mtd = nvram_find_mtd();
+       char *mtd = nvram_find_mtd();
        char buf[nvram_erase_size];
 
        stat = -1;
@@ -492,6 +491,7 @@ int nvram_to_staging(void)
                }
        }
 
+       free(mtd);
        return stat;
 }
 
@@ -499,7 +499,7 @@ int nvram_to_staging(void)
 int staging_to_nvram(void)
 {
        int fdmtd, fdstg, stat;
-       const char *mtd = nvram_find_mtd();
+       char *mtd = nvram_find_mtd();
        char buf[nvram_erase_size];
 
        stat = -1;
@@ -526,5 +526,6 @@ int staging_to_nvram(void)
                }
        }
 
+       free(mtd);
        return stat;
 }
index b718928c0e57d0d11265fb3e2c1c2766acfa3fb9..e3d64613d488c1842429f9e0bfe010415fc5bfe7 100644 (file)
@@ -90,7 +90,7 @@ uint8_t hndcrc8 (uint8_t * pdata, uint32_t nbytes, uint8_t crc);
 uint8_t nvram_calc_crc(nvram_header_t * nvh);
 
 /* Determine NVRAM device node. */
-const char * nvram_find_mtd(void);
+char * nvram_find_mtd(void);
 
 /* Copy NVRAM contents to staging file. */
 int nvram_to_staging(void);
@@ -99,7 +99,7 @@ int nvram_to_staging(void);
 int staging_to_nvram(void);
 
 /* Check NVRAM staging file. */
-const char * nvram_find_staging(void);
+char * nvram_find_staging(void);
 
 
 /* Staging file for NVRAM */