ide: Fixed corruption of LD_LIBRARY_PATH (Only PATH was being restored) with PathBack...
authorJerome St-Louis <jerome@ecere.com>
Sun, 5 Feb 2012 13:19:53 +0000 (20:19 +0700)
committerJerome St-Louis <jerome@ecere.com>
Sun, 5 Feb 2012 13:19:53 +0000 (20:19 +0700)
This corrupt LD_LIBRARY_PATH was a huge mess when running apps with MemoryGuard, it even hard-locked my machine!

ide/src/debugger/Debugger.ec
ide/src/ide.ec
ide/src/project/Project.ec
ide/src/project/ProjectView.ec
ide/src/project/Workspace.ec

index b0c5483..0f9b87b 100644 (file)
@@ -1809,11 +1809,9 @@ class Debugger
       char oldDirectory[MAX_LOCATION];
       char tempPath[MAX_LOCATION];
       char command[MAX_LOCATION];
-      char oldPath[MAX_LOCATION * 65];
       Project project = ide.project;
       DirExpression targetDirExp = project.targetDir;
-
-      GetEnvironment("PATH", oldPath, sizeof(oldPath));
+      PathBackup pathBackup { };
 
       prjConfig = project.config;
 
@@ -1948,7 +1946,8 @@ class Debugger
       }
 
       ChangeWorkingDir(oldDirectory);
-      SetEnvironment("PATH", oldPath);
+
+      delete pathBackup;
 
       if(!result)
          GdbExit();
index 7281a2e..227da29 100644 (file)
@@ -52,6 +52,39 @@ define pathListSep = ";";
 define pathListSep = ":";
 #endif
 
+define maxPathLen = 65 * MAX_LOCATION;
+
+class PathBackup : struct
+{
+   String oldLDPath;
+   String oldPath;
+
+   PathBackup()
+   {
+      oldPath = new char[maxPathLen];
+      oldLDPath = new char[maxPathLen];
+
+      GetEnvironment("PATH", oldPath, maxPathLen);
+#if defined(__APPLE__)
+      GetEnvironment("DYLD_LIBRARY_PATH", oldLDPath, maxPathLen);
+#else
+      GetEnvironment("LD_LIBRARY_PATH", oldLDPath, maxPathLen);
+#endif
+   }
+
+   ~PathBackup()
+   {
+      SetEnvironment("PATH", oldPath);
+#if defined(__APPLE__)
+      SetEnvironment("DYLD_LIBRARY_PATH", oldLDPath);
+#else
+      SetEnvironment("LD_LIBRARY_PATH", oldLDPath);
+#endif
+      delete oldPath;
+      delete oldLDPath;
+   }
+};
+
 enum OpenCreateIfFails { no, yes, something, whatever };
 enum OpenMethod { normal, add };
 
@@ -2185,7 +2218,7 @@ class IDE : Window
       int c, len, count;
       char * newList;
       char * oldPaths[128];
-      char oldList[MAX_LOCATION * 128];
+      String oldList = new char[maxPathLen];
       Array<String> newExePaths { };
       //Map<String, bool> exePathExists { };
       bool found = false;
@@ -2260,7 +2293,7 @@ class IDE : Window
             newExePaths.Add(CopySystemPath(item));
       }
 
-      GetEnvironment("PATH", oldList, sizeof(oldList));
+      GetEnvironment("PATH", oldList, maxPathLen);
 /*#ifdef _DEBUG
       printf("Old value of PATH: %s\n", oldList);
 #endif*/
@@ -2312,9 +2345,9 @@ class IDE : Window
       }
 
 #if defined(__APPLE__)
-      GetEnvironment("DYLD_LIBRARY_PATH", oldList, sizeof(oldList));
+      GetEnvironment("DYLD_LIBRARY_PATH", oldList, maxPathLen);
 #else
