external_objects_java: fix crash on ScilabJavaEnvironment when used as a local variable 30/13030/2
Simon Marchetto [Tue, 29 Oct 2013 15:24:31 +0000 (16:24 +0100)]
Fix: ScilabJavaEnvironment::getInstance() returns a pointer on instance, not the instance itself
+  return Scilab error when instance is null

Change-Id: I0645534db16ed489a59360fd6a310ae66e4a9657

scilab/modules/external_objects_java/sci_gateway/cpp/sci_jallowClassReloading.cpp
scilab/modules/external_objects_java/sci_gateway/cpp/sci_jautoTranspose.cpp
scilab/modules/external_objects_java/sci_gateway/cpp/sci_jautoUnwrap.cpp
scilab/modules/external_objects_java/sci_gateway/cpp/sci_jconvMatrixMethod.cpp
scilab/modules/external_objects_java/src/cpp/ScilabJavaEnvironment.hxx

index 21c5547..a8635fb 100644 (file)
@@ -27,7 +27,15 @@ int sci_jallowClassReloading(char * fname, unsigned long fname_len)
     try
     {
         const int envId = ScilabJavaEnvironment::start();
-        JavaOptionsSetter setter = ScilabJavaEnvironment::getInstance().getOptionsHelper().getSetter(JavaOptionsSetter::ALLOWRELOAD);
+
+        ScilabJavaEnvironment *javaEnvironment = ScilabJavaEnvironment::getInstance();
+        if (!javaEnvironment)
+        {
+            Scierror(999, "%s: No Java environment available (instance is null).", fname);
+            return 0;
+        }
+
+        JavaOptionsSetter setter = javaEnvironment->getOptionsHelper().getSetter(JavaOptionsSetter::ALLOWRELOAD);
         return ScilabGateway::getsetOptions(fname, envId, setter, pvApiCtx);
     }
     catch (std::exception & e)
index 6fd4a18..bad319e 100644 (file)
@@ -27,7 +27,14 @@ int sci_jautoTranspose(char * fname, unsigned long fname_len)
     try
     {
         const int envId = ScilabJavaEnvironment::start();
-        JavaOptionsSetter setter = ScilabJavaEnvironment::getInstance().getOptionsHelper().getSetter(JavaOptionsSetter::METHODOFCONV);
+        ScilabJavaEnvironment *javaEnvironment = ScilabJavaEnvironment::getInstance();
+        if (!javaEnvironment)
+        {
+            Scierror(999, "%s: No Java environment available (instance is null).", fname);
+            return 0;
+        }
+
+        JavaOptionsSetter setter = javaEnvironment->getOptionsHelper().getSetter(JavaOptionsSetter::METHODOFCONV);
         return ScilabGateway::getsetOptions(fname, envId, setter, pvApiCtx);
     }
     catch (std::exception & e)
index 1d6e296..0202cae 100644 (file)
@@ -27,7 +27,14 @@ int sci_jautoUnwrap(char * fname, unsigned long fname_len)
     try
     {
         const int envId = ScilabJavaEnvironment::start();
-        JavaOptionsSetter setter = ScilabJavaEnvironment::getInstance().getOptionsHelper().getSetter(JavaOptionsSetter::AUTOUNWRAP);
+        ScilabJavaEnvironment *javaEnvironment = ScilabJavaEnvironment::getInstance();
+        if (!javaEnvironment)
+        {
+            Scierror(999, "%s: No Java environment available (instance is null).", fname);
+            return 0;
+        }
+
+        JavaOptionsSetter setter = javaEnvironment->getOptionsHelper().getSetter(JavaOptionsSetter::AUTOUNWRAP);
         return ScilabGateway::getsetOptions(fname, envId, setter, pvApiCtx);
     }
     catch (std::exception & e)
index db72e08..5a87f93 100644 (file)
@@ -38,7 +38,7 @@ int sci_jconvMatrixMethod(char * fname, unsigned long fname_len)
     }
 
     envId = ScilabJavaEnvironment::start();
-    JavaOptionsSetter setter = ScilabJavaEnvironment::getInstance().getOptionsHelper().getSetter(JavaOptionsSetter::METHODOFCONV);
+    JavaOptionsSetter setter = ScilabJavaEnvironment::getInstance()->getOptionsHelper().getSetter(JavaOptionsSetter::METHODOFCONV);
     ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
     ScilabGatewayOptions & options = env.getGatewayOptions();
     OptionsHelper::setCopyOccurred(false);
index f522baf..6bcb112 100644 (file)
@@ -128,9 +128,9 @@ public :
 
     static void finish();
 
-    static ScilabJavaEnvironment & getInstance()
+    static ScilabJavaEnvironment* getInstance()
     {
-        return *instance;
+        return instance;
     }
 
     JavaOptionsHelper & getOptionsHelper();