[LyX/master] InsetIndex: add a missing destructor for IndexNode and call it from xhtml() to avoid leaking resources.

Thibaut Cuvelier tcuvelier at lyx.org
Sun Jan 8 20:05:23 UTC 2023


commit 6e4cf808da4ea846d0f5195d0d684daf4912af43
Author: Thibaut Cuvelier <tcuvelier at lyx.org>
Date:   Sun Jan 8 22:03:26 2023 +0100

    InsetIndex: add a missing destructor for IndexNode and call it from xhtml() to avoid leaking resources.
    
    Corresponding Coverity finding:
    
    ** CID 382778:  Resource leaks  (RESOURCE_LEAK)
    /home/lasgoutt/src/lyx/coverity/lyx/src/insets/InsetIndex.cpp: 1909 in _ZNK3lyx15InsetPrintIndex5xhtmlB5cxx11ERNS_9XMLStreamERKNS_12OutputParamsE()
    
    ________________________________________________________________________________________________________
    *** CID 382778:  Resource leaks  (RESOURCE_LEAK)
    /home/lasgoutt/src/lyx/coverity/lyx/src/insets/InsetIndex.cpp: 1909 in _ZNK3lyx15InsetPrintIndex5xhtmlB5cxx11ERNS_9XMLStreamERKNS_12OutputParamsE()
    1903            }
    1904
    1905            xs << xml::EndTag("ul");
    1906            xs << xml::CR();
    1907            xs << xml::EndTag("div");
    1908
    >>>     CID 382778:  Resource leaks  (RESOURCE_LEAK)
    >>>     Variable "index_root" going out of scope leaks the storage it points to.
    1909            return ods.str();
    1910     }
    1911
---
 src/insets/InsetIndex.cpp |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp
index a6fcae7..32cabca 100644
--- a/src/insets/InsetIndex.cpp
+++ b/src/insets/InsetIndex.cpp
@@ -1658,6 +1658,14 @@ std::string generateCssClassAtDepth(unsigned depth) {
 struct IndexNode {
 	std::vector<IndexEntry> entries;
 	std::vector<IndexNode*> children;
+
+	~IndexNode() {
+		for (IndexNode * child : children) {
+			if (!child)
+				continue;
+			delete child;
+		}
+	}
 };
 
 docstring termAtLevel(const IndexNode* node, unsigned depth)
@@ -1901,6 +1909,7 @@ docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
 	for (const IndexNode* node : index_root->children) {
 		outputIndexPage(xs, node);
 	}
+	delete index_root;
 
 	xs << xml::EndTag("ul");
 	xs << xml::CR();


More information about the lyx-cvs mailing list