From 2d8cb2ce13691055dfa23ba59f5ea0d3ed56b95d Mon Sep 17 00:00:00 2001
From: Victor Poughon <victor.poughon@cnes.fr>
Date: Wed, 5 Apr 2017 13:58:16 +0200
Subject: [PATCH] BUG: fix #1362, segfault if ChoiceParameter is empty

---
 .../include/otbWrapperChoiceParameter.h       |  7 +++---
 .../src/otbWrapperApplication.cxx             | 23 ++++++++++++-------
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/Modules/Wrappers/ApplicationEngine/include/otbWrapperChoiceParameter.h b/Modules/Wrappers/ApplicationEngine/include/otbWrapperChoiceParameter.h
index da4d4537b7..b376323214 100644
--- a/Modules/Wrappers/ApplicationEngine/include/otbWrapperChoiceParameter.h
+++ b/Modules/Wrappers/ApplicationEngine/include/otbWrapperChoiceParameter.h
@@ -90,13 +90,14 @@ public:
 
   bool HasValue() const ITK_OVERRIDE
   {
-    // a choice parameter always has a value
-    return true;
+    return !m_ChoiceList.empty();
   }
 
   void ClearValue() ITK_OVERRIDE
   {
-    // nothing to do : a choice parameter always has a value
+    // Same as constructor init value
+    // Note that this may be invalid if HasValue() == false
+    m_CurrentChoice = 0;
   }
 
 protected:
diff --git a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
index c2d772d151..16f2a28637 100644
--- a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
+++ b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
@@ -1015,19 +1015,26 @@ std::string Application::GetParameterString(std::string parameter)
   Parameter* param = GetParameterByKey(parameter);
 
   if (dynamic_cast<ChoiceParameter*>(param))
-    {
+  {
     ChoiceParameter* paramDown = dynamic_cast<ChoiceParameter*>(param);
-    std::string choiceKey = paramDown->GetChoiceKey( paramDown->GetValue() );
-    size_t lastPointPos = choiceKey.find_last_of('.');
-    if(lastPointPos != std::string::npos)
+    if (paramDown->HasValue())
+    {
+      std::string choiceKey = paramDown->GetChoiceKey( paramDown->GetValue() );
+      size_t lastPointPos = choiceKey.find_last_of('.');
+      if(lastPointPos != std::string::npos)
       {
-      ret = choiceKey.substr(lastPointPos);
-        }
-    else
+        ret = choiceKey.substr(lastPointPos);
+      }
+      else
       {
-      ret = choiceKey;
+        ret = choiceKey;
       }
     }
+    else
+    {
+        ret = "";
+    }
+  }
   else if (dynamic_cast<ListViewParameter*>(param))
     {
     ListViewParameter* paramDown = dynamic_cast<ListViewParameter*>(param);
-- 
GitLab