Skip to content

epub: free the ncx toc attribute string in get_toc_file_name#722

Open
maxheise wants to merge 1 commit into
linuxmint:masterfrom
maxheise:epub-free-toc-ncx-attribute
Open

epub: free the ncx toc attribute string in get_toc_file_name#722
maxheise wants to merge 1 commit into
linuxmint:masterfrom
maxheise:epub-free-toc-ncx-attribute

Conversation

@maxheise

Copy link
Copy Markdown

Hello,

This frees a small string that get_toc_file_name() in the EPUB backend
leaks on its success path.

get_toc_file_name() reads the spine's "toc" attribute with
xml_get_data_from_node(spine, XML_ATTRIBUTE, ...), which calls
xmlGetProp() and returns a freshly allocated copy that the caller must
release with xmlFree(). The value (ncx) is used once, to match the
manifest item id, and is then never freed: the success path returns the
toc file name without releasing ncx. (On the ncx == NULL path no ncx
string was allocated.) get_toc_file_name() runs once per load from
setup_document_index() for documents that carry an NCX toc, so the leak
happens on ordinary loads of such documents.

Free ncx with xmlFree() right after its last use. xmlFree() matches the
xmlGetProp() allocation.

Best regards,
Max

get_toc_file_name() in backend/epub/epub-document.c reads the spine's
"toc" attribute into a local xmlChar *ncx:

    xmlChar *ncx = xml_get_data_from_node(spine, XML_ATTRIBUTE,
                                          (xmlChar*)"toc");

xml_get_data_from_node() in XML_ATTRIBUTE mode calls xmlGetProp(), which
returns a freshly allocated copy of the attribute value that the caller
owns and must release with xmlFree(). The value is used once, as the id
to match in

    xml_parse_children_of_node(manifest, (xmlChar*)"item",
                               (xmlChar*)"id", ncx);

and is then never freed: the success path returns tocfilename without
releasing ncx, so the copy is leaked. (On the ncx == NULL path no ncx
string was allocated.) get_toc_file_name() is called once per load from
setup_document_index() for documents that carry an NCX toc, so the leak
happens on ordinary loads of such documents.

Free ncx with xmlFree() immediately after its last use. xmlFree() is the
deallocator that matches xmlGetProp().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant