Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jan 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 17 additions & 10 deletions lib/cli/consolecommand.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -369,9 +374,11 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
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;
Expand Down