From 665a3d3b3fb91e9d9239320cf6ba3d2e23df469a Mon Sep 17 00:00:00 2001
From: "R. Bernstein" <rocky@gnu.org>
Date: Thu, 30 Nov 2017 20:42:07 -0500
Subject: [PATCH 10/20] More valgrind warnings...

configure.ac, iso9660.c: initialize tm_zone in tm if that exists
example/Makefile.am: add make leak-check target
testisocd{_joliet,}.c: remove memory leaks
---
 configure.ac            |  1 +
 example/Makefile.am     |  5 +++++
 lib/iso9660/iso9660.c   |  5 +++++
 test/Makefile.am        |  2 +-
 test/testisocd.c        | 44 ++++++++++++++++-------------------------
 test/testisocd_joliet.c | 35 +++++++++++++++++++++-----------
 6 files changed, 53 insertions(+), 39 deletions(-)

diff --git a/configure.ac b/configure.ac
index b8ca2d09..832a359c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -197,6 +197,7 @@ AC_HEADER_STDC
 AC_CHECK_HEADERS(alloca.h errno.h fcntl.h glob.h limits.h pwd.h stdbool.h)
 AC_CHECK_HEADERS(stdarg.h stdbool.h stdio.h sys/cdio.h sys/param.h \
 		 sys/time.h sys/timeb.h sys/utsname.h)
+AC_STRUCT_TIMEZONE
 
 ## FreeBSD 4 has getopt in unistd.h. So we include that before
 ## getopt.h
diff --git a/example/Makefile.am b/example/Makefile.am
index f125aa10..cfdf2a84 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -60,6 +60,11 @@ test: check-am
 check-short:
 	$(MAKE) check 2>&1  | ruby @abs_top_srcdir@/make-check-filter.rb
 
+#: run valgrind on C programs
+check-leaks: $(check_PROGRAMS)
+	for p in $(check_PROGRAMS); do \
+	   valgrind ./$$p; \
+        done
 
 AM_CPPFLAGS = $(LIBCDIO_CFLAGS)
 
diff --git a/lib/iso9660/iso9660.c b/lib/iso9660/iso9660.c
index 009a88b1..c8df045d 100644
--- a/lib/iso9660/iso9660.c
+++ b/lib/iso9660/iso9660.c
@@ -204,6 +204,11 @@ iso9660_get_dtime (const iso9660_dtime_t *idr_date, bool b_localtime,
   p_tm->tm_sec    = idr_date->dt_second - idr_date->dt_gmtoff * (15 * 60);
   p_tm->tm_isdst  = -1; /* information not available */
 
+#if HAVE_STRUCT_TM_TM_ZONE == 1
+  /* Initilaize everything */
+  p_tm->tm_zone   = 0;
+#endif
+
   /* Recompute tm_wday and tm_yday via mktime. mktime will also renormalize
      date values to account for the timezone offset. */
   {
diff --git a/test/Makefile.am b/test/Makefile.am
index 75df6188..7c5beeac 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 valgrind on C progrma
+#: run valgrind on C programs
 check-leaks: $(check_PROGRAMS)
 	for p in $(check_PROGRAMS); do \
 	   valgrind ./$$p; \
diff --git a/test/testisocd.c b/test/testisocd.c
index 27ca635b..a8c5b455 100644
--- a/test/testisocd.c
+++ b/test/testisocd.c
@@ -100,18 +100,9 @@ main(int argc, const char *argv[])
       char *psz_path = NULL;
       const lsn_t i_lsn = p_statbuf->lsn;
       iso9660_stat_t *p_statbuf2 = iso9660_fs_find_lsn (p_cdio, i_lsn);
-
-      /*
-	 // FIXME: This is for memory testing. iso966_stat_free leaves
-         // around junk. Some if it is in a faulty cdio_list_free() routine.
-         iso9660_stat_free(p_statbuf);
-         iso9660_stat_free(p_statbuf2);
-         cdio_destroy(p_cdio);
-         return 0;
-      */
-
       iso9660_stat_t *p_statbuf3 =
 	iso9660_fs_find_lsn_with_path (p_cdio, i_lsn, &psz_path);
+      int rc=0;
 
       /* Compare the two statbufs. */
       if (p_statbuf->lsn != p_statbuf2->lsn ||
@@ -119,20 +110,15 @@ main(int argc, const char *argv[])
 	  p_statbuf->type != p_statbuf2->type) {
 	  fprintf(stderr, "File stat information between fs_stat and "
 		  "fs_find_lsn isn't the same\n");
-
-	  iso9660_stat_free(p_statbuf);
-	  iso9660_stat_free(p_statbuf2);
-	  cdio_destroy(p_cdio);
-	  exit(3);
+	  rc=3;
+	  goto exit;
       }
 
       if (0 != memcmp(p_statbuf3, p_statbuf2, sizeof(iso9660_stat_t))) {
 	  fprintf(stderr, "File stat information between fs_find_lsn and "
 		  "fs_find_lsn_with_path isn't the same\n");
-	  iso9660_stat_free(p_statbuf2);
-	  iso9660_stat_free(p_statbuf3);
-	  cdio_destroy(p_cdio);
-	  exit(4);
+	  rc=4;
+	  goto exit;
       }
 
       if (psz_path != NULL) {
@@ -140,13 +126,14 @@ main(int argc, const char *argv[])
 	  fprintf(stderr, "Path returned for fs_find_lsn_with_path "
 		  "is not correct should be /./, is %s\n", psz_path);
 	  free(psz_path);
-	  cdio_destroy(p_cdio);
-	  exit(5);
+	  rc=5;
+	  goto exit;
 	}
       } else {
 	fprintf(stderr, "Path returned for fs_find_lsn_with_path is NULL\n");
-	cdio_destroy(p_cdio);
-	exit(6);
+	free(psz_path);
+	rc=6;
+	goto exit;
       }
 
       /* Try reading from the directory. */
@@ -155,11 +142,14 @@ 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);
-	  cdio_destroy(p_cdio);
-	  exit(7);
+	  rc=7;
 	}
