* Bug #12426 fixed - By using addErrorMessage, random string error can be displayed. 51/10951/4
Simon Marchetto [Fri, 22 Mar 2013 11:18:50 +0000 (12:18 +0100)]
Change-Id: I2ea54291e1a329b25d134c40ebabc873a2cb672f

scilab/CHANGES_5.4.X
scilab/modules/api_scilab/src/cpp/api_error.cpp
scilab/modules/api_scilab/tests/nonreg_tests/bug_12426.tst [new file with mode: 0644]
scilab/modules/api_scilab/tests/nonreg_tests/build_bug_12426_gateway.sce [new file with mode: 0644]
scilab/modules/api_scilab/tests/nonreg_tests/sci_addErrorMessage.c [new file with mode: 0644]

index 9947f68..7cd1453 100644 (file)
@@ -646,6 +646,8 @@ Xcos
 
 * bug #12415 fixed - PATH environment variable grows when using call_scilab in a loop
 
+* Bug #12426 fixed - By using addErrorMessage, random string error can be displayed.
+
 
                     Changes between version 5.3.3 and 5.4.0
                     =======================================
index 44b927e..afe9f8d 100644 (file)
@@ -9,12 +9,12 @@
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  * Please note that piece of code will be rewrited for the Scilab 6 family
- * However, the API (profile of the functions in the header files) will be 
+ * However, the API (profile of the functions in the header files) will be
  * still available and supported in Scilab 6.
  */
 
 #include "MALLOC.h"
