stats: ensure that stat names are valid
authorNathan Binkert <nate@binkert.org>
Thu, 21 Apr 2011 02:07:46 +0000 (19:07 -0700)
committerNathan Binkert <nate@binkert.org>
Thu, 21 Apr 2011 02:07:46 +0000 (19:07 -0700)
src/base/statistics.cc

index 8bbc4bf36c335a6b38329262a0fee431183c3aca..f1a368f47ed9af5913343cd95eee3836175f9b57 100644 (file)
@@ -138,9 +138,43 @@ Info::~Info()
 {
 }
 
+bool
+validateStatName(const string &name)
+{
+    if (name.empty())
+        return false;
+
+    vector<string> vec;
+    tokenize(vec, name, '.');
+    vector<string>::const_iterator item = vec.begin();
+    while (item != vec.end()) {
+        if (item->empty())
+            return false;
+
+        string::const_iterator c = item->begin();
+
+        // The first character is different
+        if (!isalpha(*c) && *c != '_')
+            return false;
+
+        // The rest of the characters have different rules.
+        while (++c != item->end()) {
+            if (!isalnum(*c) && *c != '_')
+                return false;
+        }
+
+        ++item;
+    }
+
+    return true;
+}
+
 void
 Info::setName(const string &name)
 {
+    if (!validateStatName(name))
+        panic("invalid stat name '%s'", name);
+
     pair<NameMapType::iterator, bool> p =
         nameMap().insert(make_pair(name, this));