- if (*pos + str_length >= *len) {
- char *newbuf;
-
- newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
- *buf = newbuf;
- *len = *len * 2 + str_length + 1;
- }
-
- while (str_length-- >= 1) {
- (*buf)[(*pos)++] = *str;
- str++;
- }
- (*buf)[*pos] = '\0';
+ /* xasprintf trick will be smaller, but we are often
+ * called with str_length == 1 - don't want to have
+ * THAT much of malloc/freeing! */
+ char *buf = *bufp;
+ int len = (buf ? strlen(buf) : 0);
+ str_length++;
+ buf = xrealloc(buf, len + str_length);
+ /* copies at most str_length-1 chars! */
+ safe_strncpy(buf + len, str, str_length);
+ *bufp = buf;