]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
statbag: prevent re-declaration in production coded 9401/head
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Fri, 21 Aug 2020 09:13:47 +0000 (11:13 +0200)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Fri, 21 Aug 2020 09:13:47 +0000 (11:13 +0200)
pdns/receiver.cc
pdns/statbag.cc
pdns/statbag.hh
pdns/testrunner.cc

index 3be7c8c1e0c75855cb56278f688cefef1e819de6..6caf96898d12eeca3b0c40926bccb1167330c08a 100644 (file)
@@ -636,7 +636,13 @@ int main(int argc, char **argv)
     exit(1);
   }
   
-  declareStats();
+  try {
+    declareStats();
+  }
+  catch(PDNSException &PE) {
+    g_log<<Logger::Error<<"Exiting because: "<<PE.reason<<endl;
+    exit(1);
+  }
   S.blacklist("special-memory-usage");
 
   DLOG(g_log<<Logger::Warning<<"Verbose logging in effect"<<endl);
index 0876b2732398adaeb72b230c001f099c72aa5979..521530d6a2661a53136e0da13e3ec1367bd927e1 100644 (file)
@@ -38,6 +38,7 @@
 StatBag::StatBag()
 {
   d_doRings=false;
+  d_allowRedeclare=false;
 }
 
 void StatBag::exists(const string &key)
@@ -106,8 +107,13 @@ StatType StatBag::getStatType(const string &item)
 void StatBag::declare(const string &key, const string &descrip, StatType statType)
 {
   if(d_stats.count(key)) {
-    *d_stats[key] = 0;
-    return;
+    if (d_allowRedeclare) {
+      *d_stats[key] = 0;
+      return;
+    }
+    else {
+      throw PDNSException("Attempt to re-declare statbag '"+key+"'");
+    }
   }
 
   auto i=make_unique<AtomicCounter>(0);
@@ -118,6 +124,10 @@ void StatBag::declare(const string &key, const string &descrip, StatType statTyp
 
 void StatBag::declare(const string &key, const string &descrip, StatBag::func_t func, StatType statType)
 {
+  if(d_funcstats.count(key) && !d_allowRedeclare) {
+    throw PDNSException("Attempt to re-declare func statbag '"+key+"'");
+  }
+
   d_funcstats[key]=func;
   d_keyDescrips[key]=descrip;
   d_statTypes[key]=statType;
index 1887b7833a4cc5c329b7f5599a7260a721fd6d21..611712c223a8bfa6ab05c3b48dc83be0eecc3aa7 100644 (file)
@@ -80,6 +80,7 @@ class StatBag
   typedef map<string, func_t> funcstats_t;
   funcstats_t d_funcstats;
   bool d_doRings;
+
   std::set<string> d_blacklist;
 
   void registerRingStats(const string& name);
@@ -147,6 +148,8 @@ public:
   string getValueStr(const string &key); //!< read a value behind a key, and return it as a string
   string getValueStrZero(const string &key); //!< read a value behind a key, and return it as a string, and zero afterwards
   void blacklist(const string &str);
+
+  bool d_allowRedeclare; // only set this true during tests, never in production code
 };
 
 inline void StatBag::deposit(const string &key, int value)
index ea38f33a8fad6368ba996dda60bc88b24ded73e0..7db0337ae21f382349cef12d30934fbd9fa5f434 100644 (file)
@@ -27,5 +27,6 @@ static bool init_unit_test() {
 // entry point:
 int main(int argc, char* argv[])
 {
+  S.d_allowRedeclare = true;
   return boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
 }