Print this page
Address Robert's feedback
@@ -99,10 +99,14 @@
static boolean_t
str_reserve(str_t *s, size_t amt)
{
size_t newlen = s->str_len + amt;
+ /* overflow check */
+ if (newlen < s->str_len || newlen < amt)
+ return (B_FALSE);
+
if ((amt > 0) && (s->str_len + amt <= s->str_size))
return (B_TRUE);
size_t newsize = roundup(newlen, STR_CHUNK_SZ);
void *temp;
@@ -161,11 +165,11 @@
dest->str_len += src->str_len;
return (B_TRUE);
}
boolean_t
-str_append_c(str_t *s, int c)
+str_append_c(str_t *s, char c)
{
if (!str_reserve(s, 1))
return (B_FALSE);
s->str_s[s->str_len++] = c;
@@ -207,13 +211,23 @@
}
if (!str_reserve(dest, src->str_len))
return (B_FALSE);
- /* Unlike some programmers, *I* can read manpages. */
+ /*
+ * Shift the contents of dest over at the insertion point. Since
+ * src and dest ranges will overlap, and unlike some programmers,
+ * *I* can read man pages - memmove() is the appropriate function
+ * to this.
+ */
(void) memmove(dest->str_s + idx + src->str_len, dest->str_s + idx,
dest->str_len - idx);
+
+ /*
+ * However the content to insert does not overlap with the destination
+ * so memcpy() is fine here.
+ */
(void) memcpy(dest->str_s + idx, src->str_s, src->str_len);
dest->str_len += src->str_len;
return (B_TRUE);
}