From d66f7be06612a27e6fa78b07a2e76e882a73aa4a Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Sat, 29 Jul 2017 13:22:24 +0200
Subject: [PATCH] ENH: more graceful handling of invalid IOobject headers
 (issue #539)

- With special-purpose templating it is possible to have file contents
  that almost look like an OpenFOAM file, but which are not.
  The contents do not need to be deliberately tricky, even the simplest
  README:

      FoamFile is the first word parsed in OpenFOAM files

  will trigger problems.
  We now trap any IOerror on these and reject this type of file as invalid.
---
 src/OpenFOAM/db/IOobjectList/IOobjectList.C | 25 ++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C
index 981e5ca2e5..f70f3e7ebf 100644
--- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C
+++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C
@@ -154,14 +154,14 @@ Foam::IOobjectList::IOobjectList
     }
 
     // Create a list of file names in this directory
-    fileNameList objNames =
+    const auto objNames =
         readDir(db.path(newInstance, db.dbDir()/local), fileName::FILE);
 
-    forAll(objNames, i)
+    for (const auto& objName : objNames)
     {
         IOobject* objectPtr = new IOobject
         (
-            objNames[i],
+            objName,
             newInstance,
             local,
             db,
@@ -170,8 +170,23 @@ Foam::IOobjectList::IOobjectList
             registerObject
         );
 
-        // Use object with local scope
-        if (objectPtr->typeHeaderOk<IOList<label>>(false))
+        bool ok = false;
+        const bool throwingIOerr = FatalIOError.throwExceptions();
+
+        try
+        {
+            // Use object with local scope and current instance (no searching)
+            ok = objectPtr->typeHeaderOk<IOList<label>>(false, false);
+        }
+        catch (Foam::IOerror& err)
+        {
+            Warning
+                << err << nl << endl;
+        }
+
+        FatalIOError.throwExceptions(throwingIOerr);
+
+        if (ok)
         {
             insert(objectPtr->name(), objectPtr);
         }
-- 
GitLab