From 6251dc1b711fe9c322fae43cf864f07ad3ffade7 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Wed, 7 Feb 2024 09:35:50 +0100 Subject: [PATCH] Protect from getenv("HOME") returning nullptr (#907) * Protect from getenv("HOME") returning nullptr (e.g., in case running in systemd) * Write proper warning in Module.cpp --- slsDetectorSoftware/src/Module.cpp | 8 +++++++- slsSupportLib/include/sls/string_utils.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 33603c6ea..075d6f739 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -3332,7 +3332,13 @@ void Module::initializeModuleStructure(detectorType type) { shm()->numberOfModule.y = 0; shm()->controlPort = DEFAULT_TCP_CNTRL_PORTNO; shm()->stopPort = DEFAULT_TCP_STOP_PORTNO; - strcpy_safe(shm()->settingsDir, getenv("HOME")); + char *home_directory = getenv("HOME"); + if (home_directory != nullptr) + strcpy_safe(shm()->settingsDir, home_directory); + else { + strcpy_safe(shm()->settingsDir, ""); + LOG(logWARNING) << "HOME directory not set"; + } strcpy_safe(shm()->rxHostname, "none"); shm()->rxTCPPort = DEFAULT_TCP_RX_PORTNO + moduleIndex; shm()->useReceiverFlag = false; diff --git a/slsSupportLib/include/sls/string_utils.h b/slsSupportLib/include/sls/string_utils.h index eec1a0c08..86a5d0caf 100644 --- a/slsSupportLib/include/sls/string_utils.h +++ b/slsSupportLib/include/sls/string_utils.h @@ -20,6 +20,7 @@ Still this is better than strcpy and a buffer overflow... */ template void strcpy_safe(char (&destination)[array_size], const char *source) { + assert(source != nullptr); assert(array_size > strlen(source)); strncpy(destination, source, array_size - 1); destination[array_size - 1] = '\0';