From: Peter van Dijk Date: Fri, 21 Aug 2020 09:13:47 +0000 (+0200) Subject: statbag: prevent re-declaration in production coded X-Git-Tag: rec-4.4.0-beta1~10^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F9401%2Fhead;p=thirdparty%2Fpdns.git statbag: prevent re-declaration in production coded --- diff --git a/pdns/receiver.cc b/pdns/receiver.cc index 3be7c8c1e0..6caf96898d 100644 --- a/pdns/receiver.cc +++ b/pdns/receiver.cc @@ -636,7 +636,13 @@ int main(int argc, char **argv) exit(1); } - declareStats(); + try { + declareStats(); + } + catch(PDNSException &PE) { + g_log<(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; diff --git a/pdns/statbag.hh b/pdns/statbag.hh index 1887b7833a..611712c223 100644 --- a/pdns/statbag.hh +++ b/pdns/statbag.hh @@ -80,6 +80,7 @@ class StatBag typedef map funcstats_t; funcstats_t d_funcstats; bool d_doRings; + std::set 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) diff --git a/pdns/testrunner.cc b/pdns/testrunner.cc index ea38f33a8f..7db0337ae2 100644 --- a/pdns/testrunner.cc +++ b/pdns/testrunner.cc @@ -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 ); }