changeset 359:e2973c342e59 src

prevent 2 potential memory leaks in SetUDFCache()
author nicodvb
date Sat, 10 May 2008 20:46:42 +0000
parents e998b2df2200
children b6fa98f690ee
files dvdread/dvd_udf.c
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/dvdread/dvd_udf.c	Sat May 10 20:37:13 2008 +0000
+++ b/dvdread/dvd_udf.c	Sat May 10 20:46:42 2008 +0000
@@ -226,6 +226,7 @@
 {
   int n;
   struct udf_cache *c;
+  void *tmp;
 
   if(DVDUDFCacheLevel(device, -1) <= 0)
     return 0;
@@ -270,16 +271,18 @@
       }
     }
     c->lb_num++;
-    c->lbs = realloc(c->lbs, c->lb_num * sizeof(struct lbudf));
+    tmp = realloc(c->lbs, c->lb_num * sizeof(struct lbudf));
     /*
     fprintf(stderr, "realloc lb: %d * %d = %d\n",
       c->lb_num, sizeof(struct lbudf),
       c->lb_num * sizeof(struct lbudf));
     */
-    if(c->lbs == NULL) {
+    if(tmp == NULL) {
+      if(c->lbs) free(c->lbs);
       c->lb_num = 0;
       return 0;
     }
+    c->lbs = tmp;
     c->lbs[n].data_base = ((uint8_t **)data)[0];
     c->lbs[n].data = ((uint8_t **)data)[1];
     c->lbs[n].lb = nr;
@@ -294,16 +297,18 @@
       }
     }
     c->map_num++;
-    c->maps = realloc(c->maps, c->map_num * sizeof(struct icbmap));
+    tmp = realloc(c->maps, c->map_num * sizeof(struct icbmap));
     /*
     fprintf(stderr, "realloc maps: %d * %d = %d\n",
       c->map_num, sizeof(struct icbmap),
       c->map_num * sizeof(struct icbmap));
     */
-    if(c->maps == NULL) {
+    if(tmp == NULL) {
+      if(c->maps) free(c->maps);
       c->map_num = 0;
       return 0;
     }
+    c->maps = tmp;
     c->maps[n] = *(struct icbmap *)data;
     c->maps[n].lbn = nr;
     break;