[polynomials] memleak and invalid read/write 76/17176/2
Cedric Delamarre [Tue, 8 Sep 2015 15:39:12 +0000 (17:39 +0200)]
Change-Id: Ic62a312cc7c38d504da5f8390ed0502b5d8e2927

scilab/modules/elementary_functions/src/cpp/prod.cpp
scilab/modules/fftw/src/c/fftw_utilities.c
scilab/modules/fftw/src/cpp/fftw_common.cpp
scilab/modules/polynomials/sci_gateway/cpp/sci_roots.cpp
scilab/modules/polynomials/sci_gateway/cpp/sci_sfact.cpp

index 2a7c4b3..6d793a2 100644 (file)
@@ -156,27 +156,21 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
         pOut = new types::Polynom(pIn->getVariableName(), 1, 1, &iRankMax);
         pOut->setComplex(pIn->isComplex());
 
+        pOut->set(0, pIn->get(0));
+
         // do prod
+        int iSize = pIn->getSize();
         double* pdblRealOut = pOut->get(0)->get();
+        double* pdblTempReal = new double[iRankMax + 1];
+
         if (pIn->isComplex())
         {
             double* pdblImgOut = pOut->get(0)->getImg();
-
             // alloc temporary workspace
-            double* pdblTempReal = new double[iRankMax + 1];
             double* pdblTempImg  = new double[iRankMax + 1];
 
-            // init output to 1 + 0s + 0s²...
-            pdblRealOut[0] = 1;
-            pdblImgOut[0]  = 0;
-            for (int iRankN = 1; iRankN < iRankMax + 1; iRankN++)
-            {
-                pdblRealOut[iRankN] = 0;
-                pdblImgOut[iRankN]  = 0;
-            }
-
             // perform operations
-            for (int i = 0; i < pIn->getSize(); i++)
+            for (int i = 1; i < iSize; i++)
             {
                 double* pdblRealIn = pIn->get(i)->get();
                 double* pdblImgIn = pIn->get(i)->getImg();
@@ -187,23 +181,12 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
                                                pdblRealOut, pdblImgOut, iRankMax + 1);
             }
 
-            delete pdblTempReal;
             delete pdblTempImg;
         }
         else
         {
-            // alloc temporary workspace
-            double* pdblTempReal = new double[iRankMax + 1];
-
-            // init output to 1 + 0s + 0s²...
-            pdblRealOut[0] = 1;
-            for (int iRankN = 1; iRankN < iRankMax + 1; iRankN++)
-            {
-                pdblRealOut[iRankN] = 0;
-            }
-
             // perform operations
-            for (int i = 0; i < pIn->getSize(); i++)
+            for (int i = 1; i < iSize; i++)
             {
                 double* pdblRealIn = pIn->get(i)->get();
                 memcpy(pdblTempReal, pdblRealOut, (iRankMax + 1) * sizeof(double));
@@ -211,11 +194,10 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
                                                    pdblRealIn, piRanks[i] + 1,
                                                    pdblRealOut, iRankMax + 1);
             }
-
-            delete pdblTempReal;
         }
 
