Fixing zero-bye append in readlink. Thanks, Willem van de Krol

This commit is contained in:
David Galos 2013-07-09 19:40:28 -04:00
parent 18712b2032
commit 7adf4f380a
1 changed files with 3 additions and 1 deletions

View File

@ -20,6 +20,7 @@ main(int argc, char *argv[])
char buf[PATH_MAX]; char buf[PATH_MAX];
bool nflag = false; bool nflag = false;
bool fflag = false; bool fflag = false;
ssize_t n;
ARGBEGIN { ARGBEGIN {
case 'f': case 'f':
@ -42,8 +43,9 @@ main(int argc, char *argv[])
if (realpath(argv[0], buf) == NULL) if (realpath(argv[0], buf) == NULL)
exit(1); exit(1);
} else { } else {
if (readlink(argv[0], buf, sizeof(buf)) < 0) if ((n = readlink(argv[0], buf, sizeof(buf) - 1)) < 0)
exit(1); exit(1);
buf[n] = '\0';
} }
printf("%s", buf); printf("%s", buf);