Commit 5f036ed0

5f036ed0f319Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(parse): free allocations on error-path exits (#764)

main()'s two error-exit paths (output-fopen failure + parse-error list) returned without freeing the work-in-progress allocations:

  • fopen-failure exit: leaked source, g_source_file_escaped, the lines[] array, sp_included_paths, plus the live parser + root.
  • parse-error exit: freed source + parser but skipped sp_includes_free.

Add the missing free / destroy calls on both paths. The process exits 1 immediately, so the leak is technically benign in practice -- but it's good citizenship for tooling that invokes spinel many times in a process (test harnesses, dev loops).

Closes #764

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
5f036ed0f319
Parent
d462a8ff2417

spinel_parse.cmodifiedView file

@@ -1750,9 +1750,12 @@
17501750 for (diag = (pm_diagnostic_t *)parser.error_list.head; diag; diag = (pm_diagnostic_t *)diag->node.next) {
17511751 fprintf(stderr, " %s\n", diag->message);
17521752 }
1753+ /* Issue #764: free the registered include paths on the parse-
1754+ error exit path too. */
17531755 pm_node_destroy(&parser, root);
17541756 pm_parser_free(&parser);
17551757 free(source);
1758+ sp_includes_free();
17561759 return 1;
17571760 }
17581761
@@ -1774,6 +1777,18 @@
17741777 out = fopen(argv[2], "wb");
17751778 if (!out) {
17761779 fprintf(stderr, "spinel_parse: cannot write '%s'\n", argv[2]);
1780+ /* Issue #764: clean up before bailing on the output-open
1781+ failure. We've allocated source, g_source_file_escaped,
1782+ lines[], plus parser/root, and registered sp_included_paths
1783+ entries -- free all to avoid leaking on the way out. */
1784+ for (int i = 0; i < line_count; i++) free(lines[i]);
1785+ free(lines);
1786+ pm_node_destroy(&parser, root);
1787+ pm_parser_free(&parser);
1788+ free(source);
1789+ free(g_source_file_escaped);
1790+ g_source_file_escaped = NULL;
1791+ sp_includes_free();
17771792 return 1;
17781793 }
17791794 }