Coverity #1321356 till 1321365 Fix 75/17975/7
Aashish Misraa [Thu, 24 Mar 2016 16:57:12 +0000 (21:57 +0530)]
Change-Id: Iaf4adb9aa0cfdb76b94fcf946c5f43f4650bfeb6

scilab/modules/linear_algebra/src/cpp/schurSelect.cpp

index 7b5d3e2..2bbf58e 100644 (file)
@@ -58,12 +58,27 @@ int schurSelect(types::Double** _pDblIn, types::Double** _pDblOut, bool _bIsComp
     if (_pDblIn[1] == NULL && _bIsComplex == false)
     {
         //dgees
-        double* pWR = (double*)MALLOC(iCols * sizeof(double));
-        double* pWI = (double*)MALLOC(iCols * sizeof(double));
-        pRwork = allocDgeesWorkspace(iCols, iDim, &iWorksize);
+        double* pWR = NULL;
+        double* pWI = NULL;
+        
+        if ((pWR = (double*)MALLOC(iCols * sizeof(double))) == NULL)
+        {
+            FREE(pBwork);
+            return -1;
+        }
+
+        if ((pWI = (double*)MALLOC(iCols * sizeof(double))) == NULL)
+        {
+            FREE(pWR);
+            FREE(pBwork);
+            return -1;
+        }
 
-        if (pWR == NULL || pWI == NULL || pRwork == NULL)
+        if ((pRwork = allocDgeesWorkspace(iCols, iDim, &iWorksize)) == NULL)
         {
+            FREE(pWR);
+            FREE(pWI);
+            FREE(pBwork);
             return -1;
         }
 
@@ -118,12 +133,25 @@ int schurSelect(types::Double** _pDblIn, types::Double** _pDblOut, bool _bIsComp
     {
         //zgees
         doublecomplex* pW = NULL;
-        pRwork      = (double*)MALLOC(iCols * sizeof(double));
-        pW          = (doublecomplex*)MALLOC(iCols * sizeof(doublecomplex));
-        pCplxWork   = allocZgeesWorkspace(iCols, iDim, &iWorksize);
+        
+        if ((pRwork = (double*)MALLOC(iCols * sizeof(double))) == NULL)
+        {
+            FREE(pBwork);
+            return -1;
+        }
 
-        if (pRwork == NULL || pW == NULL || pCplxWork == NULL)
+        if ((pW = (doublecomplex*)MALLOC(iCols * sizeof(doublecomplex))) == NULL)
         {
+            FREE(pRwork);
+            FREE(pBwork);
+            return -1;
+        }
+
+        if ((pCplxWork = allocZgeesWorkspace(iCols, iDim, &iWorksize)) == NULL)
+        {
+            FREE(pW);
+            FREE(pRwork);
+            FREE(pBwork);
             return -1;
         }
 
@@ -185,13 +213,37 @@ int schurSelect(types::Double** _pDblIn, types::Double** _pDblOut, bool _bIsComp
     else if (_pDblIn[1] && _bIsComplex == false)
     {
         //dgges
-        double* pAlphaR = (double*)MALLOC(iCols * sizeof(double));
-        double* pAlphaI = (double*)MALLOC(iCols * sizeof(double));
-        double* pBeta   = (double*)MALLOC(iCols * sizeof(double));
-        pRwork = allocDggesWorkspace(iCols, iDim, &iWorksize);
+        double* pAlphaR = NULL;
+        double* pAlphaI = NULL;
+        double* pBeta = NULL;
+        
+        if ((pAlphaR = (double*)MALLOC(iCols * sizeof(double))) == NULL)
+        {
+            FREE(pBwork);
+            return -1;
+        }
 
-        if (pAlphaR == NULL || pAlphaI == NULL || pBeta == NULL || pRwork == NULL)
+        if ((pAlphaI = (double*)MALLOC(iCols * sizeof(double))) == NULL)
         {
+            FREE(pAlphaR);
+            FREE(pBwork);
+            return -1;
+        }
+
+        if ((pBeta = (double*)MALLOC(iCols * sizeof(double))) == NULL)
+        {
+            FREE(pAlphaI);
+            FREE(pAlphaR);
+            FREE(pBwork);
+            return -1;
+        }
+
+        if ((pRwork = allocDggesWorkspace(iCols, iDim, &iWorksize)) == NULL)
+        {
+            FREE(pBeta);
+            FREE(pAlphaI);
+            FREE(pAlphaR);
+            FREE(pBwork);
             return -1;
         }
 
@@ -250,13 +302,36 @@ int schurSelect(types::Double** _pDblIn, types::Double** _pDblOut, bool _bIsComp
     else if (_pDblIn[1] && _bIsComplex)
     {
         //zgges
-        doublecomplex* pAlpha   = (doublecomplex*)MALLOC(iCols * sizeof(doublecomplex));
-        doublecomplex* pBeta    = (doublecomplex*)MALLOC(iCols * sizeof(doublecomplex));
-        pRwork                  = (double*) MALLOC(8 * iCols * sizeof(double));
-        pCplxWork = allocZggesWorkspace(iCols, iDim, &iWorksize);
+        doublecomplex* pAlpha = NULL;
+        doublecomplex* pBeta = NULL;
+
+        if ((pAlpha = (doublecomplex*)MALLOC(iCols * sizeof(doublecomplex))) == NULL)
+        {
+            FREE(pBwork);
+            return -1;
+        }
+
+        if ((pBeta = (doublecomplex*)MALLOC(iCols * sizeof(doublecomplex))) == NULL)
+        {
+            FREE(pAlpha);
+            FREE(pBwork);
+            return -1;
+        }
+
+        if ((pRwork = (double*) MALLOC(8 * iCols * sizeof(double))) == NULL)
+        {
+            FREE(pBeta);
+            FREE(pAlpha);
+            FREE(pBwork);
+            return -1;
+        }
 
-        if (pRwork == NULL || pAlpha == NULL || pBeta == NULL || pCplxWork == NULL)
+        if ((pCplxWork = allocZggesWorkspace(iCols, iDim, &iWorksize)) == NULL)
         {
+            FREE(pRwork);
+            FREE(pBeta);
+            FREE(pAlpha);
+            FREE(pBwork);
             return -1;
         }