aboutsummaryrefslogtreecommitdiff
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
parentFix append. (diff)
downloadcalp-a19be8473c3060c10c76c85d633dc546eabd447a.tar.gz
calp-a19be8473c3060c10c76c85d633dc546eabd447a.tar.xz
Fix most memmory problems.
-rw-r--r--linked_list.inc.h1
-rw-r--r--parse.c13
-rw-r--r--strbuf.c10
-rw-r--r--trie.inc.h1
-rw-r--r--vcal.c9
5 files changed, 28 insertions, 6 deletions
diff --git a/linked_list.inc.h b/linked_list.inc.h
index 3710bc13..2622eb4b 100644
--- a/linked_list.inc.h
+++ b/linked_list.inc.h
@@ -19,6 +19,7 @@ FREE_F (LINK(TYPE)) {
if (this->after != NULL) this->after->before = NULL;
// TODO how much of value do I really wanna free?
// Should I implement some form of shared pointer?
+
if (this->value != NULL) FFREE(TYPE, this->value);
return 0;
}
diff --git a/parse.c b/parse.c
index a5f3206d..ca0e68c2 100644
--- a/parse.c
+++ b/parse.c
@@ -30,6 +30,11 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) {
SNEW(content_line, cline, keylen, vallen);
+ /*
+ * TODO
+ * When a file ends with CRLF then ctx_scope == s_none, leading to
+ * END:VEVENT erroring as bad start of calendar.
+ * */
char c;
while ( (c = fgetc(f)) != EOF) {
/*
@@ -60,7 +65,7 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) {
} else {
/* Actuall end of line, handle values. */
if (ungetc(s[1], f) != s[1]) {
- ERR_F("%s, %i", "Failed to put character back on FILE", line);
+ ERR_F("%s, %i", "Failed to put character back on FILE", line);
exit (2);
}
@@ -71,7 +76,7 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) {
}
strbuf_copy(cline.vals.cur->value, &str);
- strbuf_cap(cline.vals.cur->value);
+ // strbuf_cap(cline.vals.cur->value);
++line;
@@ -109,7 +114,7 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) {
if (! feof(f)) {
ERR("Error parsing");
- } else {
+ } else if (cline.vals.cur->value->len != 0 && str.ptr != 0) {
/*
* The standard (3.4, l. 2675) says that each icalobject must
* end with CRLF. My files however does not, so we also parse
@@ -194,7 +199,7 @@ int handle_kv(
} else {
NEW(content_line, c);
content_line_copy(c, cline);
- add_content_line (ev, c);
+ PUSH(TRIE(content_line))(&ev->clines, c->key.mem, c);
}
break;
}
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;
}
diff --git a/trie.inc.h b/trie.inc.h
index 50b8ec5b..fbc3b807 100644
--- a/trie.inc.h
+++ b/trie.inc.h
@@ -47,7 +47,6 @@ int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
last = t;
}
last->value = RESOLVE(TYPE)(last->value, val);
- // last->value = val;
return 0;
} else if (cur->c == subkey[0]) {
/* This node belongs to the key,
diff --git a/vcal.c b/vcal.c
index ba17338a..5ad35feb 100644
--- a/vcal.c
+++ b/vcal.c
@@ -23,6 +23,12 @@ INIT_F(vevent, char* filename) {
return 0;
}
+/*
+ * Resolves a collision in some form of structure (probably a hash-map
+ * or a trie). If dest is NULL just return new. Otherwise mutates dest
+ * to have the correct form, and returns it. Destroying new in the
+ * process.
+ */
content_line* RESOLVE(content_line)
(content_line* dest, content_line* new) {
@@ -99,6 +105,9 @@ FREE_F(vevent) {
return 0;
}
+/*
+ * TODO change this into PUSH(VCALENDAR) (vevent*) ?
+ */
int push_event(vcalendar* cal, vevent* ev) {
ev->calendar = cal;