Coverity: fix #1400678, #743503, #1400677, #1400675, #1400676 40/20940/3
Clément DAVID [Fri, 12 Apr 2019 15:01:41 +0000 (17:01 +0200)]
Change-Id: I387f16f279348c5c7950c7876ff949c8af617e86

scilab/modules/io/sci_gateway/cpp/sci_file.cpp
scilab/modules/scicos/sci_gateway/cpp/sci_sig2data.cpp
scilab/modules/scicos_blocks/src/cpp/fromws_c.cpp
scilab/modules/ui_data/src/cpp/BrowseVarManager.cpp

index 0306432..842a4f8 100644 (file)
@@ -33,7 +33,7 @@ extern "C"
 #include "mclose.h"
 #include "mseek.h"
 
-    extern int C2F(clunit)(int* , char const*, int*, int);
+    extern int C2F(clunit)(int*, char const*, int*, int);
     extern int C2F(rewindinter)(int*);
     extern int C2F(backspaceinter)(int*);
     extern int C2F(readinter)(int*, char const*, int);
@@ -414,7 +414,7 @@ types::Function::ReturnValue sci_file_no_rhs(types::typed_list &in, int _iRetCou
             out.push_back(pS);
             for (int i = 0 ; i < iCount ; i++)
             {
-                delete[] pstNames[i];
+                FREE(pstNames[i]);
             }
             delete[] pstNames;
         }
index 75c23d6..9a36d2d 100644 (file)
@@ -104,26 +104,31 @@ types::Function::ReturnValue sci_sig2data(types::typed_list &in, int _iRetCount,
         Scierror(999, _("%s: Wrong size for input argument #%d : A single struct expected.\n"), funname.data(), 1);
         return types::Function::Error;
     }
+
     auto fields = B->get(0)->getFields();
     if (fields.size() != 2)
     {
         Scierror(999, _("%s: Wrong fields for input argument #%d : \"%s\" and \"%s\" expected.\n"), funname.data(), 1, "values", "time");
         return types::Function::Error;
     }
-    if (fields.find(L"values") == fields.end())
+
+    int valuesIdx = B->get(0)->getFieldIndex(L"values");
+    if (valuesIdx < 0)
     {
         Scierror(999, _("%s: Wrong fields for input argument #%d : \"%s\" and \"%s\" expected.\n"), funname.data(), 1, "values", "time");
         return types::Function::Error;
     }
-    if (fields.find(L"time") == fields.end())
+
+    int timeIdx = B->get(0)->getFieldIndex(L"time");
+    if (timeIdx < 0)
     {
         Scierror(999, _("%s: Wrong fields for input argument #%d : \"%s\" and \"%s\" expected.\n"), funname.data(), 1, "values", "time");
         return types::Function::Error;
     }
 
     // Values
-    types::InternalType* A = B->get(0)->getData()[B->get(0)->getFieldIndex(L"values")];
-    types::InternalType* timeValues = B->get(0)->getData()[B->get(0)->getFieldIndex(L"time")];
+    types::InternalType* A = B->get(0)->getData()[valuesIdx];
+    types::InternalType* timeValues = B->get(0)->getData()[timeIdx];
 
     out.push_back(A);
     if (_iRetCount == 2)
