From 8e79f51663d2a7f1f06fe1a57c37eb729903b691 Mon Sep 17 00:00:00 2001 From: Ronald Rahaman Date: Fri, 11 Sep 2020 00:56:53 -0500 Subject: [PATCH] Check null pointer for getenv (#147) In `configRead`, a string was assigned from the return value of `getenv`: ``` string install_dir; install_dir.assign(getenv("NEKRS_HOME")); ``` However, if `NEKRS_HOME` isn't defined, `getenv` will return a null pointer, and `string::assign` will segfault. This update will avoid that issue with an error message. --- src/core/configReader.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/configReader.cpp b/src/core/configReader.cpp index ea33b493b..b2a1447d1 100644 --- a/src/core/configReader.cpp +++ b/src/core/configReader.cpp @@ -20,9 +20,15 @@ void configRead(MPI_Comm comm) { int rank; MPI_Comm_rank(comm, &rank); - - string install_dir; - install_dir.assign(getenv("NEKRS_HOME")); + + char* nekrs_home = getenv("NEKRS_HOME"); + if (nekrs_home == nullptr) { + if (rank == 0) { + cout << "\nERROR: The environment variable NEKRS_HOME could not be found!\n"; + } + ABORT(1); + } + string install_dir{nekrs_home}; string configFile = install_dir + "/nekrs.conf"; const char* ptr = realpath(configFile.c_str(), NULL);