History

fftools/tf_mermaid: Add missing uninit and fix leaks

- merge forgotten uninit from work branch - add set_str() function to free before overwriting - fix some other leaks

softworkz committed 1 year ago

Commit b7d44dd1 parent a0ac89e1 file · +37 −8Browse files

Unified · Split

fftools/textformat/tf_mermaid.c+37−8
@@ -153,7 +153,6 @@ typedef struct MermaidContext { · 152 unmodified lines
153153 } section_data[SECTION_MAX_NB_LEVELS];
154154
155155 unsigned nb_link_captions[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
156− AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
157156 AVBPrint link_buf; ///< print buffer for writing diagram links
158157 AVDictionary *link_dict;
159158 } MermaidContext;
@@ -216,6 +215,32 @@ static av_cold int mermaid_init_html(AVTextFormatContext *tfc) · 56 unmodified lines
216215 return 0;
217216 }
218217
218+static av_cold int mermaid_uninit(AVTextFormatContext *tfc)
219+{
220+ MermaidContext *mmc = tfc->priv;
221+
222+ av_bprint_finalize(&mmc->link_buf, NULL);
223+ av_dict_free(&mmc->link_dict);
224+
225+ for (unsigned i = 0; i < SECTION_MAX_NB_LEVELS; i++) {
226+ av_freep(&mmc->section_data[i].dest_id);
227+ av_freep(&mmc->section_data[i].section_id);
228+ av_freep(&mmc->section_data[i].src_id);
229+ av_freep(&mmc->section_data[i].section_type);
230+ }
231+
232+ return 0;
233+}
234+
235+static void set_str(const char **dst, const char *src)
236+{
237+ if (*dst)
238+ av_freep(dst);
239+
240+ if (src)
241+ *dst = av_strdup(src);
242+}
243+
219244 #define MM_INDENT() writer_printf(tfc, "%*c", mmc->indent_level * 2, ' ')
220245
221246 static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *data)
@@ -266,6 +291,8 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 44 unmodified lines
266291 break;
267292 }
268293
294+ av_bprint_finalize(&css_buf, NULL);
295+ av_freep(&directive);
269296 return;
270297 }
271298
@@ -310,7 +337,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 38 unmodified lines
310337 }
311338
312339 mmc->section_data[tfc->level].subgraph_start_incomplete = 1;
313− mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id);
340+ set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id);
314341 }
315342
316343 if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE) {
@@ -322,7 +349,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 5 unmodified lines
322349
323350 if (sec_ctx->context_id) {
324351
325− mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id);
352+ set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id);
326353
327354 switch (mmc->diagram_config->diagram_type) {
328355 case AV_DIAGRAMTYPE_GRAPH:
@@ -352,7 +379,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 23 unmodified lines
352379 av_log(tfc, AV_LOG_ERROR, "Unable to write shape start. Missing id field. Section: %s", section->name);
353380 }
354381
355− mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id);
382+ set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id);
356383 }
357384
358385
@@ -371,7 +398,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 12 unmodified lines
371398 mmc->nb_link_captions[tfc->level] = 0;
372399
373400 if (sec_ctx && sec_ctx->context_type)
374− mmc->section_data[tfc->level].section_type = av_strdup(sec_ctx->context_type);
401+ set_str(&mmc->section_data[tfc->level].section_type, sec_ctx->context_type);
375402
376403 ////if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) {
377404 //// AVBPrint buf;
@@ -533,17 +560,17 @@ static void mermaid_print_value(AVTextFormatContext *tfc, const char *key, · 155 unmodified lines
533560 int exit = 0;
534561
535562 if (section->id_key && !strcmp(section->id_key, key)) {
536− mmc->section_data[tfc->level].section_id = av_strdup(str);
563+ set_str(&mmc->section_data[tfc->level].section_id, str);
537564 exit = 1;
538565 }
539566
540567 if (section->dest_id_key && !strcmp(section->dest_id_key, key)) {
541− mmc->section_data[tfc->level].dest_id = av_strdup(str);
568+ set_str(&mmc->section_data[tfc->level].dest_id, str);
542569 exit = 1;
543570 }
544571
545572 if (section->src_id_key && !strcmp(section->src_id_key, key)) {
546− mmc->section_data[tfc->level].src_id = av_strdup(str);
573+ set_str(&mmc->section_data[tfc->level].src_id, str);
547574 exit = 1;
548575 }
549576
@@ -636,6 +663,7 @@ const AVTextFormatter avtextformatter_mermaid = { · 86 unmodified lines
636663 .name = "mermaid",
637664 .priv_size = sizeof(MermaidContext),
638665 .init = mermaid_init,
666+ .uninit = mermaid_uninit,
639667 .print_section_header = mermaid_print_section_header,
640668 .print_section_footer = mermaid_print_section_footer,
641669 .print_integer = mermaid_print_int,
@@ -649,6 +677,7 @@ const AVTextFormatter avtextformatter_mermaidhtml = { · 7 unmodified lines
649677 .name = "mermaidhtml",
650678 .priv_size = sizeof(MermaidContext),
651679 .init = mermaid_init_html,
680+ .uninit = mermaid_uninit,
652681 .print_section_header = mermaid_print_section_header,
653682 .print_section_footer = mermaid_print_section_footer,
654683 .print_integer = mermaid_print_int,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t