Partial patch.
From bf7ee89d6eb22887f356d90d03c03e4255dd236f Mon Sep 17 00:00:00 2001
From: "R. Bernstein" <rocky@gnu.org>
Date: Thu, 30 Nov 2017 06:38:17 -0500
Subject: [PATCH 8/20] Lots of cleanups...

isio9660.h, iso9660.c, iso9660.hpp
  remove mode2 from is9660_fs_stat_translate() and in C++ version
  go over doxygen comments. (More is needed)

iso-info.c, cd-info.c: use alloca() instead of malloc() where possible

test/Mamefile.am: correct remake comments for check-leaks

testisocd2.c: make leak free
iso9660_fs.c: fix memory leak in iso9660_ifs_find_lsn
---
 configure.ac               |   2 +-
 include/cdio++/iso9660.hpp |  14 ++--
 include/cdio/iso9660.h     | 112 +++++++++++++++++++++++------
 lib/cdio++/iso9660.cpp     |   8 +--
 lib/iso9660/iso9660_fs.c   | 144 ++++++++++++++++++++++++++++++-------
 src/cd-info.c              |  12 ++--
 test/Makefile.am           |   2 +-
 test/driver/Makefile.am    |   2 +-
 test/testisocd2.c          |  13 ++--
 9 files changed, 231 insertions(+), 78 deletions(-)

diff --git a/include/cdio++/iso9660.hpp b/include/cdio++/iso9660.hpp
index 08a9faec..a5a6e8e4 100644
--- a/include/cdio++/iso9660.hpp
+++ b/include/cdio++/iso9660.hpp
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2006, 2008, 2011, 2012 Rocky Bernstein <rocky@gnu.org>
+    Copyright (C) 2006, 2008, 2011-2012, 2016 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
@@ -180,8 +180,7 @@ public:
       pointers for the files inside that directory. The caller must free the
       returned result.
     */
-    bool readdir (const char psz_path[], stat_vector_t& stat_vector,
-                  bool b_mode2=false);
+    bool readdir (const char psz_path[], stat_vector_t& stat_vector);
 
     /*!
       Return file status for path name psz_path. NULL is returned on
@@ -191,19 +190,14 @@ public:
       dropped, i.e. ;1 is removed and if level 1 ISO-9660 names are
       lowercased.
 
-      Mode2 is used only if translate is true and is a hack that
-      really should go away in libcdio sometime. If set use mode 2
-      reading, otherwise use mode 1 reading.
-
       @return file status object for psz_path. NULL is returned on
       error. Caller must release returned object using delete when done.
     */
     Stat *
-    stat (const char psz_path[], bool b_translate=false, bool b_mode2=false)
+    stat (const char psz_path[], bool b_translate=false)
     {
       if (b_translate)
-        return new Stat(iso9660_fs_stat_translate (p_cdio, psz_path,
-                                                            b_mode2));
+        return new Stat(iso9660_fs_stat_translate (p_cdio, psz_path));
       else
         return new Stat(iso9660_fs_stat (p_cdio, psz_path));
     }
diff --git a/include/cdio/iso9660.h b/include/cdio/iso9660.h
index 829ab1a6..828ac79f 100644
--- a/include/cdio/iso9660.h
+++ b/include/cdio/iso9660.h
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2003-2008, 2012-2013
+    Copyright (C) 2003-2008, 2012-2013, 2017
                   Rocky Bernstein <rocky@gnu.org>
     Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
 
@@ -563,8 +563,11 @@ typedef struct _iso9660_s iso9660_t;
     associated with the image. Call this when done using using an ISO
     9660 image.
 
+    @param p_iso the ISO-9660 file image to get data from
+
     @return true is unconditionally returned. If there was an error
-    false would be returned.
+    false would be returned. Resources associated with p_iso are
+    freed.
   */
   bool iso9660_close (iso9660_t * p_iso);
 
@@ -572,6 +575,12 @@ typedef struct _iso9660_s iso9660_t;
   /*!
     Open an ISO 9660 image for reading. Maybe in the future we will have
     a mode. NULL is returned on error.
+
+    @param psz_path full path of ISO9660 file.
+
+
+    @return a IS9660 structure  is unconditionally returned. The caller
+    should call iso9660_close() when done.
   */
   iso9660_t *iso9660_open (const char *psz_path /*flags, mode */);
 
