avoid segfault on command line errors

This commit is contained in:
Axel Kohlmeyer
2025-01-26 21:45:12 -05:00
parent 563b5b0997
commit 6bb022853e
2 changed files with 13 additions and 4 deletions

View File

@ -119,13 +119,18 @@ void Error::all(const std::string &file, int line, int failed, const std::string
{ {
MPI_Barrier(world); MPI_Barrier(world);
// must get rank from communicator since "comm" instance may not yet exist
int me = 0;
MPI_Comm_rank(world, &me);
std::string lastcmd = "(unknown)"; std::string lastcmd = "(unknown)";
std::string mesg = "ERROR: " + str + fmt::format(" ({}:{})\n", truncpath(file), line); std::string mesg = "ERROR: " + str + fmt::format(" ({}:{})\n", truncpath(file), line);
// add text about the input following the error message // add text about the input following the error message
if (failed > NOLASTLINE) mesg += utils::point_to_error(input, failed); if (failed > NOLASTLINE) mesg += utils::point_to_error(input, failed);
if (comm->me == 0) utils::logmesg(lmp,mesg); if (me == 0) utils::logmesg(lmp,mesg);
utils::flush_buffers(lmp); utils::flush_buffers(lmp);
// allow commands if an exception was caught in a run // allow commands if an exception was caught in a run
@ -150,8 +155,12 @@ void Error::one(const std::string &file, int line, int failed, const std::string
{ {
std::string lastcmd = "(unknown)"; std::string lastcmd = "(unknown)";
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n", comm->me, str, // must get rank from communicator since "comm" instance may not yet exist
truncpath(file), line);
int me = 0;
MPI_Comm_rank(world, &me);
std::string mesg = fmt::format("ERROR on proc {}: {} ({}:{})\n", me, str, truncpath(file), line);
if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed); if (failed > NOPOINTER) mesg += utils::point_to_error(input, failed);
utils::logmesg(lmp,mesg); utils::logmesg(lmp,mesg);

View File

@ -562,7 +562,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
else if (strcmp(arg[inflag], "none") == 0) infile = stdin; else if (strcmp(arg[inflag], "none") == 0) infile = stdin;
else infile = fopen(arg[inflag],"r"); else infile = fopen(arg[inflag],"r");
if (infile == nullptr) if (infile == nullptr)
error->one(FLERR,"Cannot open input script {}: {}", arg[inflag], utils::getsyserror()); error->all(FLERR,"Cannot open input script {}: {}", arg[inflag], utils::getsyserror());
if (!helpflag) if (!helpflag)
utils::logmesg(this,"LAMMPS ({}{})\n", version, update_string); utils::logmesg(this,"LAMMPS ({}{})\n", version, update_string);