Don't dereference an invalid pointer if string is empty.
authorAnton Korobeynikov <asl@math.spbu.ru>
Tue, 5 Feb 2008 23:34:40 +0000 (23:34 +0000)
committerAnton Korobeynikov <asl@math.spbu.ru>
Tue, 5 Feb 2008 23:34:40 +0000 (23:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46781 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringMap.h

index dd040f927db9e10f36519f37da2d0b7ce3e96296..27ea5d3ea1b34bbaafab8e9d6db7afc841ce5e88 100644 (file)
@@ -273,7 +273,7 @@ public:
     return find(Key, Key + strlen(Key));
   }
   iterator find(const std::string &Key) {
     return find(Key, Key + strlen(Key));
   }
   iterator find(const std::string &Key) {
-    const char* key_start = &Key[0];
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
     return find(key_start, key_start + Key.size());
   }
 
     return find(key_start, key_start + Key.size());
   }
 
@@ -286,7 +286,7 @@ public:
     return find(Key, Key + strlen(Key));
   }
   const_iterator find(const std::string &Key) const {
     return find(Key, Key + strlen(Key));
   }
   const_iterator find(const std::string &Key) const {
-    const char* key_start = &Key[0];
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
     return find(key_start, key_start + Key.size());
   }
 
     return find(key_start, key_start + Key.size());
   }
 
@@ -295,7 +295,7 @@ public:
     return entry.getValue();
   }
   ValueTy& operator[](const std::string &Key) {
     return entry.getValue();
   }
   ValueTy& operator[](const std::string &Key) {
-    const char* key_start = &Key[0];
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
     value_type& entry = GetOrCreateValue(key_start, key_start + Key.size());
     return entry.getValue();
   }
     value_type& entry = GetOrCreateValue(key_start, key_start + Key.size());
     return entry.getValue();
   }
@@ -307,7 +307,7 @@ public:
     return count(Key, Key + strlen(Key));
   }
   size_type count(const std::string &Key) const {
     return count(Key, Key + strlen(Key));
   }
   size_type count(const std::string &Key) const {
-    const char* key_start = &Key[0];
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
     return count(key_start, key_start + Key.size());
   }
 
     return count(key_start, key_start + Key.size());
   }