From a19be8473c3060c10c76c85d633dc546eabd447a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 5 Feb 2019 17:53:13 +0100 Subject: Fix most memmory problems. --- strbuf.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index 81c9e1f5..2f83767f 100644 --- a/strbuf.c +++ b/strbuf.c @@ -50,6 +50,12 @@ FREE_F(strbuf) { * TODO this should do bounds check */ int strbuf_append(strbuf* s, char c) { +#ifdef SAFE_STR + if (s->len > s->alloc) { + ERR("Not enough memmory allocated"); + return 1; + } +#endif s->mem[s->len] = c; s->ptr = ++s->len; return 0; @@ -62,13 +68,15 @@ int strbuf_cap(strbuf* s) { int strbuf_copy(strbuf* dest, strbuf* src) { #ifdef SAFE_STR - if (dest->alloc < src->len) { + if (dest->alloc + 1 < src->len) { ERR("Not enough memmory allocated"); return 1; } #endif dest->len = src->len; memcpy(dest->mem, src->mem, src->len); + // TODO should this be here? + strbuf_cap(dest); return 0; } -- cgit v1.2.3