Never use getenv("HOME") without checking for NULL and non-zero

Obtained from: OpenBSD
This commit is contained in:
Kevin Lo 2007-10-30 03:44:10 +00:00
parent fa99a6317c
commit 0f10497bce
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=173169
2 changed files with 11 additions and 3 deletions

View File

@ -337,7 +337,10 @@ opencal(void)
if (!freopen(calendarFile, "r", stdin))
return (NULL);
} else {
chdir(getenv("HOME"));
char *home = getenv("HOME");
if (home == NULL || *home == '\0')
errx(1, "cannot get home directory");
chdir(home);
for (found = i = 0; i < sizeof(calendarHomes) /
sizeof(calendarHomes[0]); i++)
if (chdir(calendarHomes[i]) == 0 &&

View File

@ -171,6 +171,7 @@ main(int argc, char *argv[])
int blast = 0;
struct stat buf; /* stat to check access of bounds */
FILE *bounds;
char *cp;
#ifdef UNBUFFERED
setbuf(stdout, NULL);
@ -286,7 +287,7 @@ main(int argc, char *argv[])
lastmsg = 0;
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
char *cp = dp->d_name;
cp = dp->d_name;
int i = 0;
if (dp->d_ino == 0)
@ -402,7 +403,11 @@ main(int argc, char *argv[])
totty = (isatty(fileno(stdout)) != 0);
use_pager = use_pager && totty;
snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), MSGSRC);
if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
fprintf(stderr, "Error, no home directory!\n");
exit(1);
}
snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
msgsrc = fopen(fname, "r");
if (msgsrc) {
newrc = NO;