lorid

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

commit c8d2989df01e732224ee06a152bf90ed134b2963
parent e73a5b4e11157ed6734cfa45f25aeefa8b863f93
Author: nibo <nibo@relim.de>
Date:   Fri, 18 Jul 2025 12:27:26 +0200

Free memory in case of error in src/out_pdf.c

Diffstat:
Msrc/out_pdf.c | 66++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 44 insertions(+), 22 deletions(-)

diff --git a/src/out_pdf.c b/src/out_pdf.c @@ -1916,13 +1916,14 @@ pdf_texts_add_toc_entry( ) { struct PDFText ***texts; - texts = &ctx->t_ctx.content->pages[ctx->t_ctx.page]->texts; double width, page_no_x, end_of_title_x, width_between_title_and_page_no, available_dots_width; double page_no_width, dots_width; int index, line_count; char tmp[strlen(entry->title)+1]; size_t page_no_size = 11 + 1; char page_no[page_no_size]; + + texts = &ctx->t_ctx.content->pages[ctx->t_ctx.page]->texts; strcpy((char *)&tmp, entry->title); width = text_width(ctx, entry->title, style); if (width == ERROR) { @@ -2103,6 +2104,7 @@ pdf_texts_add_lyrics( double width; int t = 0; int c; + texts = &ctx->b_ctx.content->pages[ctx->b_ctx.page]->texts; *texts = erealloc(*texts, (ctx->b_ctx.text+1) * sizeof(struct PDFText *)); (*texts)[ctx->b_ctx.text] = pdf_text_new(); @@ -2211,6 +2213,7 @@ pdf_texts_add_text( char str[strlen(text)+1]; double width; int index; + strcpy((char *)&str, text); texts = &c_ctx->content->pages[c_ctx->page]->texts; width = text_width(ctx, text, style); @@ -2457,12 +2460,14 @@ pdf_body_create( ctx->b_ctx.toc_entry++; if (!pdf_texts_add_text(ctx, &ctx->b_ctx, metadata, metadata_style, ALIGNMENT_CENTER, NUMERAL_SYSTEM_WESTERN_ARABIC)) { LOG_DEBUG("pdf_texts_add_text(title) failed."); + free(metadata); return false; } free(metadata); if (cho_metadata_value(songs[s]->metadata, "subtitle", ctx->config->metadata_separator, &metadata, &metadata_style)) { if (!pdf_texts_add_text(ctx, &ctx->b_ctx, metadata, metadata_style, ALIGNMENT_CENTER, NUMERAL_SYSTEM_WESTERN_ARABIC)) { LOG_DEBUG("pdf_texts_add_text(subtitle) failed."); + free(metadata); return false; } free(metadata); @@ -2517,12 +2522,16 @@ pdf_body_create( struct CharPosition *pos; struct ChoLineItemAbove **left_aboves = (*li)->text_above; struct ChoLineItem **left_items = (*li)->items; + while (*left_aboves || *left_items) { + bool text_above_exist; + char *string; + int i; + struct ChoStyle *style; + ctx->b_ctx.consumed_lyrics = 0; ctx->b_ctx.biggest_font_size = 0.0; ctx->b_ctx.prev_added_space = 0.0; - char *string; - struct ChoStyle *style; if (!calc_space_between_text_above(ctx, left_items, left_aboves, img_objs, &ctx->b_ctx.spaces)) { LOG_DEBUG("calc_space_between_text_above failed."); return false; @@ -2542,16 +2551,15 @@ pdf_body_create( text_above_index = 20000; } } - bool text_above_exist; - int i; for (i = 0; left_aboves[i] && i<text_above_index; i++) { + struct SpaceNeeded **sp; + width = line_width_until_text_above(ctx, left_items, left_aboves[i], img_objs, NULL); if (width == ERROR) { LOG_DEBUG("line_width_until_text_above failed."); return false; } ctx->b_ctx.x = MARGIN_HORIZONTAL + width + ctx->b_ctx.prev_added_space; - struct SpaceNeeded **sp; for (sp = ctx->b_ctx.spaces; *sp; sp++) { if ((*sp)->text_above_index == i) { ctx->b_ctx.x += (*sp)->amount; @@ -2608,11 +2616,13 @@ pdf_body_create( if (ctx->b_ctx.y < ctx->b_ctx.margin_bottom) { struct PDFText **tmp = NULL; int tm = 0; + ctx->b_ctx.y = MEDIABOX_HEIGHT - MARGIN_TOP; if (text_above_exist) { /* INFO: chords/annotations and their corresponding lyrics won't be splitted */ int p; double prev_y = (*texts)[ctx->b_ctx.text-1]->y; + for (p = ctx->b_ctx.text-1; prev_y == (*texts)[p]->y; p--) { (*texts)[p]->y = ctx->b_ctx.y; tmp = erealloc(tmp, (tm+1) * sizeof(struct PDFText *)); @@ -2623,6 +2633,7 @@ pdf_body_create( } if (!pdf_page_close_then_add(ctx, &ctx->b_ctx, NUMERAL_SYSTEM_WESTERN_ARABIC)) { LOG_DEBUG("pdf_page_close_then_add failed."); + free(tmp); return false; } texts = &ctx->b_ctx.content->pages[ctx->b_ctx.page]->texts; @@ -2684,10 +2695,12 @@ pdf_body_create( if (pos->line_item_index != -1 && pos->text_index != -1) { if ((*left_items)->is_text) { char *tmp; + (*left_items)->u.text->text[pos->text_index] = 0; tmp = strdup(&(*left_items)->u.text->text[pos->text_index+1]); if (!pdf_texts_add_lyrics(ctx, *left_items, i)) { LOG_DEBUG("pdf_texts_add_lyrics failed."); + free(tmp); return false; } free((*left_items)->u.text->text); @@ -2783,8 +2796,9 @@ pdf_toc_render(struct PDFContext *ctx, struct PDFContent *content) struct PDFPage **pages; struct PDFText **texts; pdfio_stream_t *stream; - pages = content->pages; int p; + + pages = content->pages; for (p = 0; pages[p]; p++) { ctx->current_page_index = p; stream = pdf_page_create(ctx, NULL, pages[p]->annots); @@ -2814,6 +2828,7 @@ pdf_body_render(struct PDFContext *ctx, struct PDFContent *content) struct PDFText **texts; struct PDFImage **imgs; pdfio_stream_t *stream; + pages = content->pages; for (p = 0; pages[p]; p++) { ctx->current_page_index = p; @@ -2876,14 +2891,16 @@ out_pdf_create( struct Config *config ) { - struct Font **needed_fonts; + struct Font **needed_fonts = NULL; struct Obj **img_objs = NULL; struct PDFContent *pdf_body = NULL; struct PDFContent *pdf_toc = NULL; struct PDFContext ctx; pdfio_rect_t media_box_a4 = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT }; pdfio_rect_t crop_box = { 0.0, 0.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT }; - char *dirpath, *pdf_filepath, *pdf_dot_filepath; + char *dirpath = NULL; + char *pdf_filepath = NULL; + char *pdf_dot_filepath = NULL; ctx.diagram_font_is_base_font = false; ctx.fonts = NULL; @@ -2892,7 +2909,6 @@ out_pdf_create( ctx.current_font_size = 0.0; memset(&ctx.cho_dirpath, 0, PATH_MAX); memset(&ctx.current_font_name, 0, 200); - if (cho_filepath) { dirpath = filepath_dirname(cho_filepath); } else { @@ -2903,52 +2919,51 @@ out_pdf_create( pdf_filepath = pdf_filepath_create(&ctx, songs, cho_filepath, output_folder_or_file); if (!pdf_filepath) { LOG_DEBUG("pdf_filepath_create failed."); - return NULL; + goto ERR; } pdf_dot_filepath = filepath_as_dot_file(pdf_filepath); ctx.pdf_file = pdfioFileCreate(pdf_dot_filepath, "2.0", &media_box_a4, &crop_box, NULL, NULL); if (!ctx.pdf_file) { LOG_DEBUG("pdfioFileCreateTemporary failed."); - return NULL; + goto ERR; } if (!pdf_set_title(&ctx, songs)) { LOG_DEBUG("pdf_set_title failed."); - goto CLEAN; + goto ERR; } needed_fonts = fonts_get_all(songs, ctx.config); if (!pdf_load_fonts(&ctx, needed_fonts)) { LOG_DEBUG("pdf_load_fonts failed."); - goto CLEAN; + goto ERR; } cho_fonts_free(needed_fonts); if (!pdf_load_images(&ctx, &img_objs, songs)) { LOG_DEBUG("pdf_load_images failed."); - goto CLEAN; + goto ERR; } if (!pdf_body_create(&ctx, songs, img_objs, &pdf_body)) { LOG_DEBUG("pdf_body_create failed."); - goto CLEAN; + goto ERR; } if (ctx.config->output->toc->show) { if (!pdf_toc_create(&ctx, pdf_body, &pdf_toc)) { LOG_DEBUG("pdf_toc_create failed."); - goto CLEAN; + goto ERR; } if (!pdf_toc_render(&ctx, pdf_toc)) { LOG_DEBUG("pdf_toc_render failed."); - goto CLEAN; + goto ERR; } } if (!pdf_body_render(&ctx, pdf_body)) { LOG_DEBUG("pdf_body_render failed."); - goto CLEAN; + goto ERR; } objs_free(img_objs); pdf_content_free(pdf_toc); pdf_content_free(pdf_body); if (!pdfioFileClose(ctx.pdf_file)) { LOG_DEBUG("pdfioFileClose failed."); - goto CLEAN; } objs_free(ctx.fonts); if (rename(pdf_dot_filepath, pdf_filepath)) { @@ -2965,11 +2980,18 @@ out_pdf_create( } free(pdf_dot_filepath); return pdf_filepath; - CLEAN: + ERR: + objs_free(img_objs); + pdf_content_free(pdf_toc); + pdf_content_free(pdf_body); + if (!pdfioFileClose(ctx.pdf_file)) { + LOG_DEBUG("pdfioFileClose failed."); + } + objs_free(ctx.fonts); + free(pdf_filepath); if (unlink(pdf_dot_filepath)) { LOG_DEBUG("unlink failed."); } free(pdf_dot_filepath); - free(pdf_filepath); return NULL; }