sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit eac0f658cfce9aa70492127fbc980aca9b0f8f7b
parent 953ebf35730c986ca68b0cf7d97f22dceccb08a0
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun,  1 Jun 2014 15:01:09 +0200

check snprintf error aswell, handle as truncation error

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>

Diffstat:
Mdu.c | 6++++--
Mmktemp.c | 7++++---
Mutil/cp.c | 10+++++-----
3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/du.c b/du.c @@ -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); diff --git a/mktemp.c b/mktemp.c @@ -21,7 +21,7 @@ main(int argc, char *argv[]) char *template = "tmp.XXXXXXXXXX"; char *tmpdir = "/tmp", *p; char tmppath[PATH_MAX]; - int fd; + int fd, r; ARGBEGIN { case 'd': @@ -42,8 +42,9 @@ main(int argc, char *argv[]) if ((p = getenv("TMPDIR"))) tmpdir = p; - if (snprintf(tmppath, sizeof(tmppath), - "%s/%s", tmpdir, template) >= sizeof(tmppath)) + r = snprintf(tmppath, sizeof(tmppath), + "%s/%s", tmpdir, template); + if (r >= sizeof(tmppath) || r < 0) eprintf("path too long\n"); if (dflag) { if (!mkdtemp(tmppath)) { diff --git a/util/cp.c b/util/cp.c @@ -24,6 +24,7 @@ cp(const char *s1, const char *s2) struct dirent *d; struct stat st; DIR *dp; + int r; if (stat(s1, &st) == 0 && S_ISDIR(st.st_mode)) { if (!cp_rflag) @@ -40,14 +41,13 @@ cp(const char *s1, const char *s2) while((d = readdir(dp))) { if(strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { - if(snprintf(ns1, size1, "%s/%s", s1, - d->d_name) >= size1) { + r = snprintf(ns1, size1, "%s/%s", s1, d->d_name); + if(r >= size1 || r < 0) { eprintf("%s/%s: filename too long\n", s1, d->d_name); } - - if(snprintf(ns2, size2, "%s/%s", s2, - d->d_name) >= size2) { + r = snprintf(ns2, size2, "%s/%s", s2, d->d_name); + if(r >= size2 || r < 0) { eprintf("%s/%s: filename too long\n", s2, d->d_name); }