[Lldb-commits] [lldb] r255093 - Add some additional safety checks to the StructuredData access

Jason Molenda via lldb-commits lldb-commits at lists.llvm.org
Tue Dec 8 20:15:47 PST 2015


Author: jmolenda
Date: Tue Dec  8 22:15:47 2015
New Revision: 255093

URL: http://llvm.org/viewvc/llvm-project?rev=255093&view=rev
Log:
Add some additional safety checks to the StructuredData access
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> 

Modified:
    lldb/trunk/include/lldb/Core/StructuredData.h

Modified: lldb/trunk/include/lldb/Core/StructuredData.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/StructuredData.h?rev=255093&r1=255092&r2=255093&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/StructuredData.h (original)
+++ lldb/trunk/include/lldb/Core/StructuredData.h Tue Dec  8 22:15:47 2015
@@ -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




More information about the lldb-commits mailing list