@@ -867,18 +876,31 @@ iso9660_stat_t *iso9660_fs_find_lsn(CdIo_t *p_cdio, lsn_t i_lsn);
 
 /*!
    Given a directory pointer, find the filesystem entry that contains
-   lsn and return information about it.
+   LSN and return information about it.
+
+   @param p_iso the ISO-9660 file image to get data from.
+
+   @param i_lsn the LSN to find
+
+   @param ppsz_full_filename the place to store the name of the path that has LSN.
+   On entry this should point to NULL. If not, the value will be freed.
+   On exit a value is malloc'd and the caller is responsible for
+   freeing the result.
 
    @return stat_t of entry if we found lsn, or NULL otherwise.
    Caller must free return value using iso9660_stat_free().
  */
 iso9660_stat_t *iso9660_fs_find_lsn_with_path(CdIo_t *p_cdio, lsn_t i_lsn,
-                                              /*out*/ char **ppsz_path);
+                                              /*out*/ char **ppsz_full_filename);
 
 /*!
    Given a directory pointer, find the filesystem entry that contains
    lsn and return information about it.
 
+   @param p_iso the ISO-9660 file image to get data from.
+
+   @param i_lsn the LSN to find
+
    @return stat_t of entry if we found lsn, or NULL otherwise.
    Caller must free return value using iso9660_stat_free().
  */
@@ -890,7 +912,9 @@ iso9660_stat_t *iso9660_ifs_find_lsn(iso9660_t *p_iso, lsn_t i_lsn);
    lsn and return information about it.
 
    @param p_iso pointer to iso_t
+
    @param i_lsn LSN to find
+
    @param ppsz_path  full path of lsn filename. On entry *ppsz_path should be
    NULL. On return it will be allocated an point to the full path of the
    file at lsn or NULL if the lsn is not found. You should deallocate
@@ -905,6 +929,9 @@ iso9660_stat_t *iso9660_ifs_find_lsn_with_path(iso9660_t *p_iso,
 
 /*!
   Free the passed iso9660_stat_t structure.
+
+  @param p_stat iso9660 stat buffer to free.
+
  */
 void iso9660_stat_free(iso9660_stat_t *p_stat);
 
@@ -915,7 +942,9 @@ void iso9660_stat_free(iso9660_stat_t *p_stat);
 
   @param psz_path filename path to look up and get information about
 
-  @return ISO 9660 file information
+  @return ISO 9660 file information. The caller must free the returned
+  result using iso9660_stat_free().
+
 
   Important note:
 
@@ -934,49 +963,88 @@ iso9660_stat_t *iso9660_fs_stat (CdIo_t *p_cdio, const char psz_path[]);
   pathname version numbers in the ISO 9660 name are dropped, i.e. ;1
   is removed and if level 1 ISO-9660 names are lowercased.
 
-  b_mode2 is historical. It is not used.
+  @param p_cdio the CD object to read from
+
+  @param psz_path filename path to look up and get information about
+
+  @return ISO 9660 file information.  The caller must free the
+  returned result using iso9660_stat_free().
+
  */
 iso9660_stat_t *iso9660_fs_stat_translate (CdIo_t *p_cdio,
-                                           const char psz_path[],
-                                           bool b_mode2);
-
+                                           const char psz_path[]);
 /*!
-  Return file status for pathname. NULL is returned on error.
+
+  @param p_cdio the CD object to read from
+
+  @param pzs_path path the look up
+
+  @return file status for pathname. NULL is returned on error.
+  The caller must free the returned result using iso9660_stat_free().
  */
 iso9660_stat_t *iso9660_ifs_stat (iso9660_t *p_iso, const char psz_path[]);
 
 
-/*!  Return file status for path name psz_path. NULL is returned on
+/*!
+  @param p_iso the ISO-9660 file image to get data from
+
+  @param psz_path filename path translate
+
+  @return file status for path name psz_path. NULL is returned on
   error.  pathname version numbers in the ISO 9660 name are dropped,
   i.e. ;1 is removed and if level 1 ISO-9660 names are lowercased.
+  The caller must free the returned result using iso9660_stat_free().
  */
 iso9660_stat_t *iso9660_ifs_stat_translate (iso9660_t *p_iso,
                                             const char psz_path[]);
 
-/*!  Read psz_path (a directory) and return a list of iso9660_stat_t
-  pointers for the files inside that directory. The caller must free the
-  returned result using _cdio_list_free().
+/*!
+  Read psz_path (a directory) and return a list of iso9660_stat_t
+  pointers for the files inside that directory.
+
+  @param p_cdio the CD object to read from
+
+  @param pzs_path path the read the directory from.
 
-  b_mode2 is historical. It is not used.
+  @return file status for psz_path. The caller must free the
+  The caller must free the returned result using iso9660_stat_free().
 */
