[mgetl] Conditional jump or move depends on uninitialised value(s) fixed 04/19504/3
Cedric Delamarre [Tue, 24 Oct 2017 09:17:25 +0000 (11:17 +0200)]
  test_run fileio mgetl
  test_run fileio mgetl mode_nwni_profiling

Change-Id: I2aad520122102464ecb2a60d71deaabf48111b64

scilab/modules/ast/src/cpp/system_env/sci_home.cpp
scilab/modules/fileio/src/cpp/mgetl.cpp

index b888914..19830ee 100644 (file)
@@ -319,6 +319,7 @@ void defineSCIHOME()
     wchar_t* sci_home = getSCIHOMEW();
     if (wcscmp(sci_home, L"") == 0)
     {
+        FREE(sci_home);
         sci_home = computeSCIHOMEW();
     }
     else
@@ -327,6 +328,7 @@ void defineSCIHOME()
         {
             sciprint("Unable to create SCIHOME in `%ls`.\n", sci_home);
             sciprint("Back to normal behaviour.\n");
+            FREE(sci_home);
             sci_home = computeSCIHOMEW();
         }
     }
index 334ee4e..3b863da 100644 (file)
@@ -92,117 +92,106 @@ int mgetl(int iFileID, int iLineCount, wchar_t ***pwstLines)
     ifs.seekg(orig);
 
     std::list<std::string> lst;
-    std::string str;
 
-    if (1)
+    bool lineReach = false;
+    std::string previous;
+    size_t offset = 0;
+    while (lst.size() < iLineCount && ifs.eof() == false)
     {
-        bool lineReach = false;
-        std::string previous;
-        size_t offset = 0;
-        while (lst.size() < iLineCount && ifs.eof() == false)
-        {
-            int delimiter_size = 1;
-            size_t sp = previous.size();
+        int delimiter_size = 1;
+        size_t sp = previous.size();
 #define MAX_READ_LEN 262144
-            char buf[MAX_READ_LEN + 1] = {0};
-            ifs.read(buf, MAX_READ_LEN);
-            size_t s = strlen(buf);
-            //extract lines
-            char* ptr = buf;
-            for (int i = 0; i < s; ++i)
+        char buf[MAX_READ_LEN + 1] = {0};
+        ifs.read(buf, MAX_READ_LEN);
+        size_t s = strlen(buf);
+        //extract lines
+        char* ptr = buf;
+        for (int i = 0; i < s; ++i)
+        {
+            if (buf[i] == '\n')
             {
-                if (buf[i] == '\n')
+                //delimit line
+                buf[i] = '\0';
+                if(i > 0 && buf[i - 1] == '\r')
                 {
-                    //delimit line
-                    buf[i] = '\0';
-                    if(buf[i - 1] == '\r')
-                    {
-                        buf[i - 1] = '\0';
-                        delimiter_size = 2;
-                    }
+                    buf[i - 1] = '\0';
+                    delimiter_size = 2;
+                }
 
-                    //add line to list
-                    if (sp)
-                    {
-                        previous += ptr;
-                        lst.push_back(previous);
+                //add line to list
+                if (sp)
+                {
+                    previous += ptr;
+                    lst.push_back(previous);
 #ifdef _MSC_VER
-                        offset += previous.size() + 2;
+                    offset += previous.size() + 2;
 #else
-                        offset += previous.size() + delimiter_size;
+                    offset += previous.size() + delimiter_size;
 #endif
-                        previous.clear();
-                    }
-                    else
-                    {
-                        lst.emplace_back(ptr);
+                    previous.clear();
+                }
+                else
+                {
+                    lst.emplace_back(ptr);
 #ifdef _MSC_VER
-                        offset += strlen(ptr) + 2;
+                    offset += strlen(ptr) + 2;
 #else
-                        offset += strlen(ptr) + delimiter_size;
+                    offset += strlen(ptr) + delimiter_size;
 #endif
-                    }
+                }
 
-                    //move ptr to first next line char
-                    ptr = buf + i + 1;
+                //move ptr to first next line char
+                ptr = buf + i + 1;
 
-                    if (iLineCount != -1 && lst.size() >= iLineCount)
-                    {
-                        //rewind
+                if (iLineCount != -1 && lst.size() >= iLineCount)
+                {
+                    //rewind
 #ifndef _MSC_VER
-                        auto t = ifs.tellg();
+                    auto t = ifs.tellg();
 #else
-                        std::fpos_t t = ifs.tellg().seekpos();
+                    std::fpos_t t = ifs.tellg().seekpos();
 #endif
-                        if (t <= 0)
-                        {
-                            ifs.clear();
-                        }
-
-                        ifs.seekg(orig + offset, std::ios::beg);
-                        lineReach = true;
-                        break;
+                    if (t <= 0)
+                    {
+                        ifs.clear();
                     }
+
+                    ifs.seekg(orig + offset, std::ios::beg);
+                    lineReach = true;
+                    break;
                 }
             }
+        }
 
-            if (ptr == buf)
-            {
-                //long line
-                previous += buf;
-            }
-            else if (lineReach == false)
+        if (ptr == buf)
+        {
+            //long line
+            previous += buf;
+        }
+        else if (lineReach == false)
+        {
+            int offset = (int)(buf + s - ptr);
+            if (offset)
             {
-                int offset = (int)(buf + s - ptr);
-                if (offset)
+                if (!ifs.eof())
                 {
-                    if (!ifs.eof())
-                    {
-                        //some data stay in buf, rewind file to begin of this data and read it again
-                        ifs.seekg(-offset, std::ios::cur);
-                    }
-                    else
-                    {
-                        //some data stay in buf but oef is reached, add ptr data in list
-                        std::string str(ptr);
-                        lst.push_back(str);
-                    }
+                    //some data stay in buf, rewind file to begin of this data and read it again
+                    ifs.seekg(-offset, std::ios::cur);
+                }
+                else
+                {
+                    //some data stay in buf but oef is reached, add ptr data in list
+                    std::string str(ptr);
+                    lst.push_back(str);
                 }
             }
         }
-
-        if (previous.size())
-        {
-            lst.push_back(previous);
-            previous.clear();
-        }
     }
-    else
+
+    if (previous.size())
     {
-        while (lst.size() < iLineCount && std::getline(ifs, str))
-        {
-            lst.push_back(str);
-        }
+        lst.push_back(previous);
+        previous.clear();
     }
 
     int nbLinesOut = (int)lst.size();