File libcdio-0.94-leak-03.patch of Package libcdio.24379

Backport.
From 942dccd64d4281976ed321d32daa26d005be2d6f Mon Sep 17 00:00:00 2001
From: "R. Bernstein" <rocky@gnu.org>
Date: Sun, 19 Nov 2017 23:48:32 -0500
Subject: [PATCH 3/20] Remove more memory leaks; remove more #ifdefs

---
 include/cdio/util.h       | 26 +++++++++++++++----------
 lib/driver/image/bincue.c |  2 +-
 lib/driver/image_common.c | 20 ++++++++++---------
 lib/driver/image_common.h | 41 ++++++++++++++++++---------------------
 lib/iso9660/iso9660_fs.c  | 39 ++++++++++++++-----------------------
 src/util.c                |  2 +-
 test/Makefile.am          | 12 ++++++++++--
 test/driver/Makefile.am   | 10 +++++++++-
 test/testisocd2.c         | 11 +++++++++--
 9 files changed, 91 insertions(+), 72 deletions(-)

Index: libcdio-0.94/include/cdio/util.h
===================================================================
--- libcdio-0.94.orig/include/cdio/util.h
+++ libcdio-0.94/include/cdio/util.h
@@ -21,8 +21,8 @@
 #define CDIO_UTIL_H_
 
 /*!
-   \file util.h 
-   \brief Miscellaneous utility functions. 
+   \file util.h
+   \brief Miscellaneous utility functions.
 
    Warning: this will probably get removed/replaced by using glib.h
 */
@@ -65,8 +65,14 @@ _cdio_len2blocks (uint32_t i_len, uint16
   return i_blocks;
 }
 
+
+/*! free() and NULL out p_obj it is not already null. */
+#define CDIO_FREE_IF_NOT_NULL(p_obj) \
+  if (NULL != p_obj) { free(p_obj); p_obj=NULL; };
+
+
 /* round up to next block boundary */
