From f5ff9cbb0e7d5c34569b3d819823751be4ffcf34 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Fri, 18 Dec 2015 14:01:55 +0000 Subject: Fix a leak detected by ASan in the KQuery parser where on destruction of the ``ParserImpl`` it wouldn't free allocated ``Identifier``s --- lib/Expr/Parser.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/Expr/Parser.cpp b/lib/Expr/Parser.cpp index 854f6d52..e914cb80 100644 --- a/lib/Expr/Parser.cpp +++ b/lib/Expr/Parser.cpp @@ -331,6 +331,8 @@ namespace { MaxErrors(~0u), NumErrors(0) {} + virtual ~ParserImpl(); + /// Initialize - Initialize the parsing state. This must be called /// prior to the start of parsing. void Initialize() { @@ -1561,6 +1563,36 @@ void ParserImpl::Error(const char *Message, const Token &At) { llvm::errs() << '\n'; } +ParserImpl::~ParserImpl() { + // Free identifiers + // + // Note the Identifiers are not disjoint across the symbol + // tables so we need to keep track of what has freed to + // avoid doing a double free. + std::set freedNodes; + for (IdentifierTabTy::iterator pi = IdentifierTab.begin(), + pe = IdentifierTab.end(); + pi != pe; ++pi) { + const Identifier* id = pi->second; + if (freedNodes.insert(id).second) + delete id; + } + for (ExprSymTabTy::iterator pi = ExprSymTab.begin(), + pe = ExprSymTab.end(); + pi != pe; ++pi) { + const Identifier* id = pi->first; + if (freedNodes.insert(id).second) + delete id; + } + for (VersionSymTabTy::iterator pi = VersionSymTab.begin(), + pe = VersionSymTab.end(); + pi != pe; ++pi) { + const Identifier* id = pi->first; + if (freedNodes.insert(id).second) + delete id; + } +} + // AST API // FIXME: Move out of parser. -- cgit 1.4.1