[m-rev.] for review: fix bug #161

Julien Fischer jfischer at opturion.com
Mon May 20 13:42:47 AEST 2013


For review by anyone:

Branches: master, 13.05

The original test case for this bug is quite difficult to integrate into
the test suite as it effectively closes the standard output stream.  I
will commit a test case for this separately as the one I have seems to
be triggering other problems on Linux.

-----------------------

Fix bug #161.

In C grades, closing a file stream multiple times can lead to a segmentation
fault on some systems (e.g. Linux), whereas on others (e.g. Mac OS X) an
io.error exception is thrown.  (The difference is due to differences in the
semantics of fclose.)  Try to avoid the former case by checking that the
stream handle is non-NULL before we attempt to close it.  (mercury_close
zeros
out the relevant bits of the MercuryFile structure when we close a stream
so,
the stream handle is set to NULL the first time we close the stream.)

library/io.m:
  As above.

Julien.

diff --git a/library/io.m b/library/io.m
index 8a462f4..ef77bed 100644
--- a/library/io.m
+++ b/library/io.m
@@ -7342,6 +7342,16 @@ static const MercuryFile MR_closed_stream = {
  void
  mercury_close(MercuryFilePtr mf)
  {
+    /*
+    ** On some systems attempting to close a file stream that has been
+    ** previously closed will lead to a segmentation fault.  We check
+    ** that we have not previously closed the file stream here so we
+    ** can give the user some idea about what has happened.
+    */
+    if (MR_file(*mf) == NULL) {
+        mercury_io_error(mf, ""error closing file: invalid file stream"");
+    }
+
      if (MR_CLOSE(*mf) < 0) {
          mercury_io_error(mf, ""error closing file: %s"", strerror(errno));
      }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20130520/ce188cc4/attachment.html>


More information about the reviews mailing list