-static CDIO_INLINE unsigned 
+static CDIO_INLINE unsigned
 _cdio_ceil2block (unsigned offset, uint16_t i_blocksize)
 {
   return _cdio_len2blocks (offset, i_blocksize) * i_blocksize;
@@ -99,11 +105,11 @@ _cdio_memdup (const void *mem, size_t co
 char *
 _cdio_strdup_upper (const char str[]);
 
-/* Duplicate path and make it platform compliant. Typically needed for
-   MinGW/MSYS where a "/c/..." path must be translated to "c:/..." for
-   use with fopen(), etc. Returned string must be freed by the caller
-   using cdio_free(). */
-char * 
+/*! Duplicate path and make it platform compliant. Typically needed for
+    MinGW/MSYS where a "/c/..." path must be translated to "c:/..." for
+    use with fopen(), etc. Returned string must be freed by the caller
+    using cdio_free(). */
+char *
 _cdio_strdup_fixpath (const char path[]);
 
 void
@@ -125,7 +131,7 @@ char *cdio_realpath (const char *psz_src
 #ifdef WANT_FOLLOW_SYMLINK_COMPATIBILITY
 # define cdio_follow_symlink cdio_realpath
 #endif
-  
+
 #ifdef __cplusplus
 }
 #endif
@@ -133,7 +139,7 @@ char *cdio_realpath (const char *psz_src
 #endif /* CDIO_UTIL_H_ */
 
 
-/* 
+/*
  * Local variables:
  *  c-file-style: "gnu"
  *  tab-width: 8
Index: libcdio-0.94/lib/driver/image/bincue.c
===================================================================
--- libcdio-0.94.orig/lib/driver/image/bincue.c
+++ libcdio-0.94/lib/driver/image/bincue.c
@@ -744,7 +744,7 @@ parse_cuefile (_img_private_t *cd, const
               goto err_exit;
             }
             if (cd) {
-#if FIXED_ME
+#ifdef FIXME
               cd->tocent[i].indexes[cd->tocent[i].nindex++] = lba;
 #else
               track_info_t  *this_track=
Index: libcdio-0.94/lib/driver/image_common.c
===================================================================
--- libcdio-0.94.orig/lib/driver/image_common.c
+++ libcdio-0.94/lib/driver/image_common.c
@@ -1,5 +1,6 @@
 /*
-  Copyright (C) 2004-2005, 2008, 2010-2011, 2013 Rocky Bernstein <rocky@gnu.org>
+  Copyright (C) 2004-2005, 2008, 2010-2011, 2013, 2017
+   Rocky Bernstein <rocky@gnu.org>
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -26,6 +27,7 @@
 
 #include "image.h"
 #include "image_common.h"
+#include <cdio/util.h>
 #include "_cdio_stdio.h"
 
 #ifdef HAVE_STDLIB_H
@@ -61,14 +63,14 @@ _free_image (void *p_user_data)
 
   for (i_track=0; i_track < p_env->gen.i_tracks; i_track++) {
     track_info_t *p_tocent = &(p_env->tocent[i_track]);
-    free_if_notnull(p_tocent->filename);
-    free_if_notnull(p_tocent->isrc);
+    CDIO_FREE_IF_NOT_NULL(p_tocent->filename);
+    CDIO_FREE_IF_NOT_NULL(p_tocent->isrc);
     if (p_tocent->data_source) cdio_stdio_destroy(p_tocent->data_source);
   }
 
-  free_if_notnull(p_env->psz_mcn);
-  free_if_notnull(p_env->psz_cue_name);
-  free_if_notnull(p_env->psz_access_mode);
+  CDIO_FREE_IF_NOT_NULL(p_env->psz_mcn);
+  CDIO_FREE_IF_NOT_NULL(p_env->psz_cue_name);
+  CDIO_FREE_IF_NOT_NULL(p_env->psz_access_mode);
   cdtext_destroy(p_env->gen.cdtext);
   cdio_generic_stdio_free(p_env);
   free(p_env);
@@ -365,19 +367,19 @@ _set_arg_image (void *p_user_data, const
 
   if (!strcmp (key, "source"))
     {
-      free_if_notnull (p_env->gen.source_name);
+      CDIO_FREE_IF_NOT_NULL(p_env->gen.source_name);
       if (!value) return DRIVER_OP_ERROR;
       p_env->gen.source_name = strdup (value);
     }
   else if (!strcmp (key, "cue"))
     {
-      free_if_notnull (p_env->psz_cue_name);
+      CDIO_FREE_IF_NOT_NULL(p_env->psz_cue_name);
       if (!value) return DRIVER_OP_ERROR;
       p_env->psz_cue_name = strdup (value);
     }
   else if (!strcmp (key, "access-mode"))
     {
-      free_if_notnull (p_env->psz_access_mode);
+      CDIO_FREE_IF_NOT_NULL(p_env->psz_access_mode);
       if (!value) return DRIVER_OP_ERROR;
       p_env->psz_access_mode = strdup (value);
     }
Index: libcdio-0.94/lib/driver/image_common.h
===================================================================
--- libcdio-0.94.orig/lib/driver/image_common.h
+++ libcdio-0.94/lib/driver/image_common.h
@@ -15,8 +15,8 @@
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-/*! Common image routines. 
-  
+/*! Common image routines.
+
   Because _img_private_t may vary over image formats, the routines are
   included into the image drivers after _img_private_t is defined.  In
   order for the below routines to work, there is a large part of
@@ -28,19 +28,19 @@
 #define CDIO_DRIVER_IMAGE_COMMON_H_
 
 typedef struct {
-  /* Things common to all drivers like this. 
+  /* Things common to all drivers like this.
      This must be first. */
-  generic_img_private_t gen; 
-  internal_position_t pos; 
-  
+  generic_img_private_t gen;
+  internal_position_t pos;
+
   char         *psz_cue_name;
   char         *psz_access_mode; /* Just the name of the driver.
                                     We add this for regularity with other
                                     real CD drivers which has an access mode.
                                   */
-  char         *psz_mcn;        /* Media Catalog Number (5.22.3) 
+  char         *psz_mcn;        /* Media Catalog Number (5.22.3)
                                    exactly 13 bytes */
-  track_info_t  tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track 
+  track_info_t  tocent[CDIO_CD_MAX_TRACKS+1]; /* entry info for each track
                                                  add 1 for leadout. */
   discmode_t    disc_mode;
 
@@ -60,9 +60,6 @@ typedef struct {
 #endif
 } _img_private_t;
 
-#define free_if_notnull(p_obj) \
-  if (NULL != p_obj) { free(p_obj); p_obj=NULL; };
-
 /*!
   We don't need the image any more. Free all memory associated with
   it.
@@ -81,7 +78,7 @@ const char * _get_arg_image (void *user_
  */
 cdtext_t * _get_cdtext_image(void *p_user_data);
 
-/*! 
+/*!
   Get disc type associated with cd_obj.
 */
 discmode_t _get_discmode_image (void *p_user_data);
@@ -96,12 +93,12 @@ void _get_drive_cap_image (const void *u
                            cdio_drive_misc_cap_t  *p_misc_cap);
 
 /*!
-  Return the number of of the first track. 
+  Return the number of of the first track.
   CDIO_INVALID_TRACK is returned on error.
 */
 track_t _get_first_track_num_image(void *p_user_data);
 
-/*! 
+/*!
   Find out if media has changed since the last call.
   @param p_user_data the CD object to be acted upon.
   @return 1 if media has changed since last call, 0 if not. Error
@@ -120,12 +117,12 @@ int get_media_changed_image(const void *
 char * _get_mcn_image(const void *p_user_data);
 
 /*!
-  Return the number of tracks. 
+  Return the number of tracks.
 */
 track_t _get_num_tracks_image(void *p_user_data);
 
 
-/*!  
+/*!
   Return the starting MSF (minutes/secs/frames) for the track number
   track_num in obj.  Tracks numbers start at 1.
   The "leadout" track is specified either by
@@ -147,10 +144,10 @@ track_flag_t get_track_copy_permit_image
 
 /*! Return 1 if track has pre-emphasis, 0 if not, or -1 for error.
   Is this meaningful if not an audio track?
-  
+
   pre-emphasis is a non linear frequency response.
 */
-track_flag_t get_track_preemphasis_image(const void *p_user_data, 
+track_flag_t get_track_preemphasis_image(const void *p_user_data,
                                          track_t i_track);
 
 /*! Return the starting LBA for the pregap for track number i_track.
@@ -170,7 +167,7 @@ char *get_track_isrc_image(const void *p
 
 /*!
   Read a data sector
-  
+
   @param p_cdio object to read from
 
   @param p_buf place to read data into.  The caller should make sure
@@ -188,14 +185,14 @@ char *get_track_isrc_image(const void *p
   @param i_blocks number of blocks to read.
 
   */
-driver_return_code_t 
-read_data_sectors_image ( void *p_user_data, void *p_buf, 
+driver_return_code_t
+read_data_sectors_image ( void *p_user_data, void *p_buf,
                           lsn_t i_lsn,  uint16_t i_blocksize,
                           uint32_t i_blocks );
 
 /*!
   Set the arg "key" with "value" in the source device.
-  Currently "source" to set the source device in I/O operations 
+  Currently "source" to set the source device in I/O operations
   is the only valid key.
 
   0 is returned if no error was found, and nonzero if there as an error.
Index: libcdio-0.94/lib/iso9660/iso9660_fs.c
===================================================================
--- libcdio-0.94.orig/lib/iso9660/iso9660_fs.c
+++ libcdio-0.94/lib/iso9660/iso9660_fs.c
@@ -801,8 +801,7 @@ _iso9660_dir_to_statbuf (iso9660_dir_t *
   iso9660_get_dtime(&(p_iso9660_dir->recording_time), true, &(p_stat->tm));
 
   if (dir_len < sizeof (iso9660_dir_t)) {
-    free(p_stat->rr.psz_symlink);
-    free(p_stat);
+    iso9660_stat_free(p_stat);
     return NULL;
   }
 
@@ -1021,14 +1020,12 @@ _fs_stat_traverse (const CdIo_t *p_cdio,
       if (!cmp) {
 	iso9660_stat_t *ret_stat
 	  = _fs_stat_traverse (p_cdio, p_iso9660_stat, &splitpath[1]);
-	free(p_iso9660_stat->rr.psz_symlink);
-	free(p_iso9660_stat);
+	iso9660_stat_free(p_iso9660_stat);
 	free (_dirbuf);
 	return ret_stat;
       }
 
-      free(p_iso9660_stat->rr.psz_symlink);
-      free(p_iso9660_stat);
+      iso9660_stat_free(p_iso9660_stat);
 
       offset += iso9660_get_dir_len(p_iso9660_dir);
     }
@@ -1126,14 +1123,11 @@ _fs_iso_stat_traverse (iso9660_t *p_iso,
       if (!cmp) {
 	iso9660_stat_t *ret_stat
 	  = _fs_iso_stat_traverse (p_iso, p_stat, &splitpath[1]);
-	free(p_stat->rr.psz_symlink);
-	free(p_stat);
+	iso9660_stat_free(p_stat);
 	free (_dirbuf);
 	return ret_stat;
       }
-
-      free(p_stat->rr.psz_symlink);
-      free(p_stat);
+      iso9660_stat_free(p_stat);
 
       offset += iso9660_get_dir_len(p_iso9660_dir);
     }
@@ -1272,8 +1266,7 @@ iso9660_fs_readdir (CdIo_t *p_cdio, cons
   if (!p_stat) return NULL;
 
   if (p_stat->type != _STAT_DIR) {
-    free(p_stat->rr.psz_symlink);
-    free(p_stat);
+    iso9660_stat_free(p_stat);
     return NULL;
   }
 
@@ -1338,8 +1331,7 @@ iso9660_ifs_readdir (iso9660_t *p_iso, c
   if (!p_stat)   return NULL;
 
   if (p_stat->type != _STAT_DIR) {
-    free(p_stat->rr.psz_symlink);
-    free(p_stat);
+    iso9660_stat_free(p_stat);
     return NULL;
   }
 
@@ -1354,8 +1346,7 @@ iso9660_ifs_readdir (iso9660_t *p_iso, c
     if (!dirbuf_len)
       {
         cdio_warn("Invalid directory buffer sector size %u", p_stat->secsize);
-	free(p_stat->rr.psz_symlink);
-	free(p_stat);
+	iso9660_stat_free(p_stat);
 	_cdio_list_free (retval, true);
         return NULL;
       }
@@ -1364,8 +1355,7 @@ iso9660_ifs_readdir (iso9660_t *p_iso, c
     if (!_dirbuf)
       {
         cdio_warn("Couldn't calloc(1, %lu)", dirbuf_len);
-	free(p_stat->rr.psz_symlink);
-	free(p_stat);
+	iso9660_stat_free(p_stat);
 	_cdio_list_free (retval, true);
         return NULL;
       }
@@ -1373,8 +1363,7 @@ iso9660_ifs_readdir (iso9660_t *p_iso, c
     ret = iso9660_iso_seek_read (p_iso, _dirbuf, p_stat->lsn, p_stat->secsize);
     if (ret != dirbuf_len) 	  {
       _cdio_list_free (retval, true);
-      free(p_stat->rr.psz_symlink);
-      free(p_stat);
+      iso9660_stat_free(p_stat);
       free (_dirbuf);
       return NULL;
     }
@@ -1404,8 +1393,7 @@ iso9660_ifs_readdir (iso9660_t *p_iso, c
       }
 
     free (_dirbuf);
-    free(p_stat->rr.psz_symlink);
-    free (p_stat);
+    iso9660_stat_free(p_stat);
 
     if (offset != dirbuf_len) {
       _cdio_list_free (retval, true);
@@ -1555,8 +1543,11 @@ iso9660_ifs_find_lsn_with_path(iso9660_t
 void
 iso9660_stat_free(iso9660_stat_t *p_stat)
 {
-  if (p_stat != NULL)
+  if (p_stat != NULL) {
+    if (p_stat->rr.psz_symlink)
+      CDIO_FREE_IF_NOT_NULL(p_stat->rr.psz_symlink);
     free(p_stat);
+  }
 }
 
 /*!
Index: libcdio-0.94/src/util.c
===================================================================
--- libcdio-0.94.orig/src/util.c
+++ libcdio-0.94/src/util.c
@@ -389,7 +389,7 @@ print_drive_capabilities(cdio_drive_read
 
     report( stdout, _("Can set drive speed                       : %s\n"),
 	   i_misc_cap & CDIO_DRIVE_CAP_MISC_SELECT_SPEED ? "Yes" : "No" );
-#if FIXED
+#ifdef FIXED
     report( stdout, _("Can detect if CD changed                  : %s\n"),
 	   i_misc_cap & CDIO_DRIVE_CAP_MISC_MEDIA_CHANGED ? "Yes" : "No" );
 #endif
Index: libcdio-0.94/test/Makefile.am
===================================================================
--- libcdio-0.94.orig/test/Makefile.am
+++ libcdio-0.94/test/Makefile.am
@@ -1,4 +1,4 @@
-#   Copyright (C) 2003-2006, 2008-2013 Rocky Bernstein <rocky@gnu.org>
+#   Copyright (C) 2003-2006, 2008-2013, 2017 Rocky Bernstein <rocky@gnu.org>
 #
 #   This program is free software: you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -18,6 +18,9 @@
 ####################################################
 #
 #
+.PHONY: test check-short check-iso_read_large check-iso-read-terse \
+        make-executable clean-local-check check-leaks
+
 SUBDIRS = data driver
 
 hack = check_sizeof testassert testgetdevices testischar \
@@ -132,6 +135,11 @@ make-executable: check_nrg.sh check_cue.
 	fi
 
 clean-local: clean-local-check
-.PHONY: clean-local-check
 clean-local-check:
 	-rm -rf exampleIso*.iso.prep *.log *.trs *.orig *.rej
+
+#: run regression tests. "test" is the same thing as "check"
+check-leaks: $(check_PROGRAMS)
+	for p in $(check_PROGRAMS); do \
+	   valgrind ./$$p; \
+        done
Index: libcdio-0.94/test/driver/Makefile.am
===================================================================
--- libcdio-0.94.orig/test/driver/Makefile.am
+++ libcdio-0.94/test/driver/Makefile.am
@@ -1,4 +1,4 @@
-#   Copyright (C) 2009, 2010, 2012 Rocky Bernstein <rocky@gnu.org>
+#   Copyright (C) 2009, 2010, 2012, 2017 Rocky Bernstein <rocky@gnu.org>
 #
 #   This program is free software: you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -13,6 +13,8 @@
 #   You should have received a copy of the GNU General Public License
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+.PHONY: check-leaks test check-short
+
 DATA_DIR = $(abs_top_srcdir)/test/data
 AM_CPPFLAGS = $(LIBCDIO_CFLAGS) $(LIBISO9660_CFLAGS) -DDATA_DIR=\"$(DATA_DIR)\"
 
@@ -75,6 +77,12 @@ MOSTLYCLEANFILES = \
 #: run regression tests. "test" is the same thing as "check"
 test: check-am
 
+#: run regression tests. "test" is the same thing as "check"
+check-leaks: $(check_PROGRAMS)
+	for p in $(check_PROGRAMS); do \
+	   valgrind ./$$p; \
+        done
+
 #: Run all tests without bloated output
 check-short:
 	$(MAKE) check 2>&1  | ruby @abs_top_srcdir@/make-check-filter.rb
Index: libcdio-0.94/test/testisocd2.c
===================================================================
--- libcdio-0.94.orig/test/testisocd2.c
+++ libcdio-0.94/test/testisocd2.c
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2003-2008, 2012-2013 Rocky Bernstein
+  Copyright (C) 2003-2008, 2012-2013, 2017 Rocky Bernstein
   <rocky@gnu.org>
 
   This program is free software: you can redistribute it and/or modify
@@ -89,12 +89,13 @@ main(int argc, const char *argv[])
        find "." and in that Rock-Ridge information might be found which fills
        in more stat information that iso9660_fs_find_lsn also will find.
        . Ideally iso9660_fs_stat should be fixed. */
-       iso9660_stat_t *p_statbuf = iso9660_ifs_stat (p_iso, "/.");
+    iso9660_stat_t *p_statbuf = iso9660_ifs_stat (p_iso, "/.");
 
     if (NULL == p_statbuf) {
       fprintf(stderr,
 	      "Could not get ISO-9660 file information for file /.\n");
       iso9660_close(p_iso);
+      iso9660_stat_free(p_statbuf);
       exit(2);
     } else {
       /* Now try getting the statbuf another way */
@@ -112,12 +113,14 @@ main(int argc, const char *argv[])
 
 	  fprintf(stderr, "File stat information between fs_stat and "
 		  "iso9660_ifs_find_lsn isn't the same\n");
+	  iso9660_stat_free(p_statbuf);
 	  exit(3);
       }
 
       if (p_statbuf3->lsn != p_statbuf2->lsn ||
 	  p_statbuf3->size != p_statbuf2->size ||
 	  p_statbuf3->type != p_statbuf2->type) {
+          iso9660_stat_free(p_statbuf);
 	  exit(4);
       }
 
@@ -125,11 +128,13 @@ main(int argc, const char *argv[])
 	if (0 != strncmp("/./", psz_path, strlen("/./"))) {
 	  fprintf(stderr, "Path returned for ifs_find_lsn_with_path "
 		  "is not correct should be /./, is %s\n", psz_path);
+          iso9660_stat_free(p_statbuf);
 	  exit(5);
 	}
 	free(psz_path);
       } else {
 	fprintf(stderr, "Path returned for fs_find_lsn_with_path is NULL\n");
+	iso9660_stat_free(p_statbuf);
 	exit(6);
       }
 
@@ -139,8 +144,10 @@ main(int argc, const char *argv[])
 	{
 	  fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n",
 		  (long unsigned int) p_statbuf->lsn);
+	  iso9660_stat_free(p_statbuf);
 	  exit(7);
 	}
+      iso9660_stat_free(p_statbuf);
       exit(0);
     }
   }
openSUSE Build Service is sponsored by