make Input::file(const char *) function safe for passing a null pointer

This commit is contained in:
Axel Kohlmeyer
2023-03-07 13:44:34 -05:00
parent ce1956e60b
commit 4ed49d2f21
2 changed files with 13 additions and 13 deletions

View File

@ -327,15 +327,14 @@ void Input::file(const char *filename)
// call to file() will close filename and decrement nfile
if (me == 0) {
if (nfile == maxfile)
error->one(FLERR,"Too many nested levels of input scripts");
if (nfile == maxfile) error->one(FLERR,"Too many nested levels of input scripts");
infile = fopen(filename,"r");
if (infile == nullptr)
error->one(FLERR,"Cannot open input script {}: {}",
filename, utils::getsyserror());
infiles[nfile++] = infile;
if (filename) {
infile = fopen(filename,"r");
if (infile == nullptr)
error->one(FLERR,"Cannot open input script {}: {}", filename, utils::getsyserror());
infiles[nfile++] = infile;
}
}
// process contents of file
@ -343,9 +342,11 @@ void Input::file(const char *filename)
file();
if (me == 0) {
fclose(infile);
nfile--;
infile = infiles[nfile-1];
if (filename) {
fclose(infile);
nfile--;
infile = infiles[nfile-1];
}
}
}

View File

@ -532,8 +532,7 @@ void lammps_file(void *handle, const char *filename)
BEGIN_CAPTURE
{
if (lmp->update->whichflag != 0)
lmp->error->all(FLERR,"Library error: issuing LAMMPS commands "
"during a run is not allowed.");
lmp->error->all(FLERR, "Library error: issuing LAMMPS commands during a run is not allowed");
else
lmp->input->file(filename);
}