From: Niki Roo Date: Thu, 26 May 2022 15:07:20 +0000 (+0200) Subject: nsub: fix a bug in read_lrc X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=1e05895310de5041d13aa21d9f793d45c857c373;p=nsub.git nsub: fix a bug in read_lrc --- diff --git a/src/nsub/nsub.c b/src/nsub/nsub.c index 0ea748f..6b9b728 100644 --- a/src/nsub/nsub.c +++ b/src/nsub/nsub.c @@ -102,6 +102,14 @@ void song_add_meta(song_t *song, char *key, char *value) { meta->value = value ? strdup(value) : NULL; } +void uninit_lyric(lyric_t *lyric) { + if (!lyric) + return; + + free(lyric->name); + free(lyric->text); +} + song_t *nsub_read(FILE *in, NSUB_FORMAT fmt) { song_t *song = NULL; cstring_t *line = NULL; diff --git a/src/nsub/nsub.h b/src/nsub/nsub.h index 1704437..2a6bd42 100644 --- a/src/nsub/nsub.h +++ b/src/nsub/nsub.h @@ -134,7 +134,7 @@ typedef struct { char *lang; } song_t; -/* Song */ +/* Song & Lyric */ song_t *new_song(); void free_song(song_t *song); @@ -143,6 +143,7 @@ void song_add_empty(song_t *song); void song_add_comment(song_t *song, char *comment); void song_add_lyric(song_t *song, int start, int stop, char *name, char *text); void song_add_meta(song_t *song, char *key, char *value); +void uninit_lyric(lyric_t *lyric); /* Read */ diff --git a/src/nsub/nsub_read_lrc.c b/src/nsub/nsub_read_lrc.c index 796424c..d9360ca 100644 --- a/src/nsub/nsub_read_lrc.c +++ b/src/nsub/nsub_read_lrc.c @@ -53,8 +53,8 @@ int nsub_read_lrc(song_t *song, char *line) { int start = lrc_millisec(line); char *name = NULL; - array_loop(song->lyrics, lyric, lyric_t) { + lyric_t *lyric = array_last(song->lyrics); if (lyric->type == NSUB_LYRIC) lyric->stop = start; if (lyric->type == NSUB_COMMENT) @@ -65,15 +65,15 @@ int nsub_read_lrc(song_t *song, char *line) { name = NULL; } - int text_offset = end + 1; + int text_offset = end + 2; while (line[text_offset] == ' ') text_offset++; if (line[text_offset]) { if (name) { + lyric_t *tmp = array_pop(song->lyrics); + uninit_lyric(tmp); song->current_num--; - // TODO: memory leak? - array_cut_at(song->lyrics, array_count(song->lyrics) - 1); } song_add_lyric(song, start, start + 5000, name, line + text_offset); } else {