nvram: fix memory leak
[openwrt/openwrt.git] / package / utils / nvram / src / cli.c
index 66ef904885854894b3d9dda6b40a1bd3e5c832f2..4f9c77804d793df4839fdd650f37f09fe9ee21fa 100644 (file)
 
 static nvram_handle_t * nvram_open_rdonly(void)
 {
-       const char *file = nvram_find_staging();
+       char *file = nvram_find_staging();
 
        if( file == NULL )
                file = nvram_find_mtd();
 
-       if( file != NULL )
-               return nvram_open(file, NVRAM_RO);
+       if( file != NULL ) {
+               nvram_handle_t *h = nvram_open(file, NVRAM_RO);
+               if( strcmp(file, NVRAM_STAGING) )
+                       free(file);
+               return h;
+       }
 
        return NULL;
 }
@@ -123,8 +127,8 @@ static int do_info(nvram_handle_t *nvram)
        printf("NCDL values:   0x%08X\n\n", hdr->config_ncdl);
 
        printf("%i bytes used / %i bytes available (%.2f%%)\n",
-               hdr->len, NVRAM_SPACE - hdr->len,
-               (100.00 / (double)NVRAM_SPACE) * (double)hdr->len);
+               hdr->len, nvram->length - nvram->offset - hdr->len,
+               (100.00 / (double)(nvram->length - nvram->offset)) * (double)hdr->len);
 
        return 0;
 }