]> jfr.im git - irc/atheme/libmowgli-2.git/commitdiff
core/mowgli_string: don't use realloc().
authorWilliam Pitcock <redacted>
Mon, 9 Apr 2012 03:56:54 +0000 (22:56 -0500)
committerWilliam Pitcock <redacted>
Mon, 9 Apr 2012 03:56:54 +0000 (22:56 -0500)
src/libmowgli/core/mowgli_string.c

index 59619edcf4b4b570f029d822a233c177c0c96892..716f9fb760a0407e4a72b87bd3dca312f406ccdb 100644 (file)
@@ -59,11 +59,17 @@ void mowgli_string_append(mowgli_string_t *self, const char *src, size_t n)
 {
        if (self->size - self->pos <= n)
        {
-               char *new;
+               char *new_ptr;
+               size_t oldsize;
 
+               oldsize = self->size;
                self->size = MAX(self->size * 2, self->pos + n + 8);
-               new = realloc(self->str, self->size);
-               self->str = new;
+
+               new_ptr = mowgli_alloc(self->size);
+               mowgli_strlcpy(new_ptr, self->str, self->size);
+
+               mowgli_free(self->str);
+               self->str = new_ptr;
        }
 
        memcpy(self->str + self->pos, src, n);
@@ -75,11 +81,17 @@ void mowgli_string_append_char(mowgli_string_t *self, const char c)
 {
        if (self->size - self->pos <= 1)
        {
-               char *new;
+               char *new_ptr;
+               size_t oldsize;
+
+               oldsize = self->size;
+               self->size = MAX(self->size * 2, self->pos + n + 8);
+
+               new_ptr = mowgli_alloc(self->size);
+               mowgli_strlcpy(new_ptr, self->str, self->size);
 
-               self->size = MAX(self->size * 2, self->pos + 9);
-               new = realloc(self->str, self->size);
-               self->str = new;
+               mowgli_free(self->str);
+               self->str = new_ptr;
        }
 
        self->str[self->pos++] = c;