-#include <stdio.h> 
+#include <stdio.h>
 #include "api_scilab.h"
 #include "sciprint.h"
 #include "Scierror.h"
 #include "localization.h"
 extern "C"
 {
-       #include "stackinfo.h"
-       #include "mode_exec.h"
+#include "stackinfo.h"
+#include "mode_exec.h"
 }
 
 int addStackSizeError(SciErr* _psciErr, char* _pstCaller, int _iNeeded)
 {
-       char pstMsg1[bsiz];
-       char pstMsg2[bsiz];
-       char pstMsg3[bsiz];
-       char pstMsg4[bsiz];
-       char pstMsg5[bsiz];
+    char pstMsg1[bsiz];
+    char pstMsg2[bsiz];
+    char pstMsg3[bsiz];
+    char pstMsg4[bsiz];
+    char pstMsg5[bsiz];
 
-       int Memory_used_for_variables = 0;
-       int Total_Memory_available = 0;
+    int Memory_used_for_variables = 0;
+    int Total_Memory_available = 0;
 
-       C2F(getstackinfo)(&Total_Memory_available,&Memory_used_for_variables);
+    C2F(getstackinfo)(&Total_Memory_available, &Memory_used_for_variables);
 
 #ifdef _MSC_VER
-       sprintf_s(pstMsg1, bsiz, "%s\n%s", _pstCaller, _("stack size exceeded!\n"));
-       sprintf_s(pstMsg2, bsiz, _("Use stacksize function to increase it.\n"));
-       sprintf_s(pstMsg3, bsiz, _("Memory used for variables: %d\n"),Memory_used_for_variables);
-       sprintf_s(pstMsg4, bsiz, _("Intermediate memory needed: %d\n"),_iNeeded);
-       sprintf_s(pstMsg5, bsiz, _("Total memory available: %d\n"),Total_Memory_available);
+    sprintf_s(pstMsg1, bsiz, "%s\n%s", _pstCaller, _("stack size exceeded!\n"));
+    sprintf_s(pstMsg2, bsiz, _("Use stacksize function to increase it.\n"));
+    sprintf_s(pstMsg3, bsiz, _("Memory used for variables: %d\n"), Memory_used_for_variables);
+    sprintf_s(pstMsg4, bsiz, _("Intermediate memory needed: %d\n"), _iNeeded);
+    sprintf_s(pstMsg5, bsiz, _("Total memory available: %d\n"), Total_Memory_available);
 #else
-       sprintf(pstMsg1, _("stack size exceeded!\n"));
-       sprintf(pstMsg2, _("Use stacksize function to increase it.\n"));
-       sprintf(pstMsg3, _("Memory used for variables: %d\n"),Memory_used_for_variables);
-       sprintf(pstMsg4, _("Intermediate memory needed: %d\n"),_iNeeded);
-       sprintf(pstMsg5, _("Total memory available: %d\n"),Total_Memory_available);
+    sprintf(pstMsg1, _("stack size exceeded!\n"));
+    sprintf(pstMsg2, _("Use stacksize function to increase it.\n"));
+    sprintf(pstMsg3, _("Memory used for variables: %d\n"), Memory_used_for_variables);
+    sprintf(pstMsg4, _("Intermediate memory needed: %d\n"), _iNeeded);
+    sprintf(pstMsg5, _("Total memory available: %d\n"), Total_Memory_available);
 #endif
 
-       strcat(pstMsg1, pstMsg2);
-       strcat(pstMsg1, pstMsg3);
-       strcat(pstMsg1, pstMsg4);
-       strcat(pstMsg1, pstMsg5);
+    strcat(pstMsg1, pstMsg2);
+    strcat(pstMsg1, pstMsg3);
+    strcat(pstMsg1, pstMsg4);
+    strcat(pstMsg1, pstMsg5);
 
-       return addErrorMessage(_psciErr, 17, pstMsg1);
+    return addErrorMessage(_psciErr, 17, pstMsg1);
 }
 
 int addErrorMessage(SciErr* _psciErr, int _iErr, const char* _pstMsg, ...)
 {
-       int iRet = 0;
-       char pstMsg[bsiz];
-       va_list ap;
+    int iRet = 0;
+    char pstMsg[bsiz];
+    va_list ap;
 
-       va_start(ap,_pstMsg);
+    va_start(ap, _pstMsg);
 #if defined (vsnprintf) || defined (linux)
-       iRet = vsnprintf(pstMsg, bsiz-1, _pstMsg, ap );
+    iRet = vsnprintf(pstMsg, bsiz - 1, _pstMsg, ap );
 #else
-       iRet = vsprintf(pstMsg, _pstMsg, ap );
+    iRet = vsprintf(pstMsg, _pstMsg, ap );
 #endif
-
-       if(_psciErr->iMsgCount == MESSAGE_STACK_SIZE - 1)
-       {//rotation ...
-               FREE(_psciErr->pstMsg[0]);
-               for(int i = MESSAGE_STACK_SIZE - 1 ; i > 0 ; i--)
-               {
-                       _psciErr->pstMsg[i - 1] = _psciErr->pstMsg[i];
-               }
-       }
-       _psciErr->pstMsg[_psciErr->iMsgCount++] = strdup(pstMsg);
-       _psciErr->iErr = _iErr;
-       return iRet;
+    va_end(ap);
+
+    if (_psciErr->iMsgCount >= MESSAGE_STACK_SIZE)
+    {
+        // no more space, shift error messages
+        FREE(_psciErr->pstMsg[0]);
+        for (int i = 1; i < MESSAGE_STACK_SIZE; i++)
+        {
+            _psciErr->pstMsg[i - 1] = _psciErr->pstMsg[i];
+        }
+        _psciErr->pstMsg[MESSAGE_STACK_SIZE - 1] = strdup(pstMsg);
+    }
+    else
+    {
+        _psciErr->pstMsg[_psciErr->iMsgCount++] = strdup(pstMsg);
+    }
+
+    _psciErr->iErr = _iErr;
+    return iRet;
 }
 
 int printError(SciErr* _psciErr, int _iLastMsg)
 {
-       int iMode = getExecMode();
-
-       if(_psciErr->iErr == 0)
-       {
-               return 0;
-       }
-
-       SciStoreError(_psciErr->iErr);
-
-       if(iMode == SILENT_EXEC_MODE)
-       {
-               return 0;
-       }
-
-       if(_iLastMsg)
-       {
-               sciprint(_("API Error:\n"));
-               sciprint(_("\tin %s\n"), _psciErr->pstMsg[0]);
-       }
-       else
-       {
-               sciprint(_("API Error:\n"));
-
-               //              for(int i = 0 ;  i < _psciErr->iMsgCount ;i++)
-               for(int i = _psciErr->iMsgCount - 1 ;  i >= 0 ; i--)
-               {
-//                     if(i == 0)
-                       sciprint(_("\tin %s\n"), _psciErr->pstMsg[i]);
-               }
-       }
-       return 0;
+    int iMode = getExecMode();
+
+    if (_psciErr->iErr == 0)
+    {
+        return 0;
+    }
+
+    SciStoreError(_psciErr->iErr);
+
+    if (iMode == SILENT_EXEC_MODE)
+    {
+        return 0;
+    }
+
+    if (_iLastMsg)
+    {
+        sciprint(_("API Error:\n"));
+        sciprint(_("\tin %s\n"), _psciErr->pstMsg[0]);
+    }
+    else
+    {
+        sciprint(_("API Error:\n"));
+
+        for (int i = _psciErr->iMsgCount - 1 ;  i >= 0 ; i--)
+        {
+            sciprint(_("\tin %s\n"), _psciErr->pstMsg[i]);
+        }
+    }
+    return 0;
 }
 
 char* getErrorMessage(SciErr _sciErr)
 {
-       if(_sciErr.iErr == 0)
-       {
-               return NULL;
-       }
+    if (_sciErr.iErr == 0)
+    {
+        return NULL;
+    }
 
-       return _sciErr.pstMsg[0];
+    return _sciErr.pstMsg[0];
 }
 
