diff --git a/daemon/src/fileutils.cpp b/daemon/src/fileutils.cpp
index 35bea50e82bcbdd7fa46d36d13587d7522e3978c..31996710e7499629d69ac6833a4ba8ca796f2075 100644
--- a/daemon/src/fileutils.cpp
+++ b/daemon/src/fileutils.cpp
@@ -78,8 +78,8 @@ const char *get_data_dir()
 
 bool create_pidfile()
 {
-    const char * const xdg_env = XDG_CACHE_HOME;
-    std::string path = xdg_env ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
+    std::string xdg_env(XDG_CACHE_HOME);
+    std::string path = (not xdg_env.empty()) ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
 
     if (!check_dir(path.c_str()))
         return false;
diff --git a/daemon/src/fileutils.h b/daemon/src/fileutils.h
index ece20c45b5b420e6b72d966a5e7c35dfe7560d8e..bace1e5d957e782c581ebdde6a574fdcfff55d14 100644
--- a/daemon/src/fileutils.h
+++ b/daemon/src/fileutils.h
@@ -31,10 +31,14 @@
 #ifndef FILEUTILS_H_
 #define FILEUTILS_H_
 
-#define HOMEDIR					(getenv ("HOME"))				/** Home directory */
-#define XDG_DATA_HOME			(getenv ("XDG_DATA_HOME"))
-#define XDG_CONFIG_HOME			(getenv ("XDG_CONFIG_HOME"))
-#define XDG_CACHE_HOME			(getenv ("XDG_CACHE_HOME"))
+#define PROTECTED_GETENV(str) ({char *envvar_ = getenv((str)); \
+                                                   envvar_ ? envvar_ : "";})
+
+#define HOMEDIR                 (PROTECTED_GETENV("HOME"))
+#define XDG_DATA_HOME           (PROTECTED_GETENV("XDG_DATA_HOME"))
+#define XDG_CONFIG_HOME         (PROTECTED_GETENV("XDG_CONFIG_HOME"))
+#define XDG_CACHE_HOME          (PROTECTED_GETENV("XDG_CACHE_HOME"))
+
 #define PIDFILE "sfl.pid"
 
 
diff --git a/daemon/src/history/history.cpp b/daemon/src/history/history.cpp
index f32a08d7eb8b4431456eed67377f90c948b60965..15ca07847fb31e3678f2286951fed255b042d7c4 100644
--- a/daemon/src/history/history.cpp
+++ b/daemon/src/history/history.cpp
@@ -92,11 +92,8 @@ void History::ensurePath()
         string userdata;
         // If the environment variable is set (not null and not empty), we'll use it to save the history
         // Else we 'll the standard one, ie: XDG_DATA_HOME = $HOMEDIR/.local/share/sflphone
-        if (XDG_DATA_HOME != NULL) {
-            string xdg_env(XDG_DATA_HOME);
-            (!xdg_env.empty()) ? userdata = xdg_env : userdata = xdg_data;
-        } else
-            userdata = xdg_data;
+        string xdg_env(XDG_DATA_HOME);
+        (not xdg_env.empty()) ? userdata = xdg_env : userdata = xdg_data;
 
         if (mkdir(userdata.data(), 0755) != 0) {
             // If directory	creation failed
diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp
index 1d4e1652928a9763ed89e30dfabbdbd8a95cf42c..fbe401b709f2708077e1cbf88874c3b6241cc70a 100644
--- a/daemon/src/managerimpl.cpp
+++ b/daemon/src/managerimpl.cpp
@@ -1779,10 +1779,9 @@ std::string ManagerImpl::createConfigFile() const
     std::string configdir = std::string(HOMEDIR) + DIR_SEPARATOR_STR +
                             ".config" + DIR_SEPARATOR_STR + PACKAGE;
 
-    if (XDG_CONFIG_HOME != NULL) {
-        std::string xdg_env(XDG_CONFIG_HOME);
-        if (not xdg_env.empty())
-            configdir = xdg_env;
+    std::string xdg_env(XDG_CONFIG_HOME);
+    if (not xdg_env.empty()) {
+        configdir = xdg_env;
     }
 
     if (mkdir(configdir.data(), 0700) != 0) {