check snprintf error aswell, handle as truncation error

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma
2014-06-01 15:01:09 +02:00
committed by sin
parent 953ebf3573
commit eac0f658cf
3 changed files with 13 additions and 10 deletions

6
du.c
View File

@@ -120,6 +120,7 @@ du(const char *path)
struct dirent *dent;
struct stat st;
long n = 0, m;
int r;
if (lstat(path, &st) < 0)
eprintf("stat: %s:", path);
@@ -149,8 +150,9 @@ du(const char *path)
n += m;
if (aflag && !sflag) {
if (S_ISLNK(st.st_mode)) {
if (snprintf(file, sizeof(file), "%s/%s",
cwd, dent->d_name) >= sizeof(file))
r = snprintf(file, sizeof(file), "%s/%s",
cwd, dent->d_name);
if(r >= sizeof(file) || r < 0)
eprintf("path too long\n");
} else {
xrealpath(dent->d_name, file);