-      GetEnvironment("LD_LIBRARY_PATH", oldList, sizeof(oldList));
+      GetEnvironment("LD_LIBRARY_PATH", oldList, maxPathLen);
 #endif
 /*#ifdef _DEBUG
       printf("Old value of [DY]LD_LIBRARY_PATH: %s\n", oldList);
@@ -2358,6 +2391,7 @@ class IDE : Window
          SetEnvironment("DISTCC_HOSTS", compiler.distccHosts);
 
       delete compiler;
+      delete oldList;
    }
 
    void DestroyTemporaryProjectDir()
index f9994ed..f6021f9 100644 (file)
@@ -1447,16 +1447,14 @@ private:
       char makeTarget[MAX_LOCATION] = "";
       char makeFile[MAX_LOCATION];
       char makeFilePath[MAX_LOCATION];
-      char oldPath[MAX_LOCATION * 65];
       char configName[MAX_LOCATION];
       CompilerConfig compiler = GetCompilerConfig();
       DirExpression objDirExp = objDir;
+      PathBackup pathBackup { };
 
       int numJobs = compiler.numJobs;
       char command[MAX_LOCATION];
 
-      GetEnvironment("PATH", oldPath, sizeof(oldPath));
-
       strcpy(configName, this.configName);
       
       SetPath(false); //true
@@ -1527,7 +1525,7 @@ private:
             ide.outputView.buildBox.Logf("Error executing make (%s) command\n", compiler.makeCommand);
       }
 
-      SetEnvironment("PATH", oldPath);
+      delete pathBackup;
 
       delete objDirExp;
       delete compiler;
@@ -1536,14 +1534,12 @@ private:
 
    void Clean()
    {
-      char oldPath[MAX_LOCATION * 65];
       char makeFile[MAX_LOCATION];
       char makeFilePath[MAX_LOCATION];
       char command[MAX_LOCATION];
       DualPipe f;
       CompilerConfig compiler = GetCompilerConfig();
-
-      GetEnvironment("PATH", oldPath, sizeof(oldPath));
+      PathBackup pathBackup { };
 
       SetPath(false);
 
@@ -1582,18 +1578,17 @@ private:
          }
       }
 
-      SetEnvironment("PATH", oldPath);
+      delete pathBackup;
       delete compiler;
    }
 
    void Run(char * args)
    {   
-      char target[MAX_LOCATION * 65], oldDirectory[MAX_LOCATION];
-      char oldPath[MAX_LOCATION * 65];
+      String target = new char[maxPathLen];
+      char oldDirectory[MAX_LOCATION];
       DirExpression targetDirExp = targetDir;
       CompilerConfig compiler = GetCompilerConfig();
-
-      GetEnvironment("PATH", oldPath, sizeof(oldPath));
+      PathBackup pathBackup { };
 
       // Build(project, ideMain, true, null);
 
@@ -1632,10 +1627,11 @@ private:
          Execute(target);
 
       ChangeWorkingDir(oldDirectory);
-      SetEnvironment("PATH", oldPath);
+      delete pathBackup;
 
       delete targetDirExp;
       delete compiler;
+      delete target;
    }
 
    void Compile(ProjectNode node)
@@ -1672,8 +1668,8 @@ private:
       char filePath[MAX_LOCATION];
       char makeFile[MAX_LOCATION];
       CompilerConfig compiler = GetCompilerConfig();
-      /*char oldPath[MAX_LOCATION * 65];
-      char oldDirectory[MAX_LOCATION];*/
+      // PathBackup pathBackup { };
+      // char oldDirectory[MAX_LOCATION];
       File f = null;
 
       if(!altMakefilePath)
@@ -1693,8 +1689,7 @@ private:
 #endif
       f = FileOpen(altMakefilePath ? altMakefilePath : filePath, write);
 
-      /*GetEnvironment("PATH", oldPath, sizeof(oldPath));
-      SetPath(false);
+      /*SetPath(false);
       GetWorkingDir(oldDirectory, MAX_LOCATION);
       ChangeWorkingDir(topNode.path);*/
 
@@ -2338,8 +2333,8 @@ private:
          result = true;
       }
 
-      /*ChangeWorkingDir(oldDirectory);
-      SetEnvironment("PATH", oldPath);*/
+      /ChangeWorkingDir(oldDirectory);
+      // delete pathBackup;
 
       if(config)
          config.makingModified = false;
index 7a1df6d..f91efe1 100644 (file)
@@ -1401,7 +1401,7 @@ class ProjectView : Window
 
    bool Run(MenuItem selection, Modifiers mods)
    {
-      char args[MAX_LOCATION * 64];
+      String args = new char[maxPathLen];
       args[0] = '\0';
       if(ide.workspace.commandLineArgs)
          //ide.debugger.GetCommandLineArgs(args);
@@ -1412,6 +1412,7 @@ class ProjectView : Window
          MessageBox { master = ide, type = ok, text = "Run", contents = "Shared and static libraries cannot be run like executables." }.Modal();*/
       else if(BuildInterrim(project, run))
          project.Run(args);
+      delete args;
       return true;
    }
 
@@ -1432,10 +1433,9 @@ class ProjectView : Window
             {
                //bool result = false;
                char oldwd[MAX_LOCATION];
-               char oldPath[MAX_LOCATION * 65];
+               PathBackup pathBackup { };
                char command[MAX_LOCATION];
 
-               GetEnvironment("PATH", oldPath, sizeof(oldPath));
                ide.SetPath(false); //true
                
                GetWorkingDir(oldwd, sizeof(oldwd));
@@ -1445,7 +1445,8 @@ class ProjectView : Window
                //ide.outputView.buildBox.Logf("command: %s\n", command);
                Execute(command);
                ChangeWorkingDir(oldwd);
-               SetEnvironment("PATH", oldPath);
+
+               delete pathBackup;
             }
             else
             {
index 3f9fd32..a4c794f 100644 (file)
@@ -1108,11 +1108,11 @@ Workspace LoadWorkspace(char * filePath, char * fromProjectFile)
          {
             int c = 0;
             char s[2] = "";
-            char files[MAX_LOCATION * 16] = "\n";
+            String files = new char[MAX_LOCATION * 16];
             char title[512];
-            char msg[MAX_LOCATION * 16 + 2048];
-
+            String msg = new char[MAX_LOCATION * 16 + 2048];
             NamedItem item;
+            strcpy(files,"\n");
 
             item = openedFilesNotFound.first;
             if(item.next)
@@ -1134,6 +1134,9 @@ Workspace LoadWorkspace(char * filePath, char * fromProjectFile)
             sprintf(msg, "The following file%s could not be re-opened.%s", s, files);
             
             MessageBox { type = ok, master = ide, contents = msg, text = title }.Modal();
+
+            delete files;
+            delete msg;
          }
          openedFilesNotFound.Free(OldLink::Free);
       }