File libcdio-0.94-leak-15.patch of Package libcdio.7811

From be73993d34bce6b12a029788e609b50a1579077a Mon Sep 17 00:00:00 2001
From: "R. Bernstein" <rocky@gnu.org>
Date: Fri, 8 Dec 2017 20:58:23 -0500
Subject: [PATCH 15/20] Reduce memory leaks

---
 lib/driver/FreeBSD/freebsd.c | 50 +++++++++++++++++++++---------------
 lib/driver/image/cdrdao.c    |  3 ++-
 test/testisocd_joliet.c      |  9 ++++++-
 3 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/lib/driver/FreeBSD/freebsd.c b/lib/driver/FreeBSD/freebsd.c
index 6a0d1cda..77ef3fa4 100644
--- a/lib/driver/FreeBSD/freebsd.c
+++ b/lib/driver/FreeBSD/freebsd.c
@@ -962,27 +962,32 @@ get_media_changed_freebsd (const void *p_user_data)
 static const char*
 get_access_mode(const char *psz_source)
 {
-    char *psz_src;
+    char *psz_src = NULL;
     if (!psz_source) {
-	psz_src = cdio_get_default_device_freebsd();
+        psz_src = cdio_get_default_device_freebsd();
     } else {
-	psz_src = strdup(psz_source);
+        psz_src = strdup(psz_source);
     }
 
-    if (psz_src) {
-	if (!(strncmp(psz_src, "/dev/acd", 8)))
-	    return "ioctl";
-	else {
-	    char devname[256];
-	    int  bytes = readlink(psz_src, devname, 255);
-
-	    if (bytes > 0) {
-		devname[bytes]=0;
-		if (!(strncmp(devname, "acd", 3)))
-		    return "ioctl";
-	    }
-	}
+    if (psz_src != NULL) {
+        if (!(strncmp(psz_src, "/dev/acd", 8))) {
+            free(psz_src);
+            return "ioctl";
+	} else {
+            char devname[256];
+            int  bytes = readlink(psz_src, devname, 255);
+
+            if (bytes > 0) {
+                devname[bytes]=0;
+                if (!(strncmp(devname, "acd", 3))) {
+		    free(psz_src);
+                    return "ioctl";
+		}
+            }
+        }
     }
+    if (psz_src != NULL)
+      free(psz_src);
     return "CAM";
 }
 
@@ -1174,7 +1179,10 @@ cdio_open_am_freebsd (const char *psz_orig_source_name,
 
   if (NULL == psz_orig_source_name) {
     psz_source_name=cdio_get_default_device_freebsd();
-    if (NULL == psz_source_name) return NULL;
+    if (NULL == psz_source_name) {
+      cdio_generic_free (_data);
+      return NULL;
+    }
     _data->device  = psz_source_name;
     set_arg_freebsd(_data, "source", psz_source_name);
   } else {
@@ -1186,13 +1194,16 @@ cdio_open_am_freebsd (const char *psz_orig_source_name,
 #if 0
       cdio_info ("source %s is a not a device", psz_orig_source_name);
 #endif
-      free(_data);
+      cdio_generic_free (_data);
       return NULL;
     }
   }
 
   ret = cdio_new ((void *)_data, &_funcs);
-  if (ret == NULL) return NULL;
+  if (ret == NULL) {
+    cdio_generic_free (_data);
+    return NULL;
+  }
 
   open_access_mode = 0;
   if (_AM_MMC_RDWR == _data->access_mode) {
@@ -1245,7 +1256,6 @@ cdio_open_am_freebsd (const char *psz_orig_source_name,
 #else
   return NULL;
 #endif /* HAVE_FREEBSD_CDROM */
-
 }
 
 bool
diff --git a/lib/driver/image/cdrdao.c b/lib/driver/image/cdrdao.c
index 0c27d494..3d961c37 100644
--- a/lib/driver/image/cdrdao.c
+++ b/lib/driver/image/cdrdao.c
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2004-2008, 2011-2012, 2014
+  Copyright (C) 2004-2008, 2011-2012, 2014, 2017
   Rocky Bernstein <rocky@gnu.org>
     toc reading routine adapted from cuetools
   Copyright (C) 2003 Svend Sanjay Sorensen <ssorensen@fastmail.fm>
@@ -715,6 +715,7 @@ parse_tocfile (_img_private_t *cd, const char *psz_cue_name)
 		cdio_log (log_level,
 			  "%s line %d: can't open file `%s' for reading",
 			  psz_cue_name, i_line, psz_field);
+		cdio_stdio_destroy (s);
 		goto err_exit;
 	      }
 	      cdio_stdio_destroy (s);
diff --git a/test/testisocd_joliet.c b/test/testisocd_joliet.c
index 25bb4ed9..8eedc2f2 100644
--- a/test/testisocd_joliet.c
+++ b/test/testisocd_joliet.c
@@ -18,6 +18,7 @@
 
 /* Tests reading ISO 9660 info from an ISO 9660 image.  */
 #include "portable.h"
+#include "cdio_assert.h"
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -110,6 +111,7 @@ main(int argc, const char *argv[])
       iso9660_stat_t *p_statbuf3 =
 	iso9660_ifs_find_lsn_with_path (p_iso, i_lsn, &psz_path);
       int rc=0;
+      const unsigned int statbuf_test_size = 100;
 
       /* Compare the two statbufs. */
       if (p_statbuf->lsn != p_statbuf2->lsn ||
@@ -118,11 +120,16 @@ main(int argc, const char *argv[])
 
 	  fprintf(stderr, "File stat information between fs_stat and "
 		  "iso9660_ifs_find_lsn isn't the same\n");
+	  printf("statbuf  lsn: %d, size: %d, type: %d\n",
+		 p_statbuf->lsn, p_statbuf->size, p_statbuf->type);
+	  printf("statbuf2 lsn: %d, size: %d, type: %d\n",
+		 p_statbuf2->lsn, p_statbuf2->size, p_statbuf2->type);
 	  rc=3;
 	  goto exit;
       }
 
-      if (0 != memcmp(p_statbuf3, p_statbuf2, sizeof(iso9660_stat_t))) {
+      cdio_assert(statbuf_test_size < sizeof(iso9660_stat_t));
+      if (0 != memcmp(p_statbuf3, p_statbuf2, statbuf_test_size)) {
 	  fprintf(stderr, "File stat information between fs_find_lsn and "
 		  "fs_find_lsn_with_path isn't the same\n");
 	  rc=4;
-- 
2.17.0
openSUSE Build Service is sponsored by