]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Do not use boost::format, it is not thread safe when calling into locale handling...
authorOtto <otto.moerbeek@open-xchange.com>
Wed, 22 Dec 2021 09:00:47 +0000 (10:00 +0100)
committerOtto <otto.moerbeek@open-xchange.com>
Wed, 22 Dec 2021 10:08:50 +0000 (11:08 +0100)
according to tsan.

pdns/syncres.cc

index 1e4ab0a4bca0a269a311a8b1b8f6541f931e8557..38fdb655366239c58bdab4a0694dbabfd0d1a1f8 100644 (file)
@@ -104,6 +104,19 @@ int SyncRes::s_event_trace_enabled;
 
 #define LOG(x) if(d_lm == Log) { g_log <<Logger::Warning << x; } else if(d_lm == Store) { d_trace << x; }
 
+// A helper function to print a double with specific precsision
+// Not using boost::format since it is not thread safe while calling into locale handling code according to tsan
+// This allocates a string, but that's nothing compared to what boost::format is doing
+static inline std::string fmtfloat(const char* fmt, double f)
+{
+  char buf[20];
+  int ret = snprintf(buf, sizeof(buf), fmt, f);
+  if (ret < 0 || ret >= static_cast<int>(sizeof(buf))) {
+    return "?";
+  }
+  return std::string(buf, strlen(buf));
+}
+
 static inline void accountAuthLatency(uint64_t usec, int family)
 {
   if (family == AF_INET) {
@@ -1224,7 +1237,7 @@ vector<ComboAddress> SyncRes::getAddrs(const DNSName &qname, unsigned int depth,
       else {
         LOG(", ");
       }
-      LOG((addr.toString())<<"(" << (boost::format("%0.2f") % (speeds[addr]/1000.0)).str() <<"ms)");
+      LOG((addr.toString())<<"(" << fmtfloat("%0.2f", speeds[addr]/1000.0) <<"ms)");
     }
     LOG(endl);
   }
@@ -2023,7 +2036,7 @@ inline std::vector<std::pair<DNSName, float>> SyncRes::shuffleInSpeedOrder(NsSet
           LOG(endl<<prefix<<"             ");
         }
       }
-      LOG(i->first.toLogString()<<"(" << (boost::format("%0.2f") % (i->second/1000.0)).str() <<"ms)");
+      LOG(i->first.toLogString()<<"(" << fmtfloat("%0.2f", i->second/1000.0) <<"ms)");
     }
     LOG(endl);
   }
@@ -2054,7 +2067,7 @@ inline vector<ComboAddress> SyncRes::shuffleForwardSpeed(const vector<ComboAddre
           LOG(endl<<prefix<<"             ");
         }
       }
-      LOG((wasRd ? string("+") : string("-")) << i->toStringWithPort() <<"(" << (boost::format("%0.2f") % (speeds[*i]/1000.0)).str() <<"ms)");
+      LOG((wasRd ? string("+") : string("-")) << i->toStringWithPort() <<"(" << fmtfloat("%0.2f", speeds[*i]/1000.0) <<"ms)");
     }
     LOG(endl);
   }