about summary refs log tree commit diff stats
path: root/src/config/files.c
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-06-12 10:23:31 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-06-12 10:23:31 +0200
commitf1141932fc45fa4bcc87984241b524381c0ab2b3 (patch)
treec405c18fe32e0c73bf9d50c68499a2b8a9950805 /src/config/files.c
parent74ff38f0bda6e365bdedf29fbd53eedfa520ed74 (diff)
downloadprofani-tty-f1141932fc45fa4bcc87984241b524381c0ab2b3.tar.gz
Dont manipulate pointer from getenv
Found this when looking to fix bug https://github.com/profanity-im/profanity/issues/1357
Not sure if it is related.

man 3 getenv sais:
```
As  typically implemented, getenv() returns a pointer to a string within
the environment list.  The caller must take  care  not  to  modify  this
string, since that would change the environment of the process.
```
Diffstat (limited to 'src/config/files.c')
-rw-r--r--src/config/files.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/config/files.c b/src/config/files.c
index bdaf6a93..ff612d64 100644
--- a/src/config/files.c
+++ b/src/config/files.c
@@ -170,12 +170,16 @@ files_get_data_path(char *data_base)
 static char*
 _files_get_xdg_config_home(void)
 {
-    gchar *xdg_config_home = getenv("XDG_CONFIG_HOME");
-    if (xdg_config_home)
+    gchar *xdg_config_home_env = getenv("XDG_CONFIG_HOME");
+    gchar *xdg_config_home = NULL;
+
+    if (xdg_config_home_env) {
+        xdg_config_home = strdup(xdg_config_home_env);
         g_strstrip(xdg_config_home);
+    }
 
     if (xdg_config_home && (strcmp(xdg_config_home, "") != 0)) {
-        return strdup(xdg_config_home);
+        return xdg_config_home;
     } else {
         GString *default_path = g_string_new(getenv("HOME"));
         g_string_append(default_path, "/.config");
@@ -189,12 +193,16 @@ _files_get_xdg_config_home(void)
 static char*
 _files_get_xdg_data_home(void)
 {
-    gchar *xdg_data_home = getenv("XDG_DATA_HOME");
-    if (xdg_data_home)
+    gchar *xdg_data_home_env = getenv("XDG_DATA_HOME");
+    gchar *xdg_data_home = NULL;
+
+    if (xdg_data_home_env) {
+        xdg_data_home = strdup(xdg_data_home_env);
         g_strstrip(xdg_data_home);
+    }
 
     if (xdg_data_home && (strcmp(xdg_data_home, "") != 0)) {
-        return strdup(xdg_data_home);
+        return xdg_data_home;
     } else {
         GString *default_path = g_string_new(getenv("HOME"));
         g_string_append(default_path, "/.local/share");