]> granicus.if.org Git - icinga2/commitdiff
Fix crash when running 'icinga2 console' without HOME environment variable 6020/head
authorGunnar Beutner <gunnar.beutner@icinga.com>
Sat, 20 Jan 2018 06:46:19 +0000 (07:46 +0100)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Sat, 20 Jan 2018 06:46:19 +0000 (07:46 +0100)
fixes #6019

lib/cli/consolecommand.cpp

index c102452dce8d02c8a707f0c71111f28b5698688b..7d9ac27199d4005ec130b61017eab415bbac3427 100644 (file)
@@ -286,17 +286,22 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
        int next_line = 1;
 
 #ifdef HAVE_EDITLINE
-       String homeEnv = getenv("HOME");
-       String historyPath = homeEnv + "/.icinga2_history";
+       char *homeEnv = getenv("HOME");
 
+       String historyPath;
        std::fstream historyfp;
-       historyfp.open(historyPath.CStr(), std::fstream::in);
 
-       String line;
-       while (std::getline(historyfp, line.GetData()))
-               add_history(line.CStr());
+       if (homeEnv) {
+               historyPath = String(homeEnv) + "/.icinga2_history";
 
-       historyfp.close();
+               historyfp.open(historyPath.CStr(), std::fstream::in);
+
+               String line;
+               while (std::getline(historyfp, line.GetData()))
+                       add_history(line.CStr());
+
+               historyfp.close();
+       }
 #endif /* HAVE_EDITLINE */
 
        l_ScriptFrame = &scriptFrame;
@@ -369,9 +374,11 @@ incomplete:
                        if (commandOnce.IsEmpty() && cline[0] != '\0') {
                                add_history(cline);
 
-                               historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app);
-                               historyfp << cline << "\n";
-                               historyfp.close();
+                               if (!historyPath.IsEmpty()) {
+                                       historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app);
+                                       historyfp << cline << "\n";
+                                       historyfp.close();
+                               }
                        }
 
                        line = cline;