-      exit(0);
+    exit:
+      iso9660_stat_free(p_statbuf);
+      iso9660_stat_free(p_statbuf2);
+      iso9660_stat_free(p_statbuf3);
+      cdio_destroy(p_cdio);
+      exit(rc);
     }
   }
 
diff --git a/test/testisocd_joliet.c b/test/testisocd_joliet.c
index 4d4e28ae..25bb4ed9 100644
--- a/test/testisocd_joliet.c
+++ b/test/testisocd_joliet.c
@@ -77,6 +77,7 @@ main(int argc, const char *argv[])
     }
 
 #ifdef HAVE_JOLIET
+    iso9660_close(p_iso);
     p_iso = iso9660_open_ext(ISO9660_IMAGE, ISO_EXTENSION_ALL);
     joliet_level = iso9660_ifs_get_joliet_level(p_iso);
     if ( joliet_level != 3) {
@@ -105,9 +106,10 @@ main(int argc, const char *argv[])
       char buf[ISO_BLOCKSIZE];
       char *psz_path = NULL;
       const lsn_t i_lsn = p_statbuf->lsn;
-      const iso9660_stat_t *p_statbuf2 = iso9660_ifs_find_lsn (p_iso, i_lsn);
-      const iso9660_stat_t *p_statbuf3 =
+      iso9660_stat_t *p_statbuf2 = iso9660_ifs_find_lsn (p_iso, i_lsn);
+      iso9660_stat_t *p_statbuf3 =
 	iso9660_ifs_find_lsn_with_path (p_iso, i_lsn, &psz_path);
+      int rc=0;
 
       /* Compare the two statbufs. */
       if (p_statbuf->lsn != p_statbuf2->lsn ||
@@ -116,25 +118,31 @@ main(int argc, const char *argv[])
 
 	  fprintf(stderr, "File stat information between fs_stat and "
 		  "iso9660_ifs_find_lsn isn't the same\n");
-	  exit(3);
+	  rc=3;
+	  goto exit;
       }
 
-      if (p_statbuf3->lsn != p_statbuf2->lsn ||
-	  p_statbuf3->size != p_statbuf2->size ||
-	  p_statbuf3->type != p_statbuf2->type) {
-	  exit(4);
+      if (0 != memcmp(p_statbuf3, p_statbuf2, sizeof(iso9660_stat_t))) {
+	  fprintf(stderr, "File stat information between fs_find_lsn and "
+		  "fs_find_lsn_with_path isn't the same\n");
+	  rc=4;
+	  goto exit;
       }
 
       if (psz_path != NULL) {
 	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);
-	  exit(5);
+	  free(psz_path);
+	  rc=5;
+	  goto exit;
 	}
 	free(psz_path);
       } else {
 	fprintf(stderr, "Path returned for fs_find_lsn_with_path is NULL\n");
-	exit(6);
+	free(psz_path);
+	rc=6;
+	goto exit;
       }
 
       /* Try reading from the directory. */
@@ -143,9 +151,14 @@ main(int argc, const char *argv[])
 	{
 	  fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n",
 		  (long unsigned int) p_statbuf->lsn);
-	  exit(7);
+	  rc=7;
 	}
-      exit(0);
+    exit:
+      iso9660_stat_free(p_statbuf);
+      iso9660_stat_free(p_statbuf2);
+      iso9660_stat_free(p_statbuf3);
+      iso9660_close(p_iso);
+      exit(rc);
     }
   }
 
-- 
2.17.0

