From f5e80d6382463ed3fd8cfe0ed9dfeea0b50e0d8e Mon Sep 17 00:00:00 2001
From: Luc Hermitte <luc.hermitte@c-s.fr>
Date: Tue, 26 Jun 2012 20:47:33 +0200
Subject: [PATCH] BUG: OTB-134/OGR -- infinite recursion & modifiable layers

---
 .../OGRAdapters/otbOGRDataSourceWrapper.cxx   | 30 +++++++++++++++----
 .../OGRAdapters/otbOGRDataSourceWrapper.h     |  2 +-
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx
index 2aa039e96d..2562d10417 100644
--- a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx
+++ b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.cxx
@@ -440,11 +440,21 @@ bool otb::ogr::DataSource::IsLayerModifiable(size_t i) const
 bool otb::ogr::DataSource::IsLayerModifiable(std::string const& layername) const
 {
   assert(m_DataSource && "Datasource not initialized");
-  const size_t id = GetLayerID(layername);
-  return IsLayerModifiable(id);
+  switch(m_OpenMode)
+  {
+    case Modes::Read:
+      return false;
+    case Modes::Update_LayerCreateOnly:
+        {
+        const int id = this->GetLayerIDUnchecked(layername);
+        return id >= m_FirstModifiableLayerID;
+        }
+    default:
+      return true;
+  }
 }
 
-size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const
+int otb::ogr::DataSource::GetLayerIDUnchecked(std::string const& name) const
 {
   assert(m_DataSource && "Datasource not initialized");
   for (int i = 0, N = GetLayersCount(); i < N; i++)
@@ -457,10 +467,18 @@ size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const
       return i;
       }
     }
+  return -1;
+}
 
-  itkExceptionMacro( << "Cannot fetch any layer named <" << name
-    << "> in the OGRDataSource <" << m_DataSource->GetName() << ">: "
-    << CPLGetLastErrorMsg());
+size_t otb::ogr::DataSource::GetLayerID(std::string const& name) const
+{
+  int const id = GetLayerIDUnchecked(name);
+  if (id < 0)
+    {
+    itkExceptionMacro( << "Cannot fetch any layer named <" << name
+      << "> in the OGRDataSource <" << m_DataSource->GetName() << ">: "
+      << CPLGetLastErrorMsg());
+    }
   return 0; // keep compiler happy
 }
 
diff --git a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h
index 832348cf47..6b5ce9b82b 100644
--- a/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h
+++ b/Code/UtilitiesAdapters/OGRAdapters/otbOGRDataSourceWrapper.h
@@ -520,10 +520,10 @@ private:
   OGRLayer* GetLayerUnchecked(size_t i) const;
 
   bool IsLayerModifiable(size_t i) const;
-
   bool IsLayerModifiable(std::string const& name) const;
 
   size_t GetLayerID(std::string const& name) const;
+  int GetLayerIDUnchecked(std::string const& name) const;
 
 private:
   OGRDataSource  *m_DataSource;
-- 
GitLab