Add estrlcat() and estrlcpy()
It has become a common idiom in sbase to check strlcat() and strlcpy()
using
if (strl{cat, cpy}(dst, src, siz) >= siz)
eprintf("path too long\n");
However, this was not carried out consistently and to this very day,
some tools employed unchecked calls to these functions, effectively
allowing silent truncations to happen, which in turn may lead to
security issues.
To finally put an end to this, the e*-functions detect truncation
automatically and the caller can lean back and enjoy coding without
trouble. :)
This commit is contained in:
@@ -66,12 +66,10 @@ recurse(const char *path, void *data, struct recursor *r)
|
||||
}
|
||||
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||
continue;
|
||||
if (strlcpy(subpath, path, PATH_MAX) >= PATH_MAX)
|
||||
eprintf("strlcpy: path too long\n");
|
||||
if (path[strlen(path) - 1] != '/' && strlcat(subpath, "/", PATH_MAX) >= PATH_MAX)
|
||||
eprintf("strlcat: path too long\n");
|
||||
if (strlcat(subpath, d->d_name, PATH_MAX) >= PATH_MAX)
|
||||
eprintf("strlcat: path too long\n");
|
||||
estrlcpy(subpath, path, sizeof(subpath));
|
||||
if (path[strlen(path) - 1] != '/')
|
||||
estrlcat(subpath, "/", sizeof(subpath));
|
||||
estrlcat(subpath, d->d_name, sizeof(subpath));
|
||||
if ((r->flags & SAMEDEV) && statf(subpath, &dst) < 0) {
|
||||
if (errno != ENOENT) {
|
||||
weprintf("%s %s:", statf_name, subpath);
|
||||
|
||||
@@ -50,3 +50,14 @@ strlcat(char *dst, const char *src, size_t siz)
|
||||
*d = '\0';
|
||||
return(dlen + (s - src)); /* count does not include NUL */
|
||||
}
|
||||
|
||||
size_t
|
||||
estrlcat(char *dst, const char *src, size_t siz)
|
||||
{
|
||||
size_t ret;
|
||||
|
||||
if ((ret = strlcat(dst, src, siz)) >= siz)
|
||||
eprintf("strlcat: input string too long\n");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -46,3 +46,14 @@ strlcpy(char *dst, const char *src, size_t siz)
|
||||
}
|
||||
return(s - src - 1); /* count does not include NUL */
|
||||
}
|
||||
|
||||
size_t
|
||||
estrlcpy(char *dst, const char *src, size_t siz)
|
||||
{
|
||||
size_t ret;
|
||||
|
||||
if ((ret = strlcpy(dst, src, siz)) >= siz)
|
||||
eprintf("strlcpy: input string too long\n");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user