about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorDan Liew <daniel.liew@imperial.ac.uk>2015-12-18 14:01:55 +0000
committerDan Liew <daniel.liew@imperial.ac.uk>2015-12-18 14:04:19 +0000
commitf5ff9cbb0e7d5c34569b3d819823751be4ffcf34 (patch)
treea060123f950c4555c5aa69924eb033386f37716d
parentd348aee430c8308a29d5edadd4c38e7fee1abadc (diff)
downloadklee-f5ff9cbb0e7d5c34569b3d819823751be4ffcf34.tar.gz
Fix a leak detected by ASan in the KQuery parser where on destruction of
the ``ParserImpl`` it wouldn't free allocated ``Identifier``s
-rw-r--r--lib/Expr/Parser.cpp32
1 files changed, 32 insertions, 0 deletions
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<const Identifier*> 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.