aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-01-20 23:01:04 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-01-20 23:01:04 +0100
commitbb76c6a4d74036c9de5e75134554ca04b0b9b5cb (patch)
tree809b8c9b1240be540c581a6e28ac0fcdb75c8dde /strbuf.c
parentSet SAFE_STR in makefile. (diff)
downloadcalp-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.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/strbuf.c b/strbuf.c
index 877778a0..48702cf8 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -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