-CdioList_t * iso9660_fs_readdir (CdIo_t *p_cdio, const char psz_path[],
-                                 bool b_mode2);
+CdioList_t * iso9660_fs_readdir (CdIo_t *p_cdio, const char psz_path[]);
 
-/*!  Read psz_path (a directory) and return a list of iso9660_stat_t
-  pointers for the files inside that directory. The caller must free
-  the returned result using _cdio_list_free().
+/*!
+  Read psz_path (a directory) and return a list of iso9660_stat_t
+  pointers for the files inside that directory.
+
+  @param p_iso the ISO-9660 file image to get data from
+
+  @param pzs_path path the read the directory from.
+
+  @return file status for psz_path. The caller must free the
+  The caller must free the returned result using iso9660_stat_free().
 */
 CdioList_t * iso9660_ifs_readdir (iso9660_t *p_iso, const char psz_path[]);
 
 /*!
   Return the PVD's application ID.
+
+  @param p_pvd the PVD to get data from
+
+  @return  the application id.
   NULL is returned if there is some problem in getting this.
+  The caller must free the resturned result using free() if
+  not null.
 */
 char * iso9660_get_application_id(iso9660_pvd_t *p_pvd);
 
 /*!
-  Get the application ID.  psz_app_id is set to NULL if there
-  is some problem in getting this and false is returned.
+  Return the PVD's application ID.
+
+  @param p_iso the ISO-9660 file image to get data from
+
+  @return  the application id.
+  NULL is returned if there is some problem in getting this.
+  The caller must free the resturned result using free() if
+  not null.
 */
 bool iso9660_ifs_get_application_id(iso9660_t *p_iso,
                                     /*out*/ cdio_utf8_t **p_psz_app_id);
diff --git a/lib/cdio++/iso9660.cpp b/lib/cdio++/iso9660.cpp
index d6ab2cb2..be606b0e 100644
--- a/lib/cdio++/iso9660.cpp
+++ b/lib/cdio++/iso9660.cpp
@@ -1,5 +1,5 @@
 /* -*- C++ -*-
-  Copyright (C) 2006, 2008, 2011 Rocky Bernstein <rocky@gnu.org>
+  Copyright (C) 2006, 2008, 2011, 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
@@ -70,11 +70,9 @@ ISO9660::FS::read_superblock (iso_extension_mask_t iso_extension_mask)
   returned result.
 */
 bool
