Add some additional safety checks to the StructuredData access
authorJason Molenda <jmolenda@apple.com>
Wed, 9 Dec 2015 04:15:47 +0000 (04:15 +0000)
committerJason Molenda <jmolenda@apple.com>
Wed, 9 Dec 2015 04:15:47 +0000 (04:15 +0000)
methods - lldb can still crash pretty easily on corrupt JSON text,
and these will help eliminate a bunch of cases where that would
result in a crash.  Some of the methods would check that e.g.
GetItemAtIndex would actually return an item before dereferencing it,
some would not, that kind of thing.

<rdar://problem/23768693>

llvm-svn: 255093

lldb/include/lldb/Core/StructuredData.h

index 90d8200..52f3452 100644 (file)
@@ -250,11 +250,14 @@ public:
         bool
         GetItemAtIndexAsInteger(size_t idx, IntType &result) const
         {
-            ObjectSP value = GetItemAtIndex(idx);
-            if (auto int_value = value->GetAsInteger())
+            ObjectSP value_sp = GetItemAtIndex(idx);
+            if (value_sp.get())
             {
-                result = static_cast<IntType>(int_value->GetValue());
-                return true;
+                if (auto int_value = value_sp->GetAsInteger())
+                {
+                    result = static_cast<IntType>(int_value->GetValue());
+                    return true;
+                }
             }
             return false;
         }
@@ -272,11 +275,14 @@ public:
         bool
         GetItemAtIndexAsString(size_t idx, std::string &result) const
         {
-            ObjectSP value = GetItemAtIndex(idx);
-            if (auto string_value = value->GetAsString())
+            ObjectSP value_sp = GetItemAtIndex(idx);
+            if (value_sp.get())
             {
-                result = string_value->GetValue();
-                return true;
+                if (auto string_value = value_sp->GetAsString())
+                {
+                    result = string_value->GetValue();
+                    return true;
+                }
             }
             return false;
         }
@@ -293,13 +299,13 @@ public:
         bool
         GetItemAtIndexAsString(size_t idx, ConstString &result) const
         {
-            ObjectSP value = GetItemAtIndex(idx);
-            if (!value)
-                return false;
-            if (auto string_value = value->GetAsString())
-            {
-                result = ConstString(string_value->GetValue());
-                return true;
+            ObjectSP value_sp = GetItemAtIndex(idx);
+            if (value_sp.get()) {
+                if (auto string_value = value_sp->GetAsString())
+                {
+                    result = ConstString(string_value->GetValue());
+                    return true;
+                }
             }
             return false;
         }
@@ -316,17 +322,27 @@ public:
         bool
         GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const
         {
-            ObjectSP value = GetItemAtIndex(idx);
-            result = value->GetAsDictionary();
-            return (result != nullptr);
+            result = nullptr;
+            ObjectSP value_sp = GetItemAtIndex(idx);
+            if (value_sp.get()) 
+            {
+                result = value_sp->GetAsDictionary();
+                return (result != nullptr);
+            }
+            return false;
         }
 
         bool
         GetItemAtIndexAsArray(size_t idx, Array *&result) const
         {
-            ObjectSP value = GetItemAtIndex(idx);
-            result = value->GetAsArray();
-            return (result != nullptr);
+            result = nullptr;
+            ObjectSP value_sp = GetItemAtIndex(idx);
+            if (value_sp.get())
+            {
+                result = value_sp->GetAsArray();
+                return (result != nullptr);
+            }
+            return false;
         }
 
         void
@@ -542,13 +558,13 @@ public:
         bool
         GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
         {
-            ObjectSP value = GetValueForKey(key);
-            if (!value)
-                return false;
-            if (auto int_value = value->GetAsInteger())
-            {
-                result = static_cast<IntType>(int_value->GetValue());
-                return true;
+            ObjectSP value_sp = GetValueForKey(key);
+            if (value_sp) {
+                if (auto int_value = value_sp->GetAsInteger())
+                {
+                    result = static_cast<IntType>(int_value->GetValue());
+                    return true;
+                }
             }
             return false;
         }
@@ -566,13 +582,14 @@ public:
         bool
         GetValueForKeyAsString(llvm::StringRef key, std::string &result) const
         {
-            ObjectSP value = GetValueForKey(key);
-            if (!value)
-                return false;
-            if (auto string_value = value->GetAsString())
+            ObjectSP value_sp = GetValueForKey(key);
+            if (value_sp.get())
             {
-                result = string_value->GetValue();
-                return true;
+                if (auto string_value = value_sp->GetAsString())
+                {
+                    result = string_value->GetValue();
+                    return true;
+                }
             }
             return false;
         }
@@ -594,13 +611,14 @@ public:
         bool
         GetValueForKeyAsString(llvm::StringRef key, ConstString &result) const
         {
-            ObjectSP value = GetValueForKey(key);
-            if (!value)
-                return false;
-            if (auto string_value = value->GetAsString())
+            ObjectSP value_sp = GetValueForKey(key);
+            if (value_sp.get())
             {
-                result = ConstString(string_value->GetValue());
-                return true;
+                if (auto string_value = value_sp->GetAsString())
+                {
+                    result = ConstString(string_value->GetValue());
+                    return true;
+                }
             }
             return false;
         }
@@ -618,22 +636,26 @@ public:
         GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
         {
             result = nullptr;
-            ObjectSP value = GetValueForKey(key);
-            if (!value)
-                return false;
-            result = value->GetAsDictionary();
-            return true;
+            ObjectSP value_sp = GetValueForKey(key);
+            if (value_sp.get())
+            {
+                result = value_sp->GetAsDictionary();
+                return (result != nullptr);
+            }
+            return false;
         }
 
         bool
         GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
         {
             result = nullptr;
-            ObjectSP value = GetValueForKey(key);
-            if (!value)
-                return false;
-            result = value->GetAsArray();
-            return true;
+            ObjectSP value_sp = GetValueForKey(key);
+            if (value_sp.get())
+            {
+                result = value_sp->GetAsArray();
+                return (result != nullptr);
+            }
+            return false;
         }
 
         bool
@@ -645,10 +667,10 @@ public:
         }
 
         void
-        AddItem (llvm::StringRef key, ObjectSP value)
+        AddItem (llvm::StringRef key, ObjectSP value_sp)
         {
             ConstString key_cs(key);
-            m_dict[key_cs] = value;
+            m_dict[key_cs] = value_sp;
         }
 
         void