From 4ed49d2f21ffe6a65d022d46906ff9c82ec1ec7a Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 7 Mar 2023 13:44:34 -0500 Subject: [PATCH] make Input::file(const char *) function safe for passing a null pointer --- src/input.cpp | 23 ++++++++++++----------- src/library.cpp | 3 +-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index e653704ae3..ef0433dc99 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -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]; + } } } diff --git a/src/library.cpp b/src/library.cpp index 7fcc22ef17..9f817a1199 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -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); }