-ISO9660::FS::readdir (const char psz_path[], stat_vector_t& stat_vector,
-		      bool b_mode2)
+ISO9660::FS::readdir (const char psz_path[], stat_vector_t& stat_vector)
 {
-  CdioList_t * p_stat_list = iso9660_fs_readdir (p_cdio, psz_path,
-						 b_mode2);
+  CdioList_t * p_stat_list = iso9660_fs_readdir (p_cdio, psz_path);
   if (p_stat_list) {
     CdioListNode_t *p_entnode;
     _CDIO_LIST_FOREACH (p_entnode, p_stat_list) {
diff --git a/lib/iso9660/iso9660_fs.c b/lib/iso9660/iso9660_fs.c
index 3fbf7e8a..07d6c238 100644
--- a/lib/iso9660/iso9660_fs.c
+++ b/lib/iso9660/iso9660_fs.c
@@ -206,6 +206,11 @@ iso9660_open_ext_private (const char *psz_path,
 /*!
   Open an ISO 9660 image for reading. Maybe in the future we will have
   a mode. NULL is returned on error.
+
+  @param psz_path full path of ISO9660 file.
+
+  @return a IS9660 structure  is unconditionally returned. The caller
+  should call iso9660_close() when done.
 */
 iso9660_t *
 iso9660_open (const char *psz_path /*, mode*/)
@@ -214,8 +219,11 @@ iso9660_open (const char *psz_path /*, mode*/)
 }
 
 /*!
-  Open an ISO 9660 image for reading. Maybe in the future we will have
-  a mode. NULL is returned on error.
+  Open an ISO 9660 image for reading allowing various ISO 9660
+  extensions.  Maybe in the future we will have a mode. NULL is
+  returned on error.
+
+  @see iso9660_open_fuzzy
 */
 iso9660_t *
 iso9660_open_ext (const char *psz_path,
@@ -225,9 +233,19 @@ iso9660_open_ext (const char *psz_path,
 }
 
 
-/*!
-  Open an ISO 9660 image for reading. Maybe in the future we will have
-  a mode. NULL is returned on error.
+/*! Open an ISO 9660 image for "fuzzy" reading. This means that we
+  will try to guess various internal offset based on internal
+  checks. This may be useful when trying to read an ISO 9660 image
+  contained in a file format that libiso9660 doesn't know natively
+  (or knows imperfectly.)
+
+  Some tolerence allowed for positioning the ISO 9660 image. We scan
+  for STANDARD_ID and use that to set the eventual offset to adjust
+  by (as long as that is <= i_fuzz).
+
+  Maybe in the future we will have a mode. NULL is returned on error.
+
+  @see iso9660_open, @see iso9660_fuzzy_ext
 */
 iso9660_t *
 iso9660_open_fuzzy (const char *psz_path, uint16_t i_fuzz /*, mode*/)
@@ -236,8 +254,13 @@ iso9660_open_fuzzy (const char *psz_path, uint16_t i_fuzz /*, mode*/)
 }
 
 /*!
-  Open an ISO 9660 image for reading. Maybe in the future we will have
-  a mode. NULL is returned on error.
+  Open an ISO 9660 image for reading with some tolerence for positioning
+  of the ISO9660 image. We scan for ISO_STANDARD_ID and use that to set
+  the eventual offset to adjust by (as long as that is <= i_fuzz).
+
+  Maybe in the future we will have a mode. NULL is returned on error.
+
+  @see iso9660_open_ext @see iso9660_open_fuzzy
 */
 iso9660_t *
 iso9660_open_fuzzy_ext (const char *psz_path,
@@ -248,10 +271,12 @@ iso9660_open_fuzzy_ext (const char *psz_path,
 				  true);
 }
 
-/*!
-  Close previously opened ISO 9660 image.
-  True is unconditionally returned. If there was an error false would
-  be returned.
+/*! Close previously opened ISO 9660 image and free resources
+    associated with the image. Call this when done using using an ISO
+    9660 image.
+
+    @return true is unconditionally returned. If there was an error
+    false would be returned.
 */
 bool
 iso9660_close (iso9660_t *p_iso)
@@ -1178,7 +1203,22 @@ _fs_iso_stat_traverse (iso9660_t *p_iso, const iso9660_stat_t *_root,
 }
 
 /*!
-  Get file status for psz_path into stat. NULL is returned on error.
+  Return file status for psz_path. NULL is returned on error.
+
+  @param p_cdio the CD object to read from
+
+  @param psz_path filename path to look up and get information about
+
+  @return ISO 9660 file information
+
+  Important note:
+
+  You make get different results looking up "/" versus "/." and the
+  latter may give more complete information. "/" will take information
+  from the PVD only, whereas "/." will force a directory read of "/" and
+  find "." and in that Rock-Ridge information might be found which fills
+  in more stat information. Ideally iso9660_fs_stat should be fixed.
+  Patches anyone?
  */
 iso9660_stat_t *
 iso9660_fs_stat (CdIo_t *p_cdio, const char psz_path[])
@@ -1235,9 +1275,21 @@ fs_stat_translate (void *p_image, stat_root_t stat_root,
   return p_stat;
 }
 
+/*!
+  Return file status for path name psz_path. NULL is returned on error.
+  pathname version numbers in the ISO 9660 name are dropped, i.e. ;1
+  is removed and if level 1 ISO-9660 names are lowercased.
+
+  @param p_cdio the CD object to read from
+
+  @param psz_path filename path to look up and get information about
+
+  @return ISO 9660 file information.  The caller must free the
+  returned result using iso9660_stat_free().
+
+ */
 iso9660_stat_t *
-iso9660_fs_stat_translate (CdIo_t *p_cdio, const char psz_path[],
-			   bool b_mode2)
+iso9660_fs_stat_translate (CdIo_t *p_cdio, const char psz_path[])
 {
   return fs_stat_translate(p_cdio, (stat_root_t *) _fs_stat_root,
 			   (stat_traverse_t *) _fs_stat_traverse,
@@ -1245,10 +1297,14 @@ iso9660_fs_stat_translate (CdIo_t *p_cdio, const char psz_path[],
 }
 
 /*!
-  Get file status for psz_path into stat. NULL is returned on error.
-  pathname version numbers in the ISO 9660
-  name are dropped, i.e. ;1 is removed and if level 1 ISO-9660 names
-  are lowercased.
+  @param p_iso the ISO-9660 file image to get data from
+
+  @param psz_path filename path translate
+
+  @return file status for path name psz_path. NULL is returned on
+  error.  pathname version numbers in the ISO 9660 name are dropped,
+  i.e. ;1 is removed and if level 1 ISO-9660 names are lowercased.
+  The caller must free the returned result using iso9660_stat_free().
  */
 iso9660_stat_t *
 iso9660_ifs_stat_translate (iso9660_t *p_iso, const char psz_path[])
@@ -1260,7 +1316,13 @@ iso9660_ifs_stat_translate (iso9660_t *p_iso, const char psz_path[])
 
 
 /*!
-  Get file status for psz_path into stat. NULL is returned on error.
+
+  @param p_cdio the CD object to read from
+
+  @param pzs_path path the look up
+
+  @return file status for pathname. NULL is returned on error.
+  The caller must free the returned result using iso9660_stat_free().
  */
 iso9660_stat_t *
 iso9660_ifs_stat (iso9660_t *p_iso, const char psz_path[])
@@ -1290,7 +1352,7 @@ iso9660_ifs_stat (iso9660_t *p_iso, const char psz_path[])
   b_mode2 is historical. It is not used.
 */
 CdioList_t *
-iso9660_fs_readdir (CdIo_t *p_cdio, const char psz_path[], bool b_mode2)
+iso9660_fs_readdir (CdIo_t *p_cdio, const char psz_path[])
 {
   generic_img_private_t *p_env;
   iso9660_stat_t *p_stat;
@@ -1530,9 +1592,17 @@ iso9660_fs_find_lsn(CdIo_t *p_cdio, lsn_t i_lsn)
 
 /*!
    Given a directory pointer, find the filesystem entry that contains
-   lsn and return information about it.
+   LSN and return information about it.
 
-   Returns stat_t of entry if we found lsn, or NULL otherwise.
+   @param p_iso the ISO-9660 file image to get data from.
+   @param i_lsn the LSN to find
+   @param ppsz_full_filename the place to store the name of the path that has LSN.
+   On entry this should point to NULL. If not, the value will be freed.
+   On exit a value is malloc'd and the caller is responsible for
+   freeing the result.
+
+   @return stat_t of entry if we found lsn, or NULL otherwise.
+   Caller must free return value using iso9660_stat_free().
  */
 iso9660_stat_t *
 iso9660_fs_find_lsn_with_path(CdIo_t *p_cdio, lsn_t i_lsn,
@@ -1546,21 +1616,40 @@ iso9660_fs_find_lsn_with_path(CdIo_t *p_cdio, lsn_t i_lsn,
    Given a directory pointer, find the filesystem entry that contains
    lsn and return information about it.
 
-   Returns stat_t of entry if we found lsn, or NULL otherwise.
+   @param p_iso the ISO-9660 file image to get data from.
+
+   @param i_lsn the LSN to find
+
+   @return stat_t of entry if we found lsn, or NULL otherwise.
+   Caller must free return value using iso9660_stat_free().
  */
 iso9660_stat_t *
 iso9660_ifs_find_lsn(iso9660_t *p_iso, lsn_t i_lsn)
 {
   char *psz_full_filename = NULL;
-  return find_lsn_recurse (p_iso, (iso9660_readdir_t *) iso9660_ifs_readdir,
-			   "/", i_lsn, &psz_full_filename);
+  iso9660_stat_t *ret  =
+    find_lsn_recurse (p_iso, (iso9660_readdir_t *) iso9660_ifs_readdir,
+		      "/", i_lsn, &psz_full_filename);
+  if (psz_full_filename != NULL)
+    free(psz_full_filename);
+  return ret;
 }
 
 /*!
    Given a directory pointer, find the filesystem entry that contains
    lsn and return information about it.
 
-   Returns stat_t of entry if we found lsn, or NULL otherwise.
+   @param p_iso pointer to iso_t
+
+   @param i_lsn LSN to find
+
+   @param ppsz_path  full path of lsn filename. On entry *ppsz_path should be
+   NULL. On return it will be allocated an point to the full path of the
+   file at lsn or NULL if the lsn is not found. You should deallocate
+   *ppsz_path when you are done using it.
+
+   @return stat_t of entry if we found lsn, or NULL otherwise.
+   Caller must free return value using iso9660_stat_free().
  */
 iso9660_stat_t *
 iso9660_ifs_find_lsn_with_path(iso9660_t *p_iso, lsn_t i_lsn,
@@ -1572,6 +1661,9 @@ iso9660_ifs_find_lsn_with_path(iso9660_t *p_iso, lsn_t i_lsn,
 
 /*!
   Free the passed iso9660_stat_t structure.
+
+  @param p_stat iso9660 stat buffer to free.
+
  */
 void
 iso9660_stat_free(iso9660_stat_t *p_stat)
diff --git a/src/cd-info.c b/src/cd-info.c
index cfec0076..7d93fb28 100644
--- a/src/cd-info.c
+++ b/src/cd-info.c
@@ -68,6 +68,10 @@
 #include <errno.h>
 #endif
 
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
 #define STRONG "__________________________________\n"
 #define NORMAL ""
 
@@ -544,20 +548,19 @@ print_iso9660_recurse (CdIo_t *p_cdio, const char pathname[],
   CdioList_t *p_dirlist =  _cdio_list_new ();
   CdioListNode_t *entnode;
   uint8_t i_joliet_level;
-  char *translated_name = (char *) malloc(4096);
+  char *translated_name = (char *) alloca(4096);
   size_t translated_name_size = 4096;
 
   i_joliet_level = (opts.no_joliet)
     ? 0
     : cdio_get_joliet_level(p_cdio);
 
-  p_entlist = iso9660_fs_readdir (p_cdio, pathname, false);
+  p_entlist = iso9660_fs_readdir (p_cdio, pathname);
 
   printf ("%s:\n", pathname);
 
   if (NULL == p_entlist) {
     report( stderr, "Error getting above directory information\n" );
-    free(translated_name);
     free(p_dirlist);
     return;
   }
@@ -571,8 +574,6 @@ print_iso9660_recurse (CdIo_t *p_cdio, const char pathname[],
       char _fullname[4096] = { 0, };
        if (strlen(psz_iso_name) >= translated_name_size) {
          translated_name_size = strlen(psz_iso_name)+1;
-         free(translated_name);
-         translated_name = (char *) malloc(translated_name_size);
          if (!translated_name) {
            report( stderr, "Error allocating memory\n" );
 	   _cdio_list_free (p_dirlist, true, free);
@@ -604,7 +605,6 @@ print_iso9660_recurse (CdIo_t *p_cdio, const char pathname[],
         p_statbuf->rr.i_symlink = 0;
       }
     }
-    free (translated_name);
 
   _cdio_list_free (p_entlist, true, (CdioDataFree_t) iso9660_stat_free);
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 8e9e0985..75df6188 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -138,7 +138,7 @@ clean-local: 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"
+#: run valgrind on C progrma
 check-leaks: $(check_PROGRAMS)
 	for p in $(check_PROGRAMS); do \
 	   valgrind ./$$p; \
diff --git a/test/driver/Makefile.am b/test/driver/Makefile.am
index 4587fb8b..14a646ad 100644
--- a/test/driver/Makefile.am
+++ b/test/driver/Makefile.am
@@ -77,7 +77,7 @@ MOSTLYCLEANFILES = \
 #: run regression tests. "test" is the same thing as "check"
 test: check-am
 
-#: run regression tests. "test" is the same thing as "check"
+#: run valgrind on C progrma
 check-leaks: $(check_PROGRAMS)
 	for p in $(check_PROGRAMS); do \
 	   valgrind ./$$p; \
diff --git a/test/testisocd2.c b/test/testisocd2.c
index f101c16a..a1187764 100644
--- a/test/testisocd2.c
+++ b/test/testisocd2.c
@@ -135,7 +135,6 @@ main(int argc, const char *argv[])
 	  rc = 5;
 	  goto exit;
 	}
-	free(psz_path);
       } else {
 	fprintf(stderr, "Path returned for fs_find_lsn_with_path is NULL\n");
 	rc = 6;
@@ -153,11 +152,13 @@ main(int argc, const char *argv[])
 	  goto exit;
 	}
   exit:
-    iso9660_stat_free(p_statbuf2);
-    iso9660_stat_free(p_statbuf3);
-    iso9660_stat_free(p_statbuf);
-    iso9660_close(p_iso);
-    exit(rc);
+      if (psz_path != NULL)
+      	free(psz_path);
+      iso9660_stat_free(p_statbuf2);
+      iso9660_stat_free(p_statbuf3);
+      iso9660_stat_free(p_statbuf);
+      iso9660_close(p_iso);
+      exit(rc);
     }
   }
 
-- 
2.17.0