diff --git a/scilab/modules/api_scilab/tests/nonreg_tests/bug_12426.tst b/scilab/modules/api_scilab/tests/nonreg_tests/bug_12426.tst
new file mode 100644 (file)
index 0000000..50f6cff
--- /dev/null
@@ -0,0 +1,39 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// <-- INTERACTIVE TEST -->
+//
+// <-- Non-regression test for bug 12426 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=12426
+//
+// <-- Short Description -->
+// By using addErrorMessage, random string error can be displayed.
+
+ilib_verbose(0);
+dir = get_absolute_file_path("bug_12426.tst");
+exec(dir + "/build_bug_12426_gateway.sce", "errcatch", -1);
+exec(TMPDIR + "/bug_12426_gateway/loader.sce", "errcatch", -1);
+
+n = int32(1);
+disp("add 1 error message");
+addErrorMessage(n);
+
+n = int32(4);
+disp("add 4 error messages");
+addErrorMessage(n);
+
+n = int32(5);
+disp("add 5 error messages");
+addErrorMessage(n);
+
+n = int32(6);
+disp("add 6 error messages");
+addErrorMessage(n);
diff --git a/scilab/modules/api_scilab/tests/nonreg_tests/build_bug_12426_gateway.sce b/scilab/modules/api_scilab/tests/nonreg_tests/build_bug_12426_gateway.sce
new file mode 100644 (file)
index 0000000..1a5e40e
--- /dev/null
@@ -0,0 +1,61 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Simon MARCHETTO
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution.  The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+
+gatewayName = "bug_12426_gateway";
+lib1 = "api_scilab";
+lib1include = SCI + "/modules/" + lib1 + "/includes";
+func1 = "addErrorMessage";
+
+previous_dir = pwd();
+cd(get_absolute_file_path("build_" + gatewayName + ".sce"));
+current_dir = pwd();
+mkdir(TMPDIR + "/" + gatewayName);
+cd(TMPDIR + "/" + gatewayName);
+
+ilib_name = gatewayName;
+files = ["sci_" + func1 + ".c"];
+
+for i = 1 : size(files, "*")
+    copyfile(current_dir + "/" + files(i), TMPDIR + "/" + gatewayName);
+end
+
+if getos() == "Windows" then
+    libs   = SCI  + "/bin/" + lib1;
+    cflags = "-I " + lib1include;
+
+elseif isdir(lib1include)
+    // Unix source version
+    libs   = SCI + "/modules/" + lib1 + "/.libs/lib" + lib1;
+    cflags = "-I " lib1include;
+else
+    // Unix binary version
+    SCI_LIB     = strsubst(SCI,"/share\/scilab$/", "lib/scilab", "r");
+    SCI_INCLUDE = strsubst(SCI,"/share\/scilab$/", "include/scilab", "r");
+    libs        = SCI_LIB + "/lib" + lib1 + ..
+                  SCI_LIB + "/lib" + lib1;
+    cflags      = "-I " + SCI_INCLUDE;
+end
+
+// name known in scilab, C function called
+table = [func1, "sci_" + func1];
+
+libname = ilib_build(ilib_name, table, files, libs, [], "", cflags);
+
+cd(previous_dir);
+
+clear ilib_build;
+clear libname;
+clear table;
+clear libs;
+clear files;
+clear ilib_name;
+clear previous_dir;
+clear gateway_c_dir;
+clear SCI_LIB;
+clear SCI_INCLUDE;
diff --git a/scilab/modules/api_scilab/tests/nonreg_tests/sci_addErrorMessage.c b/scilab/modules/api_scilab/tests/nonreg_tests/sci_addErrorMessage.c
new file mode 100644 (file)
index 0000000..aab35cf
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) Scilab Enterprises - 2013 - Simon MARCHETTO
+*
+* This file must be used under the terms of the CeCILL.
+* This source file is licensed as described in the file COPYING, which
+* you should have received as part of this distribution.  The terms
+* are also available at
+* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+*/
+
+#include <stdio.h>
+#include "api_scilab.h"
+#include "api_int.h"
+
+/*--------------------------------------------------------------------------*/
+int sci_addErrorMessage(char *fname, unsigned long fname_len)
+{
+    SciErr sciErr;
+    int *piAddressVarOne = NULL;
+    int errorMessageCount = 0;
+    sciErr.iMsgCount = 0;
+
+    CheckInputArgument(pvApiCtx, 1, 1);
+    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
+    if (!sciErr.iErr)
+    {
+        if (isIntegerType(pvApiCtx, piAddressVarOne))
+        {
+            if (!getScalarInteger32(pvApiCtx, piAddressVarOne,
+                                    &errorMessageCount));
+            {
+                SciErr sciErr;
+                char errMsg[100];
+                int i = 0;
+                sciErr.iMsgCount = 0;
+
+                for (i = 1; i <= errorMessageCount; i++)
+                {
+                    sprintf(errMsg, "Error %d", i);
+                    addErrorMessage(&sciErr, 999, errMsg);
+                }
+                printError(&sciErr, 0);
+
+                return -1;
+            }
+        }
+    }
+    return -1;
+}
+/*--------------------------------------------------------------------------*/