index 9578ee5..0df43ff 100644 (file)
@@ -318,12 +318,12 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                 switch (xSubType)
                 {
                     case 0 : /* Real */
-                        ptr->work = new double[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(double));
                         ptr_d = (double*) ptr->work;
                         ierr = readDoubleMatrix(xSetId, ptr_d);
                         break;
                     case 1 :  /* Complex */
-                        ptr->work = new double[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX * 2, sizeof(double));
                         ptr_d = (double*) ptr->work;
                         ierr = readDoubleComplexMatrix(xSetId, ptr_d, ptr_d + nPoints * mX * nX);
                         break;
@@ -335,32 +335,32 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                 switch (xSubType)
                 {
                     case SCI_INT8 :
-                        ptr->work = new char[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(char));
                         ptr_c = (char*)ptr->work;
                         ierr = readInteger8Matrix(xSetId, ptr_c);
                         break;
                     case SCI_INT16 :
-                        ptr->work = new short int[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(short int));
                         ptr_s = (short int*) ptr->work;
                         ierr = readInteger16Matrix(xSetId, ptr_s);
                         break;
                     case SCI_INT32 :
-                        ptr->work = new int[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(int));
                         ptr_l = (int*) ptr->work;
                         ierr = readInteger32Matrix(xSetId, ptr_l);
                         break;
                     case SCI_UINT8 :
-                        ptr->work = new unsigned char[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(unsigned char));
                         ptr_uc = (unsigned char*) ptr->work;
                         ierr = readUnsignedInteger8Matrix(xSetId, ptr_uc);
                         break;
                     case SCI_UINT16 :
-                        ptr->work = new unsigned short int[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(unsigned short int));
                         ptr_us = (unsigned short int*) ptr->work;
                         ierr = readUnsignedInteger16Matrix(xSetId, ptr_us);
                         break;
                     case SCI_UINT32 :
-                        ptr->work = new unsigned int[(nPoints + 1) * mX * nX];
+                        ptr->work = CALLOC((nPoints + 1) * mX * nX, sizeof(unsigned int));
                         ptr_ul = (unsigned int*) ptr->work;
                         ierr = readUnsignedInteger32Matrix(xSetId, ptr_ul);
                         break;
@@ -369,7 +369,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
             if (ierr != 0)
             {
                 Coserror(_("Cannot read the values field.\n"));
-                delete[] (char*) ptr->work;
+                FREE(ptr->work);
                 delete[] ptr;
                 closeHDF5File(fd);
                 return;
@@ -393,7 +393,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                 Coserror(_("The Time vector type is not ""double"".\n"));
                 set_block_error(-3);
                 *work = nullptr;
-                delete[] (char*) ptr->work;
+                FREE(ptr->work);
                 delete[] ptr;
                 closeHDF5File(fd);
                 return;
@@ -408,7 +408,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                 Coserror(_("The Time vector type is complex.\n"));
                 set_block_error(-3);
                 *work = nullptr;
-                delete[] (char*) ptr->work;
+                FREE(ptr->work);
                 delete[] ptr;
                 delete[] ptDims;
                 closeHDF5File(fd);
@@ -419,7 +419,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                 Coserror(_("The Time vector has a wrong size, expecting [%d, %d] and getting [%d, %d].\n"), nPoints, 1, ptDims[0], ptDims[1]);
                 /*set_block_error(-3);*/
                 *work = nullptr;
-                delete[] (char*) ptr->work;
+                FREE(ptr->work);
                 delete[] ptr;
                 delete[] ptDims;
                 closeHDF5File(fd);
@@ -434,7 +434,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
             {
                 Coserror(_("Cannot read the time field.\n"));
                 delete[] ptr->workt;
-                delete[] (char*) ptr->work;
+                FREE(ptr->work);
                 delete[] ptr;
                 closeHDF5File(fd);
                 return;
@@ -453,7 +453,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                     /*set_block_error(-3);*/
                     *work = nullptr;
                     delete[] ptr->workt;
-                    delete[] (char*) ptr->work;
+                    FREE(ptr->work);
                     delete[] ptr;
                     return;
                 }
@@ -1406,7 +1406,7 @@ SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)
                 }
                 if (ptr->work != nullptr)
                 {
-                    delete[] (char*) ptr->work;
+                    FREE(ptr->work);
                 }
                 if (ptr->workt != nullptr)
                 {
index b446745..36cc2d9 100644 (file)
@@ -148,9 +148,14 @@ void SetBrowseVarData()
 
         // type with Scilab < 6 compatibility (structs and cells have type 17)
         err = getVarType(NULL, (int*)pIT, &piAllVariableTypes[i]);
-        if (!err.iErr)
+        if (err.iErr)
         {
-            err = getVarDimension(NULL, (int*)pIT, &nbRows, &nbCols);
+            return;
+        }
+        err = getVarDimension(NULL, (int*)pIT, &nbRows, &nbCols);
+        if (err.iErr)
+        {
+            return;
         }
 
         if (pIT->isArrayOf() || pIT->isSparse())
@@ -158,10 +163,10 @@ void SetBrowseVarData()
             int nbRows = pIT->getAs<types::GenericType>()->getRows();
             int nbCols = pIT->getAs<types::GenericType>()->getCols();
             piAllVariableNbRows[i] = nbRows;
-            piAllVariableNbCols[i] = nbCols;                
-            if (nbRows*nbCols == 0)
+            piAllVariableNbCols[i] = nbCols;
+            if (nbRows * nbCols == 0)
             {
-                pstAllVariableSizes[i] = pIT->isCell() ? os_strdup(EMPTY_CELL) : os_strdup(EMPTY_MATRIX);                
+                pstAllVariableSizes[i] = pIT->isCell() ? os_strdup(EMPTY_CELL) : os_strdup(EMPTY_MATRIX);
             }
             else if (pIT->isArrayOf())
             {
@@ -187,6 +192,10 @@ void SetBrowseVarData()
             // Integer case
             int iPrec       = 0;
             err = getMatrixOfIntegerPrecision(NULL, (int*)pIT, &iPrec);
+            if (err.iErr)
+            {
+                return;
+            }
             switch (iPrec)
             {
                 case SCI_INT8:
@@ -240,10 +249,10 @@ void SetBrowseVarData()
         {
             piAllVariableFromUser[i] = FALSE;
         }
-        
+
         int bytesWithoutOverHead;
         pIT->getMemory(&bytesWithoutOverHead, &piAllVariableBytes[i]);
-        
+
         ++i;
     }
 
@@ -317,7 +326,8 @@ void SetBrowseVarData()
 /*--------------------------------------------------------------------------*/
 static std::set<string> createScilabDefaultVariablesSet()
 {
-    string arr[] = {
+    string arr[] =
+    {
         "home",
         "PWD",
         "%tk",
@@ -405,7 +415,8 @@ static char *valueToDisplay(types::InternalType* pIT)
     types::GenericType *pGT = pIT->getAs<types::GenericType>();
     int *piDims = pGT->getDimsArray();
 
-    if (pIT->isDouble() && pGT->getDims() < 3 && pGT->getSize() <= 4) {
+    if (pIT->isDouble() && pGT->getDims() < 3 && pGT->getSize() <= 4)
+    {
         // 4 is the dimension max to which display the content
         types::Double* pD = pIT->getAs<types::Double>();
         // Small double value, display it