blob: 02381d9c7a11d5641c3e21ff094dba8436ac27d0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
deffname: do not segfault when getpwuid() has no entry for the current uid
On a minimal target the user running birthday may have no entry in
/etc/passwd, so getpwuid(getuid()) returns NULL and the old code
dereferenced it (strcpy(buf, NULL->pw_dir)) => segmentation fault.
Fall back to $HOME when there is no passwd entry, and exit with a clear
error message when neither is available.
--- a/bdengine.c
+++ b/bdengine.c
@@ -309,9 +309,20 @@
{
#if defined(UNIX)
char buf[256];
+ struct passwd *pw;
+ const char *home;
- strcpy(buf,getpwuid(getuid())->pw_dir);
- strcat(buf, "/" DEFAULT_FILE);
+ /* getpwuid() may fail (e.g. no passwd entry for the current uid on a
+ minimal system): dereferencing the NULL result segfaults. Fall back
+ to $HOME in that case. */
+ pw = getpwuid(getuid());
+ if (pw != NULL && pw->pw_dir != NULL && pw->pw_dir[0] != '\0')
+ home = pw->pw_dir;
+ else if ((home = getenv("HOME")) == NULL || home[0] == '\0')
+ fatal("birthday: no passwd entry for uid %d and HOME is not set\n",
+ (int)getuid());
+
+ snprintf(buf, sizeof(buf), "%s/%s", home, DEFAULT_FILE);
return strdup(buf);
#elif defined(DOS)
|