]> git.ipfire.org Git - thirdparty/elfutils.git/commit
elfcompress: Handle shstrtab/symtab names more consistently main
authorMark Wielaard <mark@klomp.org>
Tue, 26 May 2026 16:18:25 +0000 (18:18 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 28 May 2026 13:35:31 +0000 (15:35 +0200)
commita787c560dc3867a464e67a5c5a741739f9820470
treef4e2e87c54507d412f83c438912432c5ac2848a4
parentf3f59252b8c27cf1c35f6f59e1215c256d9edb44
elfcompress: Handle shstrtab/symtab names more consistently

We track the shstrtab and symtab section names in case they need to be
renamed. These names are sometimes assigned a static string (from the
shstrtab string pool), sometimes created as a copy of an existing
string name and sometimes created by constructing a new string. This
means they might leak because they are never freed.

Fix this leak by defining the variables early (as NULL) before looping
over all sections, making sure to always assign a allocated (copy) of
the (new) name and explicitly free them at cleanup (run both on
success and failure).

This makes it easier to reason about allocation status and string
"ownership" making it easy to see there are no memory issues even in
the rare case of a .[z]debug section being used as a symtab or
shstrtab table (which is why there is no testcase, because it is
questionable whether such a file is actually valid).

* src/elfcompress.c (process_file): Define shstrtab_name,
shstrtab_newname, symtab_name and symtab_newname
early. xstrdup shstrtab_name and symtab_name if they were
assigned to a static string. Free all four strings at
cleanup.

Signed-off-by: Mark Wielaard <mark@klomp.org>
src/elfcompress.c