aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 17:53:13 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 18:16:19 +0100
commita19be8473c3060c10c76c85d633dc546eabd447a (patch)
treefea97d5207a7c0b5783efc979ebbf9317ccbaba7 /strbuf.c
parentFix append. (diff)
downloadcalp-a19be8473c3060c10c76c85d633dc546eabd447a.tar.gz
calp-a19be8473c3060c10c76c85d633dc546eabd447a.tar.xz
Fix most memmory problems.
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c10
1 files changed, 9 insertions, 1 deletions
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;
}