[hackers] [sbase] check snprintf error aswell, handle as truncation error || Hiltjo Posthuma
commit eac0f658cfce9aa70492127fbc980aca9b0f8f7b
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Jun 1 15:01:09 2014 +0200
check snprintf error aswell, handle as truncation error
Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
diff --git a/du.c b/du.c
index 304fee1..8382178 100644
--- a/du.c
+++ b/du.c
_AT_@ -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);
_AT_@ -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
");
} else {
xrealpath(dent->d_name, file);
diff --git a/mktemp.c b/mktemp.c
index cf2572f..e001dac 100644
--- a/mktemp.c
+++ b/mktemp.c
_AT_@ -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':
_AT_@ -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
");
if (dflag) {
if (!mkdtemp(tmppath)) {
diff --git a/util/cp.c b/util/cp.c
index e8627cd..753e616 100644
--- a/util/cp.c
+++ b/util/cp.c
_AT_@ -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)
_AT_@ -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
",
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
",
s2, d->d_name);
}
Received on Sun Jun 01 2014 - 19:02:59 CEST
This archive was generated by hypermail 2.3.0
: Sun Jun 01 2014 - 19:12:14 CEST