From e48df05eeeb85838526a03a3371964f5f6b14321 Mon Sep 17 00:00:00 2001
From: John Darrington <john@darrington.wattle.id.au>
Date: Fri, 25 Feb 2005 13:14:29 +0000
Subject: [PATCH] Fixed a bug which caused a crash when reading non-existent
 files.

---
 src/ChangeLog             |  5 +++
 src/sfm-read.c            | 10 ++++--
 tests/Makefile.am         |  1 +
 tests/bugs/get-no-file.sh | 64 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100755 tests/bugs/get-no-file.sh

diff --git a/src/ChangeLog b/src/ChangeLog
index c5fd1977..f00274b7 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+Fri Feb 25 21:11:35 WST 2005 John Darrington <john@darrington.wattle.id.au>
+
+	* sfm-read.c: Fixed a buglet which caused a crash when trying
+	to read a non-existent file.
+
 Sun Feb 13 16:11:13 2005  Ben Pfaff  <blp@gnu.org>
 
 	Fix Bug #11955.
diff --git a/src/sfm-read.c b/src/sfm-read.c
index 9c0cc6d3..40a366cf 100644
--- a/src/sfm-read.c
+++ b/src/sfm-read.c
@@ -153,9 +153,13 @@ sfm_close_reader (struct sfm_reader *r)
 
   if (r->fh != NULL)
     fh_close (r->fh, "system file", "rs");
-  if (fn_close (handle_get_filename (r->fh), r->file) == EOF)
-    msg (ME, _("%s: Closing system file: %s."),
-         handle_get_filename (r->fh), strerror (errno));
+  
+  if ( r->file ) {
+    if (fn_close (handle_get_filename (r->fh), r->file) == EOF)
+      msg (ME, _("%s: Closing system file: %s."),
+	   handle_get_filename (r->fh), strerror (errno));
+    r->file = NULL;
+  }
   free (r->vars);
   free (r->buf);
   free (r);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f94d9b4d..41769383 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -55,6 +55,7 @@ TESTS = \
 	bugs/double-frequency.sh \
 	bugs/get.sh \
 	bugs/examine-1sample.sh \
+	bugs/get-no-file.sh \
 	bugs/html-frequency.sh \
 	bugs/if_crash.sh \
 	bugs/multipass.sh \
diff --git a/tests/bugs/get-no-file.sh b/tests/bugs/get-no-file.sh
new file mode 100755
index 00000000..7df000f9
--- /dev/null
+++ b/tests/bugs/get-no-file.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+# This program tests for a bug which caused a crash when 
+# GET specified a non-existent file
+
+TEMPDIR=/tmp/pspp-tst-$$
+TESTFILE=$TEMPDIR/`basename $0`.sps
+
+here=`pwd`;
+
+# ensure that top_srcdir is absolute
+cd $top_srcdir; top_srcdir=`pwd`
+
+export STAT_CONFIG_PATH=$top_srcdir/config
+
+
+cleanup()
+{
+     rm -rf $TEMPDIR
+}
+
+
+fail()
+{
+    echo $activity
+    echo FAILED
+    cleanup;
+    exit 1;
+}
+
+
+no_result()
+{
+    echo $activity
+    echo NO RESULT;
+    cleanup;
+    exit 2;
+}
+
+pass()
+{
+    cleanup;
+    exit 0;
+}
+
+mkdir -p $TEMPDIR
+
+cd $TEMPDIR
+
+activity="create program"
+cat > $TESTFILE <<EOF
+GET /FILE='$TEMPDIR/no-file.xx'.
+EOF
+if [ $? -ne 0 ] ; then no_result ; fi
+
+
+# The command should produce a warning.  Not an error.
+# We use the stdinput here, because the bug seems to manifest itself only in 
+# interactive mode.
+activity="run program"
+cat $TESTFILE | $SUPERVISOR $here/../src/pspp -o raw-ascii  > /dev/null
+if [ $? -ne 0 ] ; then fail ; fi
+
+pass
-- 
2.30.2