Coverity #1321338, #1321354 fixed 23/18223/2
Dhruv Khattar [Mon, 6 Jun 2016 21:53:02 +0000 (02:53 +0530)]
Change-Id: Id18c8a3f844695a1c04095104652d163eda0fbd0

scilab/modules/linear_algebra/sci_gateway/cpp/sci_schur.cpp
scilab/modules/linear_algebra/src/c/svd.c

index 44f56da..2912969 100644 (file)
@@ -224,12 +224,11 @@ types::Function::ReturnValue sci_schur(types::typed_list &in, int _iRetCount, ty
         {
             if (pDbl[0]->getCols() == 0)
             {
-                types::Double* zero = new types::Double(0);
-
                 for (int i = 0; i < _iRetCount; i++)
                 {
                     if (i == 1 && !bIsComplexStr && !bIsRealStr)
                     {
+                        types::Double* zero = new types::Double(0);
                         out.push_back(zero);
                     }
                     else
@@ -245,8 +244,6 @@ types::Function::ReturnValue sci_schur(types::typed_list &in, int _iRetCount, ty
         {
             if (pDbl[0]->getCols() == 0)
             {
-                types::Double* zero = new types::Double(0);
-
                 for (int i = 1; i < _iRetCount; i++)
                 {
                     out.push_back(types::Double::Empty());
@@ -254,6 +251,7 @@ types::Function::ReturnValue sci_schur(types::typed_list &in, int _iRetCount, ty
 
                 if (_iRetCount > 1)
                 {
+                    types::Double* zero = new types::Double(0);
                     out.push_back(zero);
                 }
                 else
index ac4cea3..811e31b 100644 (file)
@@ -129,10 +129,11 @@ int iSvdM(double* pData, int iRows, int iCols, int complexArg, int economy, doub
     double* pWork = NULL;
     double* pRWork = NULL;
     double* pVT = NULL;
+    double* _pSV = NULL;
 
     char const job = pU ? (economy ? 'S' : 'A') : 'N';
     int colsToCompute = (job == 'S') ? Min(iRows, iCols) : iCols;
-    pSV = pSV ? pSV : (double*) MALLOC(Min(iRows, iCols) * sizeof(double));
+    _pSV = pSV ? pSV : (double*) MALLOC(Min(iRows, iCols) * sizeof(double));
     if ( (allocOK = ( (pWork = (complexArg
                                 ? (double*)allocZgesvdWorkspace(job, iRows, iCols, colsToCompute, &worksize)
                                 : allocDgesvdWorkspace(job, iRows, iCols, colsToCompute, &worksize)))
@@ -143,28 +144,28 @@ int iSvdM(double* pData, int iRows, int iCols, int complexArg, int economy, doub
                                  : pData)))))
     {
         ret = complexArg
-              ? iZgesvd(job,  (doublecomplex*)pData, iRows, iCols, colsToCompute, pSV, (doublecomplex*) pU, (doublecomplex*)pVT
+              ? iZgesvd(job,  (doublecomplex*)pData, iRows, iCols, colsToCompute, _pSV, (doublecomplex*) pU, (doublecomplex*)pVT
                         , (doublecomplex*)pWork, worksize, pRWork)
-              : iDgesvd(job, pData, iRows, iCols, colsToCompute, pSV, pU, pVT, pWork, worksize);
+              : iDgesvd(job, pData, iRows, iCols, colsToCompute, _pSV, pU, pVT, pWork, worksize);
         if ( (ret == 0) && pU)
         {
             /* openmp sections */
             /* openmp section */
             {
                 int unused = complexArg
-                             ? iComputeSandVComplex(pSV, (doublecomplex*)pVT, iRows, iCols, economy, pS, (doublecomplex*)pV)
-                             : iComputeSandVReal(pSV, pVT, iRows, iCols, economy, pS, pV);
+                             ? iComputeSandVComplex(_pSV, (doublecomplex*)pVT, iRows, iCols, economy, pS, (doublecomplex*)pV)
+                             : iComputeSandVReal(_pSV, pVT, iRows, iCols, economy, pS, pV);
             }
             /* openmp section */
             {
                 if (pRk) /* compute rank */
                 {
                     int i, rk;
-                    tol = (tol == 0.) ? (Max(iRows, iCols) * nc_eps() * pSV[0]) : tol; /* original Fortran code does the strict fp compare */
+                    tol = (tol == 0.) ? (Max(iRows, iCols) * nc_eps() * _pSV[0]) : tol; /* original Fortran code does the strict fp compare */
                     rk = -1;
                     for (i = 0; i != Min(iRows, iCols); ++i)
                     {
-                        if (pSV[i] > tol)
+                        if (_pSV[i] > tol)
                         {
                             rk = i;
                         }
@@ -180,6 +181,10 @@ int iSvdM(double* pData, int iRows, int iCols, int complexArg, int economy, doub
     {
         FREE(pVT);
     }
+    if (!pSV)
+    {
+        FREE(_pSV);
+    }
     return allocOK ? ret : -1;
 }