diff libinterp/corefcn/load-save.cc @ 17183:ca5103ab0b21

check_gzip_magic before get_file_format (wrong type detection, bug #39652) * load-save.cc (get_file_format): call check_gzip_magic before get_file_format to avoid random LS_MAT_ASCII detections in gzipped files. * ls-mat-ascii.cc (looks_like_mat_ascii_file): New arg, IS which could also be a gzipped file, is now opened in the calling function. * ls-mat-ascii.h (looks_like_mat_ascii_file): New arg, IS.
author Andreas Weber <andy.weber.aw@gmail.com>
date Fri, 02 Aug 2013 19:48:34 +0200
parents d6499c14021c
children bc924baa2c4e
line wrap: on
line diff
--- a/libinterp/corefcn/load-save.cc	Mon Aug 05 11:43:58 2013 -0700
+++ b/libinterp/corefcn/load-save.cc	Fri Aug 02 19:48:34 2013 +0200
@@ -269,6 +269,20 @@
 
               if (! tmp.empty ())
                 retval = LS_ASCII;
+              else
+                {
+                  file.clear ();
+                  file.seekg (0, std::ios::beg);
+
+                  // FIXME -- looks_like_mat_ascii_file does not check to see
+                  // whether the file contains numbers.  It just skips comments and
+                  // checks for the same number of words on each line.  We may need
+                  // a better check here.  The best way to do that might be just
+                  // to try to read the file and see if it works.
+
+                  if (looks_like_mat_ascii_file (file, filename))
+                    retval = LS_MAT_ASCII;
+                }
             }
         }
     }
@@ -288,39 +302,36 @@
     return LS_HDF5;
 #endif /* HAVE_HDF5 */
 
-  std::ifstream file (fname.c_str ());
+#ifdef HAVE_ZLIB
+  use_zlib = check_gzip_magic (fname);
+#else
   use_zlib = false;
-
-  if (file)
-    {
-      retval = get_file_format (file, orig_fname);
-      file.close ();
-
-#ifdef HAVE_ZLIB
-      if (retval == LS_UNKNOWN && check_gzip_magic (fname))
-        {
-          gzifstream gzfile (fname.c_str ());
-          use_zlib = true;
-
-          if (gzfile)
-            {
-              retval = get_file_format (gzfile, orig_fname);
-              gzfile.close ();
-            }
-        }
 #endif
 
-      // FIXME -- looks_like_mat_ascii_file does not check to see
-      // whether the file contains numbers.  It just skips comments and
-      // checks for the same number of words on each line.  We may need
-      // a better check here.  The best way to do that might be just
-      // to try to read the file and see if it works.
-
-      if (retval == LS_UNKNOWN && looks_like_mat_ascii_file (fname))
-        retval = LS_MAT_ASCII;
+  if (! use_zlib)
+    {
+      std::ifstream file (fname.c_str ());
+      if (file)
+        {
+          retval = get_file_format (file, orig_fname);
+          file.close ();
+        }
+      else if (! quiet)
+        gripe_file_open ("load", orig_fname);
     }
-  else if (! quiet)
-    gripe_file_open ("load", orig_fname);
+#ifdef HAVE_ZLIB
+  else
+    {
+      gzifstream gzfile (fname.c_str ());
+      if (gzfile)
+        {
+          retval = get_file_format (gzfile, orig_fname);
+          gzfile.close ();
+        }
+      else if (! quiet)
+        gripe_file_open ("load", orig_fname);
+    }
+#endif
 
   return retval;
 }