[Lldb-commits] [lldb] r213778 - Make sure we don't crash if someone (E.G.) comments out on entry from g_core_definitions[] without removing the ArchSpec::Core enumeration when submitting from source.

Greg Clayton gclayton at apple.com
Wed Jul 23 11:12:06 PDT 2014


Author: gclayton
Date: Wed Jul 23 13:12:06 2014
New Revision: 213778

URL: http://llvm.org/viewvc/llvm-project?rev=213778&view=rev
Log:
Make sure we don't crash if someone (E.G.) comments out on entry from g_core_definitions[] without removing the ArchSpec::Core enumeration when submitting from source.

We now catch the issue with a static_assert() at compile time and use llvm::array_lengthof(g_core_definitions) as well.

<rdar://problem/17767541>


Modified:
    lldb/trunk/source/Core/ArchSpec.cpp

Modified: lldb/trunk/source/Core/ArchSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ArchSpec.cpp?rev=213778&r1=213777&r2=213778&view=diff
==============================================================================
--- lldb/trunk/source/Core/ArchSpec.cpp (original)
+++ lldb/trunk/source/Core/ArchSpec.cpp Wed Jul 23 13:12:06 2014
@@ -42,13 +42,13 @@ namespace lldb_private {
         uint32_t max_opcode_byte_size;
         llvm::Triple::ArchType machine;
         ArchSpec::Core core;
-        const char *name;
+        const char * const name;
     };
 
 }
 
 // This core information can be looked using the ArchSpec::Core as the index
-static const CoreDefinition g_core_definitions[ArchSpec::kNumCores] =
+static const CoreDefinition g_core_definitions[] =
 {
     { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm    , ArchSpec::eCore_arm_generic     , "arm"       },
     { eByteOrderLittle, 4, 2, 4, llvm::Triple::arm    , ArchSpec::eCore_arm_armv4       , "armv4"     },
@@ -118,6 +118,11 @@ static const CoreDefinition g_core_defin
     { eByteOrderLittle, 4, 1, 1 , llvm::Triple::kalimba , ArchSpec::eCore_kalimba  , "kalimba" }
 };
 
+// Ensure that we have an entry in the g_core_definitions for each core. If you comment out an entry above,
+// you will need to comment out the corresponding ArchSpec::Core enumeration.
+static_assert(llvm::array_lengthof(g_core_definitions) == ArchSpec::kNumCores, "make sure we have one core definition for each core");
+
+
 struct ArchDefinitionEntry
 {
     ArchSpec::Core core;
@@ -142,7 +147,7 @@ ArchSpec::AutoComplete (const char *name
     uint32_t i;
     if (name && name[0])
     {
-        for (i = 0; i < ArchSpec::kNumCores; ++i)
+        for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
         {
             if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
                 matches.AppendString (g_core_definitions[i].name);
@@ -150,7 +155,7 @@ ArchSpec::AutoComplete (const char *name
     }
     else
     {
-        for (i = 0; i < ArchSpec::kNumCores; ++i)
+        for (i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
             matches.AppendString (g_core_definitions[i].name);
     }
     return matches.GetSize();
@@ -312,7 +317,7 @@ FindArchDefinition (ArchitectureType arc
 static const CoreDefinition *
 FindCoreDefinition (llvm::StringRef name)
 {
-    for (unsigned int i = 0; i < ArchSpec::kNumCores; ++i)
+    for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
     {
         if (name.equals_lower(g_core_definitions[i].name))
             return &g_core_definitions[i];
@@ -323,7 +328,7 @@ FindCoreDefinition (llvm::StringRef name
 static inline const CoreDefinition *
 FindCoreDefinition (ArchSpec::Core core)
 {
-    if (core >= 0 && core < ArchSpec::kNumCores)
+    if (core >= 0 && core < llvm::array_lengthof(g_core_definitions))
         return &g_core_definitions[core];
     return NULL;
 }





More information about the lldb-commits mailing list