-        delete[]piRanks;
+        delete[] pdblTempReal;
+        delete[] piRanks;
     }
     else // sum following a dimension
     {
@@ -278,26 +260,21 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
         pOut = new types::Polynom(pIn->getVariableName(), iDims, piDims, piRankMax);
         pOut->setComplex(pIn->isComplex());
 
+        // init output with first element of lead dimension
+        for (int i = 0; i < pOut->getSize(); i++)
+        {
+            pOut->getIndexes(i, piIndex);
+            int iIndex = pIn->getIndex(piIndex);
+            pOut->set(i, pIn->get(iIndex));
+        }
+
+        // alloc temporary workspace
+        double* pdblTempReal = new double[iMaxOutputRank + 1];
         if (pIn->isComplex())
         {
             // alloc temporary workspace
-            double* pdblTempReal = new double[iMaxOutputRank + 1];
             double* pdblTempImg  = new double[iMaxOutputRank + 1];
 
-            // init output to a matrix of 1 + 0s + 0s²...
-            for (int i = 0; i < pOut->getSize(); i++)
-            {
-                double* pdblRealOut = pOut->get(i)->get();
-                double* pdblImgOut = pOut->get(i)->getImg();
-                pdblRealOut[0] = 1;
-                pdblImgOut[0]  = 0;
-                for (int iRankN = 1; iRankN < piRankMax[i] + 1; iRankN++)
-                {
-                    pdblRealOut[iRankN] = 0;
-                    pdblImgOut[iRankN]  = 0;
-                }
-            }
-
             // perform operations
             for (int i = 0 ; i < pIn->getSize() ; i++)
             {
@@ -305,6 +282,12 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
                 pIn->getIndexes(i, piIndex);
 
                 //convert indexes for result
+                if (piIndex[iOrientation - 1] == 0)
+                {
+                    // first element of lead dimension is already setted.
+                    continue;
+                }
+
                 piIndex[iOrientation - 1] = 0;
                 int iIndex = pOut->getIndex(piIndex);
 
@@ -320,25 +303,10 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
                                                pdblRealOut, pdblImgOut, piRankMax[iIndex] + 1);
             }
 
-            delete pdblTempReal;
-            delete pdblTempImg;
+            delete[] pdblTempImg;
         }
         else
         {
-            // alloc temporary workspace
-            double* pdblTempReal = new double[iMaxOutputRank + 1];
-
-            // init output to a matrix of 1 + 0s + 0s²...
-            for (int i = 0; i < pOut->getSize(); i++)
-            {
-                double* pdblRealOut = pOut->get(i)->get();
-                pdblRealOut[0] = 1;
-                for (int iRankN = 1; iRankN < piRankMax[i] + 1; iRankN++)
-                {
-                    pdblRealOut[iRankN] = 0;
-                }
-            }
-
             // perform operations
             for (int i = 0 ; i < pIn->getSize() ; i++)
             {
@@ -346,6 +314,12 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
                 pIn->getIndexes(i, piIndex);
 
                 //convert indexes for result
+                if (piIndex[iOrientation - 1] == 0)
+                {
+                    // first element of lead dimension is already setted.
+                    continue;
+                }
+
                 piIndex[iOrientation - 1] = 0;
                 int iIndex = pOut->getIndex(piIndex);
 
@@ -357,10 +331,9 @@ types::Polynom* prod(types::Polynom* pIn, int iOrientation)
                                                    pdblRealIn, piRanks[i] + 1,
                                                    pdblRealOut, piRankMax[iIndex] + 1);
             }
-
-            delete pdblTempReal;
         }
 
+        delete[] pdblTempReal;
         delete[] piRankMax;
         delete[] piRanks;
         delete[] piIndex;
index 0d869a8..15921b1 100644 (file)
@@ -1331,7 +1331,10 @@ int complete_array(double *Ar, double *Ai, guru_dim_struct gdim)
 
 int withMKL(void)
 {
-    return (call_fftw_export_wisdom_to_string() == NULL);
+    char* str = call_fftw_export_wisdom_to_string();
+    int iWithMKL = (int)(str == NULL);
+    FREE(str);
+    return iWithMKL;
 }
 /*--------------------------------------------------------------------------*/
 
index 1038a4e..c8cbf01 100644 (file)
@@ -281,7 +281,8 @@ types::Function::ReturnValue common_2args(std::string& name, types::typed_list &
         FREE(gdim.howmany_dims);
         return types::Function::OK;
     }
-
+    FREE(gdim.dims);
+    FREE(gdim.howmany_dims);
     out.push_back(D);
     return types::Function::OK;
 }
index a4b4b72..d8298c3 100644 (file)
@@ -248,6 +248,7 @@ types::Function::ReturnValue sci_roots(types::typed_list &in, int _iRetCount, ty
             pdblOutImg = pDblOut->getImg();
             memset(pdblOutImg, 0x00, pDblOut->getSize() * sizeof(double));
             C2F(dcopy)(&iSize, pdblTempImg, &iOne, pdblOutImg, &iOne);
+            delete[] pdblTempImg;
         }
 
         //call spec
index e8dbd34..c084341 100644 (file)
@@ -154,11 +154,13 @@ types::Function::ReturnValue sci_sfact(types::typed_list &in, int _iRetCount, ty
 
         if (iErr < 0)
         {
+            delete[] pdblOut;
             Scierror(999, _("%s: Wrong value for input argument #%d: Convergence problem.\n"), "sfact", 1);
             return types::Function::Error;
         }
         else if (iErr > 0)
         {
+            delete[] pdblOut;
             Scierror(999, _("%s: Wrong value for input argument #%d: singular or asymmetric problem.\n"), "sfact", 1);
             return types::Function::Error;
         }
@@ -172,6 +174,8 @@ types::Function::ReturnValue sci_sfact(types::typed_list &in, int _iRetCount, ty
             pPolyOut->set(i, pSP);
             delete pSP;
         }
+
+        delete[] pdblOut;
     }
 
     out.push_back(pPolyOut);