diff options
Diffstat (limited to 'src/common/better_string.c')
-rw-r--r-- | src/common/better_string.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/common/better_string.c b/src/common/better_string.c index 366dda6..7863da0 100644 --- a/src/common/better_string.c +++ b/src/common/better_string.c @@ -18,11 +18,12 @@ string_t *init_string(const char *src) { } void string_push(string_t *s, char c) { - if (s->len >= s->size - 2) { + if (s->len >= s->size - 3) { s->size *= 2; s->buf = safe_realloc(s->buf, s->size); } s->buf[s->len] = c; + s->buf[s->len + 1] = '\0'; s->len++; } @@ -33,9 +34,8 @@ char string_pop(string_t *s) { } void string_concat_const(string_t *s1, const char *s2) { - for (int i = 0; i < strlen(s2); i++) { + for (int i = 0; i < strlen(s2); i++) string_push(s1, s2[i]); - } } void string_concat(string_t *s1, string_t *s2) { @@ -46,6 +46,10 @@ void string_concat(string_t *s1, string_t *s2) { void string_free(void *x) { string_t *s = x; + + if (!x) + return; + free(s->buf); free(s); } |