lorid

convert chordpro to pdf
git clone git://git.relim.de/lorid.git
Log | Files | Refs | README | LICENSE

commit 6f39268da207807015347f5e5b248e556649dadb
parent 32c9c5f9317b037451c7690140ce8829243203d4
Author: nibo <nibo@relim.de>
Date:   Sun,  2 Jun 2024 16:12:36 +0200

Remove alloc/free error via valgrind

Diffstat:
Mchordpro.c | 43++++++++++++++++++++++++-------------------
Mlorid.c | 1+
2 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/chordpro.c b/chordpro.c @@ -342,6 +342,13 @@ struct ChoLineItem *cho_line_item_new(void) return item; } +void cho_line_item_free(struct ChoLineItem *item) +{ + cho_style_free(item->style); + free(item->text); + free(item); +} + struct ChoSection *cho_section_new(void) { struct ChoSection *section = malloc(sizeof(struct ChoSection)); @@ -593,14 +600,13 @@ struct ChoSong **cho_parse(FILE *fp) songs[so]->sections[se]->lines[li] = cho_line_new(); songs[so]->sections[se]->lines[li]->lyrics = malloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); - songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_new(); struct Tag **tags = malloc(sizeof(struct Tag *)); tags[ta] = NULL; ta++; while (feof(fp) == 0) { read = fread(&buf, 1, 1, fp); if (read == 1) { - printf("state: %s, buf: %c\n", the_state(state), buf); + // printf("state: %s, buf: %c\n", the_state(state), buf); switch (state) { case STATE_LYRICS: if (buf == '{') { @@ -622,6 +628,7 @@ struct ChoSong **cho_parse(FILE *fp) if (buf == '\n') { songs[so]->sections[se]->lines[li]->lyrics[ly]->text = realloc(songs[so]->sections[se]->lines[li]->lyrics[ly]->text, (te+1) * sizeof(char)); songs[so]->sections[se]->lines[li]->lyrics[ly]->text[te] = 0; + te = 0; ly++; songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = NULL; @@ -634,7 +641,6 @@ struct ChoSong **cho_parse(FILE *fp) songs[so]->sections[se]->lines[li] = cho_line_new(); songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); - songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_new(); break; } songs[so]->sections[se]->lines[li]->lyrics[ly]->text = realloc(songs[so]->sections[se]->lines[li]->lyrics[ly]->text, (te+1) * sizeof(char)); @@ -654,9 +660,8 @@ struct ChoSong **cho_parse(FILE *fp) case DT_ENVIRONMENT: switch (directive->position) { case POS_BEGIN: - ly++; - songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); - songs[so]->sections[se]->lines[li]->lyrics[ly] = NULL; + cho_line_item_free(songs[so]->sections[se]->lines[li]->lyrics[ly]); + free(songs[so]->sections[se]->lines[li]->lyrics); ly = 0; free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; @@ -669,13 +674,11 @@ struct ChoSong **cho_parse(FILE *fp) songs[so]->sections[se]->lines[li] = cho_line_new(); songs[so]->sections[se]->lines[li]->lyrics = malloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); - songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_new(); break; case POS_END: if (directive->stype == songs[so]->sections[se]->type) { - ly++; - songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); - songs[so]->sections[se]->lines[li]->lyrics[ly] = NULL; + cho_line_item_free(songs[so]->sections[se]->lines[li]->lyrics[ly]); + free(songs[so]->sections[se]->lines[li]->lyrics); ly = 0; free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; @@ -687,7 +690,6 @@ struct ChoSong **cho_parse(FILE *fp) songs[so]->sections[se]->lines[li] = cho_line_new(); songs[so]->sections[se]->lines[li]->lyrics = malloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); - songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_new(); } break; } @@ -753,9 +755,8 @@ struct ChoSong **cho_parse(FILE *fp) case DT_ENVIRONMENT: switch (directive->position) { case POS_BEGIN: - ly++; - songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); - songs[so]->sections[se]->lines[li]->lyrics[ly] = NULL; + cho_line_item_free(songs[so]->sections[se]->lines[li]->lyrics[ly]); + free(songs[so]->sections[se]->lines[li]->lyrics); ly = 0; free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; @@ -769,13 +770,11 @@ struct ChoSong **cho_parse(FILE *fp) songs[so]->sections[se]->lines[li] = cho_line_new(); songs[so]->sections[se]->lines[li]->lyrics = malloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); - songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_new(); break; case POS_END: if (directive->stype == songs[so]->sections[se]->type) { - ly++; - songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); - songs[so]->sections[se]->lines[li]->lyrics[ly] = NULL; + cho_line_item_free(songs[so]->sections[se]->lines[li]->lyrics[ly]); + free(songs[so]->sections[se]->lines[li]->lyrics); ly = 0; free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; @@ -787,7 +786,6 @@ struct ChoSong **cho_parse(FILE *fp) songs[so]->sections[se]->lines[li] = cho_line_new(); songs[so]->sections[se]->lines[li]->lyrics = malloc(sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); - songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_new(); } break; } @@ -815,6 +813,7 @@ struct ChoSong **cho_parse(FILE *fp) ly++; songs[so]->sections[se]->lines[li]->lyrics = realloc(songs[so]->sections[se]->lines[li]->lyrics, (ly+1) * sizeof(struct ChoLineItem *)); songs[so]->sections[se]->lines[li]->lyrics[ly] = cho_line_item_new(); + cho_style_free(songs[so]->sections[se]->lines[li]->lyrics[ly]->style); songs[so]->sections[se]->lines[li]->lyrics[ly]->style = cho_style_duplicate(directive->style); songs[so]->sections[se]->lines[li]->lyrics[ly]->text = string_remove_leading_whitespace(directive_value); te = 0; @@ -935,8 +934,14 @@ struct ChoSong **cho_parse(FILE *fp) int e = 0; while (tags[e] != NULL) { printf("name: %s\n", tags[e]->name); + free(tags[e]->name); + free(tags[e]); e++; } + free(tags); + cho_line_item_free(songs[so]->sections[se]->lines[li]->lyrics[ly]); + free(songs[so]->sections[se]->lines[li]->lyrics); + free(songs[so]->sections[se]->lines[li]->chords); free(songs[so]->sections[se]->lines[li]); songs[so]->sections[se]->lines[li] = NULL; songs[so]->metadata = realloc(songs[so]->metadata, (m+1) * sizeof(struct ChoMetadata *)); diff --git a/lorid.c b/lorid.c @@ -32,6 +32,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "cho_parse failed.\n"); return 1; } + printf("Something\n"); /*int m = 0;*/ /*printf("---- BEGIN METADATA ----\n");*/ /*while (song->metadata[m] != NULL) {*/