diff options
author | Hugo Hörnquist <hugo@hornquist.se> | 2019-01-20 23:01:04 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@hornquist.se> | 2019-01-20 23:01:04 +0100 |
commit | bb76c6a4d74036c9de5e75134554ca04b0b9b5cb (patch) | |
tree | 809b8c9b1240be540c581a6e28ac0fcdb75c8dde /strbuf.c | |
parent | Set SAFE_STR in makefile. (diff) | |
download | calp-bb76c6a4d74036c9de5e75134554ca04b0b9b5cb.tar.gz calp-bb76c6a4d74036c9de5e75134554ca04b0b9b5cb.tar.xz |
Fix some memmory errors.
And discovered some new ones.
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -2,6 +2,9 @@ #include <string.h> +#include <stdio.h> +#define ERR(s) fprintf(stderr, "\x1B[0;31mERR\x1b[m (strbuf %3i): %s\n", __LINE__, s) + int init_string(string* str, size_t len) { str->mem = malloc(len); str->alloc = len; @@ -24,7 +27,7 @@ int realloc_string(string* str, size_t len) { int free_string(string* str) { #ifdef SAFE_STR - if (str->alloc == 0) { + if (str->alloc == 0 || str->mem == NULL) { ERR("String not allocated"); return 1; } @@ -69,7 +72,7 @@ char* charat(string* s, int idx) { #ifdef SAFE_STR if (idx > s->len) { ERR("Index out of bounds"); - return -1; + return (char*) -1; } #endif return &s->mem[idx]; @@ -88,7 +91,7 @@ int strbuf_reset(string* s) int strbuf_init_copy(string* dest, string* src) { #ifdef SAFE_STR if (dest->alloc != 0) { - ERR("Dest already allocated", -1); + ERR("Dest already allocated"); return 1; } #endif |