Fix crash when running 'icinga2 console' without HOME environment variable

fixes #6019
This commit is contained in:
Gunnar Beutner 2018-01-20 07:46:19 +01:00
parent 3122e44087
commit 3e3658b7a5
1 changed files with 17 additions and 10 deletions

View File

@ -286,10 +286,14 @@ 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;
if (homeEnv) {
historyPath = String(homeEnv) + "/.icinga2_history";
historyfp.open(historyPath.CStr(), std::fstream::in);
String line;
@ -297,6 +301,7 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
add_history(line.CStr());
historyfp.close();
}
#endif /* HAVE_EDITLINE */
l_ScriptFrame = &scriptFrame;
@ -369,10 +374,12 @@ incomplete:
if (commandOnce.IsEmpty() && cline[0] != '\0') {
add_history(cline);
if (!historyPath.IsEmpty()) {
historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app);
historyfp << cline << "\n";
historyfp.close();
}
}
line = cline;