summaryrefslogtreecommitdiffstats
path: root/src/userpref.c
diff options
context:
space:
mode:
authorGravatar Bastien Nocera2012-09-05 10:07:15 +0100
committerGravatar Martin Szulecki2012-09-05 14:15:37 +0200
commit825da48d2e9c20086c4e34869da0b28376676b4c (patch)
tree3e5ef66544cf6eb03cb747373f8d785c55c149b9 /src/userpref.c
parent060e3f2683ed2b0b08e1a31deb9608a99e193b4a (diff)
downloadlibimobiledevice-825da48d2e9c20086c4e34869da0b28376676b4c.tar.gz
libimobiledevice-825da48d2e9c20086c4e34869da0b28376676b4c.tar.bz2
Don't crash if $HOME is empty
If both $XDG_CONFIG_HOME and $HOME are unset, we'd try to copy a NULL string, causing a crash. This is the environment systemd provides to its daemons, and that was causing upowerd to crash. http://libiphone.lighthouseapp.com/projects/27916-libiphone/tickets/273-patch-fix-segfault-when-running-with-home-unset#ticket-273-2 http://libiphone.lighthouseapp.com/projects/27916/tickets/265-userpref_get_config_dir-segfaults-when-home-is-undefined https://bugzilla.redhat.com/show_bug.cgi?id=834359
Diffstat (limited to 'src/userpref.c')
-rw-r--r--src/userpref.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/userpref.c b/src/userpref.c
index a0c3545..0e774b7 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -102,6 +102,20 @@ static char *userpref_utf16_to_utf8(wchar_t *unistr, long len, long *items_read,
102} 102}
103#endif 103#endif
104 104
105static const char *userpref_get_tmp_dir()
106{
107 const char *cdir = getenv("TMPDIR");
108 if (cdir && cdir[0])
109 return cdir;
110 cdir = getenv("TMP");
111 if (cdir && cdir[0])
112 return cdir;
113 cdir = getenv("TEMP");
114 if (cdir && cdir[0])
115 return cdir;
116 return "/tmp";
117}
118
105static const char *userpref_get_config_dir() 119static const char *userpref_get_config_dir()
106{ 120{
107 if (__config_dir[0]) return __config_dir; 121 if (__config_dir[0]) return __config_dir;
@@ -125,7 +139,14 @@ static const char *userpref_get_config_dir()
125 const char *cdir = getenv("XDG_CONFIG_HOME"); 139 const char *cdir = getenv("XDG_CONFIG_HOME");
126 if (!cdir) { 140 if (!cdir) {
127 cdir = getenv("HOME"); 141 cdir = getenv("HOME");
128 strcpy(__config_dir, cdir); 142 if (!cdir || !cdir[0]) {
143 const char *tdir = userpref_get_tmp_dir();
144 strcpy(__config_dir, tdir);
145 strcat(__config_dir, DIR_SEP_S);
146 strcat(__config_dir, "root");
147 } else {
148 strcpy(__config_dir, cdir);
149 }
129 strcat(__config_dir, DIR_SEP_S); 150 strcat(__config_dir, DIR_SEP_S);
130 strcat(__config_dir, ".config"); 151 strcat(__config_dir, ".config");
131 } else { 152 } else {