asmith created this revision.

Check that a regex is valid before searching by regex for a symbol in a pdb.
This avoids throwing an exception on an invalid regex.


Repository:
  rL LLVM

https://reviews.llvm.org/D41086

Files:
  source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
  unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp

Index: unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp
===================================================================
--- unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp
+++ unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp
@@ -161,8 +161,7 @@
 
 TEST_F(SymbolFilePDBTests, TestResolveSymbolContextBasename) {
   // Test that attempting to call ResolveSymbolContext with only a basename
-  // finds all full paths
-  // with the same basename
+  // finds all full paths with the same basename
   FileSpec fspec(m_pdb_test_exe.c_str(), false);
   ArchSpec aspec("i686-pc-windows");
   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
@@ -181,8 +180,7 @@
 
 TEST_F(SymbolFilePDBTests, TestResolveSymbolContextFullPath) {
   // Test that attempting to call ResolveSymbolContext with a full path only
-  // finds the one source
-  // file that matches the full path.
+  // finds the one source file that matches the full path.
   FileSpec fspec(m_pdb_test_exe.c_str(), false);
   ArchSpec aspec("i686-pc-windows");
   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
@@ -203,10 +201,9 @@
 
 TEST_F(SymbolFilePDBTests,
        TestLookupOfHeaderFileWithInlines) {
-  // Test that when looking up a header file via ResolveSymbolContext (i.e. a
-  // file that was not by itself
-  // compiled, but only contributes to the combined code of other source files),
-  // a SymbolContext is returned
+  // Test that when looking up a header file via ResolveSymbolContext
+  // (i.e. a file that was not by itself compiled, but only contributes
+  // to the combined code of other source files), a SymbolContext is returned
   // for each compiland which has line contributions from the requested header.
   FileSpec fspec(m_pdb_test_exe.c_str(), false);
   ArchSpec aspec("i686-pc-windows");
@@ -231,10 +228,9 @@
 }
 
 TEST_F(SymbolFilePDBTests, TestLookupOfHeaderFileWithNoInlines) {
-  // Test that when looking up a header file via ResolveSymbolContext (i.e. a
-  // file that was not by itself
-  // compiled, but only contributes to the combined code of other source files),
-  // that if check_inlines
+  // Test that when looking up a header file via ResolveSymbolContext
+  // (i.e. a file that was not by itself compiled, but only contributes
+  // to the combined code of other source files), that if check_inlines
   // is false, no SymbolContexts are returned.
   FileSpec fspec(m_pdb_test_exe.c_str(), false);
   ArchSpec aspec("i686-pc-windows");
@@ -256,8 +252,7 @@
 
 TEST_F(SymbolFilePDBTests, TestLineTablesMatchAll) {
   // Test that when calling ResolveSymbolContext with a line number of 0, all
-  // line entries from
-  // the specified files are returned.
+  // line entries from the specified files are returned.
   FileSpec fspec(m_pdb_test_exe.c_str(), false);
   ArchSpec aspec("i686-pc-windows");
   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
@@ -305,8 +300,7 @@
 
 TEST_F(SymbolFilePDBTests, TestLineTablesMatchSpecific) {
   // Test that when calling ResolveSymbolContext with a specific line number,
-  // only line entries
-  // which match the requested line are returned.
+  // only line entries which match the requested line are returned.
   FileSpec fspec(m_pdb_test_exe.c_str(), false);
   ArchSpec aspec("i686-pc-windows");
   lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);
@@ -459,16 +453,14 @@
 
 TEST_F(SymbolFilePDBTests, TestArrayTypes) {
   // In order to get this test working, we need to support lookup by symbol
-  // name.  Because array
-  // types themselves do not have names, only the symbols have names (i.e. the
-  // name of the array).
+  // name.  Because array types themselves do not have names, only the
+  // symbols have names (i.e. the name of the array).
 }
 
 TEST_F(SymbolFilePDBTests, TestFunctionTypes) {
   // In order to get this test working, we need to support lookup by symbol
-  // name.  Because array
-  // types themselves do not have names, only the symbols have names (i.e. the
-  // name of the array).
+  // name.  Because array types themselves do not have names, only the
+  // symbols have names (i.e. the name of the array).
 }
 
 TEST_F(SymbolFilePDBTests, TestTypedefs) {
@@ -520,6 +512,13 @@
                                             false, 0, searched_files, results);
   EXPECT_GT(num_results, 1u);
   EXPECT_EQ(num_results, results.GetSize());
+
+  // We expect no exception thrown if the given regex can't be compiled
+  results.Clear();
+  num_results = symfile->FindTypes(sc, ConstString("**"), nullptr,
+                                   false, 0, searched_files, results);
+  EXPECT_EQ(num_results, 0u);
+  EXPECT_EQ(num_results, results.GetSize());
 }
 
 TEST_F(SymbolFilePDBTests, TestMaxMatches) {
@@ -538,8 +537,7 @@
   // Try to limit ourselves from 1 to 10 results, otherwise we could be doing
   // this thousands of times.
   // The idea is just to make sure that for a variety of values, the number of
-  // limited results always
-  // comes out to the number we are expecting.
+  // limited results always comes out to the number we are expecting.
   uint32_t iterations = std::min(num_results, 10u);
   for (uint32_t i = 1; i <= iterations; ++i) {
     uint32_t num_limited_results = symfile->FindTypes(
Index: source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
===================================================================
--- source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
+++ source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
@@ -19,6 +19,7 @@
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Symbol/SymbolContext.h"
 #include "lldb/Symbol/TypeMap.h"
+#include "lldb/Utility/RegularExpression.h"
 
 #include "llvm/DebugInfo/PDB/GenericError.h"
 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
@@ -388,7 +389,14 @@
   // If this might be a regex, we have to return EVERY symbol and process them
   // one by one, which is going to destroy performance on large PDB files.  So
   // try really hard not to use a regex match.
-  if (name_str.find_first_of("[]?*.-+\\") != std::string::npos)
+  bool is_regex = false;
+  if (name_str.find_first_of("[]?*.-+\\") != std::string::npos) {
+    // Trying to compile an invalid regex could throw an exception.
+    // Only search by regex when it's valid.
+    lldb_private::RegularExpression name_regex(name_str);
+    is_regex = name_regex.IsValid();
+  }
+  if (is_regex)
     FindTypesByRegex(name_str, max_matches, types);
   else
     FindTypesByName(name_str, max_matches, types);
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to