[Coverity] Out-of-bounds read (OVERRUN)
authorPeter D. Barnes, Jr. <barnes26@llnl.gov>
Tue, 28 May 2013 17:29:48 -0700
changeset 9876 c84d748a959f
parent 9875 7d31acb96ea3
child 9877 7511dc1f8ae8
[Coverity] Out-of-bounds read (OVERRUN)
src/core/model/test.cc
--- a/src/core/model/test.cc	Tue May 28 16:53:09 2013 -0700
+++ b/src/core/model/test.cc	Tue May 28 17:29:48 2013 -0700
@@ -484,22 +484,30 @@
 TestRunnerImpl::ReplaceXmlSpecialCharacters (std::string xml) const
 {
   NS_LOG_FUNCTION (this << xml);
-  std::string specials = "<>&\"'";
-  std::string replacements[] = {"&lt;", "&gt;", "&amp;", "&#39;", "&quot;"};
+  typedef std::map <char, std::string> specials_map;
+  specials_map specials;
+  specials['<'] = "&lt;";
+  specials['>'] = "&gt;";
+  specials['&'] = "&amp;";
+  specials['"'] = "&#39;";
+  specials['\''] = "&quot;";
+
   std::string result;
-  std::size_t index, length = xml.length ();
+  std::size_t length = xml.length ();
 
   for (size_t i = 0; i < length; ++i)
     {
       char character = xml[i];
 
-      if ((index = specials.find (character)) == std::string::npos)
+      specials_map::const_iterator it = specials.find (character);
+
+      if (it == specials.end ())
         {
           result.push_back (character);
         }
       else
         {
-          result += replacements[index];
+          result += it->second;
         }
     }
   return result;