ref: ceab69f32aa9b98b40be8f76e3b29a86c0cdb3cb
parent: d7f35e49d14644bcb7a1327a1fdb8cdb8f55dac3
author: Ori Bernstein <[email protected]>
date: Sat Aug 18 22:51:31 EDT 2012
Don't use string functions with unchecked length. Sure, it's safe if we calculate, but it's better if we don't use them at all. And OpenBSD is happier if we don't use them.
--- a/6/simp.c
+++ b/6/simp.c
@@ -194,14 +194,14 @@
len = strlen(Fprefix);
if (n->name.ns)
len += strlen(n->name.ns) + 1; /* +1 for separator */
- len += strlen(n->name.name);
+ len += strlen(n->name.name) + 1;
s = xalloc(len + 1);
s[0] = '\0';
- sprintf(s, "%s", Fprefix);
+ snprintf(s, len, "%s", Fprefix);
if (n->name.ns)
- sprintf(s, "%s%s$", s, n->name.ns);
- sprintf(s, "%s%s", s, n->name.name);
+ snprintf(s, len, "%s%s$", s, n->name.ns);
+ snprintf(s, len, "%s%s", s, n->name.name);
return s;
}
--- a/parse/util.c
+++ b/parse/util.c
@@ -93,8 +93,7 @@
n = strlen(u) + strlen(v) + 1;
s = xalloc(n);
- strcpy(s, u);
- strcat(s, v);
+ snprintf(s, n + 1, "%s%s", u, v);
return s;
}
@@ -352,12 +351,12 @@
die("swapsuffix: buf too small");
buf[0] = '\0';
+ /* if we have matching suffixes */
if (suflen < slen && !strcmp(suf, &s[slen - suflen])) {
strncat(buf, s, slen - suflen);
- strcat(buf, swap);
+ strncat(buf, swap, suflen);
} else {
- strncat(buf, s, slen);
- strcat(buf, swap);
+ snprintf(buf, sz, "%s%s", s, swap);
}
return buf;