[optimization] crash and leak fixed in readmps 68/18868/4
Cedric Delamarre [Wed, 11 Jan 2017 15:55:42 +0000 (16:55 +0100)]
crash because of the parameter BUFFER declaration (windows only)

test_run optimization readmps
test_run optimization readmps mode_nwni_profiling

Change-Id: Iad826c9fd22388cc3d2f822cc6624f9a4671b40c

scilab/modules/optimization/sci_gateway/cpp/sci_readmps.cpp
scilab/modules/optimization/src/fortran/rdmps1.f

index 17e8308..c5e93cb 100644 (file)
@@ -186,6 +186,8 @@ types::Function::ReturnValue sci_readmps(types::typed_list &in, int _iRetCount,
     char* pstrClName = new char[8 * iN + 1];
     pstrClName[8 * iN] = '\0';
     char* strErrorBuf = new char[bsiz];
+    memset(strErrorBuf, ' ', bsiz-1);
+    strErrorBuf[bsiz - 1] = '\0';
 
     int irobj       = 0;
     int* piStavar   = new int[iN];
@@ -222,17 +224,34 @@ types::Function::ReturnValue sci_readmps(types::typed_list &in, int _iRetCount,
 
     if (ierr)
     {
-        int iLen = 4096;
-        char* str = strErrorBuf + 4095;
+        int iPos = bsiz - 1;
+        char* str = strErrorBuf + iPos;
         while (*str == ' ')
         {
-            iLen--;
+            iPos--;
             str--;
         }
 
-        strErrorBuf[iLen] = '\0';
+        iPos = iPos + 1 == bsiz ? iPos - 1 : iPos;
+        strErrorBuf[iPos + 1] = '\0';
         Scierror(999, "%s", strErrorBuf);
+
+        delete[] pstrRwName;
+        delete[] pstrClName;
         delete[] strErrorBuf;
+
+        delete[] piStavar;
+        delete[] piRwstat;
+        delete[] piRowcod;
+        delete[] piColcod;
+        delete[] piRwnmbs;
+        delete[] piClpnts;
+
+        delete pDblCoef;
+        delete pDblRhsb;
+        delete pDblRanges;
+        delete pDblBnds;
+
         return types::Function::Error;
     }
 
index 87f721b..b631872 100644 (file)
@@ -2,9 +2,9 @@
 C****************************************************
 C     ****  RDMPS1 ... READ THE  MPS FILE  ****
 C****************************************************
-      SUBROUTINE rdmps1(RCODE,buffer,MAXM,MAXN,MAXNZA,
+      SUBROUTINE rdmps1(RCODE,BUFFER,MAXM,MAXN,MAXNZA,
      X M,N,NZA,IROBJ,BIG,DLOBND,DUPBND,
-     X NAMEC,NAMEB,NAMRAN,NAMBND,NAMMPS,inmps,
+     X NAMEC,NAMEB,NAMRAN,NAMBND,NAMMPS,INMPS,
      X RWNAME,CLNAME,STAVAR,RWSTAT,
      X HDRWCD,LNKRW,HDCLCD,LNKCL,
      X RWNMBS,CLPNTS,IROW,
@@ -15,7 +15,7 @@ C *** PARAMETERS
       INTEGER*4 RCODE,MAXM,MAXN,MAXNZA,M,N,NZA,IROBJ
       DOUBLE PRECISION BIG,DLOBND,DUPBND
       CHARACTER*(*) NAMEC,NAMEB,NAMRAN,NAMBND,NAMMPS
-      CHARACTER*(*) BUFFER
+      CHARACTER*4096 BUFFER
       CHARACTER*8 RWNAME(MAXM),CLNAME(MAXN)
       INTEGER*4 STAVAR(*),RWSTAT(*),RWNMBS(*)
       INTEGER*4 HDRWCD(*),LNKRW(*)