From 32a127d2e0c0a624d12cf9570905fec264554550 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 1 Mar 2025 16:06:32 +0000 Subject: [PATCH] Avoid the use of ConvToStr in string concatenation. This function calls INSP_FORMAT in most cases nowadays so we may as well just call that manually. --- include/modules/regex.h | 4 ++-- include/timeutils.h | 6 ++++-- src/configparser.cpp | 8 ++++---- src/configreader.cpp | 2 +- src/helperfuncs.cpp | 6 +++--- src/modules/extra/m_ssl_gnutls.cpp | 3 ++- src/modules/m_chanhistory.cpp | 4 ++-- src/modules/m_cloak_md5.cpp | 4 ++-- src/modules/m_cloak_sha256.cpp | 2 +- src/modules/m_dnsbl.cpp | 6 +++--- src/modules/m_filter.cpp | 2 +- src/modules/m_pbkdf2.cpp | 3 ++- src/modules/m_permchannels.cpp | 2 +- src/modules/m_setidle.cpp | 2 +- src/modules/m_spanningtree/capab.cpp | 2 +- src/modules/m_spanningtree/override_map.cpp | 5 ++++- src/modules/m_spanningtree/treesocket2.cpp | 8 ++++---- src/modules/m_spanningtree/utils.cpp | 2 +- src/modules/m_xline_db.cpp | 2 +- src/snomasks.cpp | 2 +- src/socket.cpp | 2 +- src/usermanager.cpp | 2 +- 22 files changed, 43 insertions(+), 36 deletions(-) diff --git a/include/modules/regex.h b/include/modules/regex.h index d3fb71807..525990ab4 100644 --- a/include/modules/regex.h +++ b/include/modules/regex.h @@ -139,7 +139,7 @@ public: * @param error The error which occurred whilst compiling the regular expression. */ Exception(const Module* mod, const std::string& regex, const std::string& error) - : ModuleException(mod, "Error in regex '" + regex + "': " + error) + : ModuleException(mod, INSP_FORMAT("Error in regex '{}': {}", regex, error)) { } @@ -150,7 +150,7 @@ public: * @param offset The offset at which the errror occurred. */ Exception(const Module* mod, const std::string& regex, const std::string& error, size_t offset) - : ModuleException(mod, "Error in regex '" + regex + "' at offset " + ConvToStr(offset) + ": " + error) + : ModuleException(mod, INSP_FORMAT("Error in regex '{}' at offset {}: {}", regex, offset, error)) { } }; diff --git a/include/timeutils.h b/include/timeutils.h index c82138030..962c1c946 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -29,9 +29,10 @@ namespace Duration * seconds. If called with this duration 33,019,565 will be returned. * * @param str A string containing a duration. + * @param base The base time to use for leap year calculation. * @return Either the number of seconds in the duration or 0 on error. */ - CoreExport unsigned long From(const std::string& str); + CoreExport unsigned long From(const std::string& str, time_t base = 0); /** Determines whether a duration string is valid. * @param str The duration string to check. @@ -54,9 +55,10 @@ namespace Duration * * @param str A string containing a duration. * @param duration The location to store the resulting duration. + * @param base The base time to use for leap year calculation. * @return True if the conversion succeeded; otherwise, false. */ - CoreExport bool TryFrom(const std::string& str, unsigned long& duration); + CoreExport bool TryFrom(const std::string& str, unsigned long& duration, time_t base = 0); } namespace Time diff --git a/src/configparser.cpp b/src/configparser.cpp index 457840b71..25f97b727 100644 --- a/src/configparser.cpp +++ b/src/configparser.cpp @@ -348,7 +348,7 @@ FilePosition::FilePosition(const std::string& Name, unsigned long Line, unsigned std::string FilePosition::str() const { - return name + ":" + ConvToStr(line) + ":" + ConvToStr(column); + return INSP_FORMAT("{}:{}:{}", name, line, column); } void ParseStack::DoInclude(const std::shared_ptr& tag, int flags) @@ -588,7 +588,7 @@ std::string ConfigTag::getString(const std::string& key, const std::string& def, if (res.length() < minlen || res.length() > maxlen) { - LogMalformed(key, res, def, "not between " + ConvToStr(minlen) + " and " + ConvToStr(maxlen) + " characters in length"); + LogMalformed(key, res, def, INSP_FORMAT("not between {} and {} characters in length", minlen, maxlen)); return def; } return res; @@ -629,7 +629,7 @@ namespace default: num = def; - tag->LogMalformed(key, val, ConvToStr(def), "contains an invalid magnitude specifier (" + ConvToStr(*tail) +")"); + tag->LogMalformed(key, val, ConvToStr(def), INSP_FORMAT("contains an invalid magnitude specifier ({})", *tail)); return; } } @@ -649,7 +649,7 @@ namespace if (num >= min && num <= max) return; - tag->LogMalformed(key, ConvToStr(num), ConvToStr(def), "not between " + ConvToStr(min) + " and " + ConvToStr(max)); + tag->LogMalformed(key, ConvToStr(num), ConvToStr(def), INSP_FORMAT("not between {} and {}", min, max)); num = def; } } diff --git a/src/configreader.cpp b/src/configreader.cpp index 0fc7900a4..ac6cd7953 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -281,7 +281,7 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current) throw CoreException("Connect class must have allow, deny, or name specified at " + tag->source.str()); if (name.empty()) - name = "unnamed-" + ConvToStr(i); + name = INSP_FORMAT("unnamed-{}", i); if (names.find(name) != names.end()) throw CoreException("Two connect classes with name \"" + name + "\" defined!"); diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index a43c4d9dd..c81a9fa0f 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -403,7 +403,7 @@ static constexpr unsigned int duration_multi[] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -bool Duration::TryFrom(const std::string& str, unsigned long& duration) +bool Duration::TryFrom(const std::string& str, unsigned long& duration, time_t base) { unsigned long total = 0; unsigned long subtotal = 0; @@ -437,10 +437,10 @@ bool Duration::TryFrom(const std::string& str, unsigned long& duration) return true; } -unsigned long Duration::From(const std::string& str) +unsigned long Duration::From(const std::string& str, time_t base) { unsigned long out = 0; - Duration::TryFrom(str, out); + Duration::TryFrom(str, out, base); return out; } diff --git a/src/modules/extra/m_ssl_gnutls.cpp b/src/modules/extra/m_ssl_gnutls.cpp index b6a8a142f..81db25757 100644 --- a/src/modules/extra/m_ssl_gnutls.cpp +++ b/src/modules/extra/m_ssl_gnutls.cpp @@ -292,7 +292,8 @@ namespace GnuTLS if (ret < 0) { // gnutls did not understand the user supplied string - throw Exception("Unable to initialize priorities to \"" + priorities + "\": " + gnutls_strerror(ret) + " Syntax error at position " + ConvToStr((unsigned int) (prioerror - priocstr))); + throw Exception(INSP_FORMAT("Unable to initialize priorities to \"{}\": {} Syntax error at position {}.", + priorities, gnutls_strerror(ret), static_cast(prioerror - priocstr))); } } diff --git a/src/modules/m_chanhistory.cpp b/src/modules/m_chanhistory.cpp index 290a8b2de..02d91f345 100644 --- a/src/modules/m_chanhistory.cpp +++ b/src/modules/m_chanhistory.cpp @@ -281,9 +281,9 @@ public: if ((prefixmsg) && (!batchcap.IsEnabled(localuser))) { - std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history"); + auto message = INSP_FORMAT("Replaying up to {} lines of pre-join history", list->maxlen); if (list->maxtime > 0) - message.append(" from the last " + Duration::ToString(list->maxtime)); + message += INSP_FORMAT(" from the last {}", Duration::ToString(list->maxtime)); memb->WriteNotice(message); } diff --git a/src/modules/m_cloak_md5.cpp b/src/modules/m_cloak_md5.cpp index 4cb93ea66..a8c9a5101 100644 --- a/src/modules/m_cloak_md5.cpp +++ b/src/modules/m_cloak_md5.cpp @@ -299,13 +299,13 @@ public: // Ensure that we have the parameter. const std::string key = tag->getString("key"); if (key.empty()) - throw ModuleException(creator, "You have not defined a cloaking key. Define as a " + ConvToStr(minkeylen) + "+ character network-wide secret, at " + tag->source.str()); + throw ModuleException(creator, INSP_FORMAT("You have not defined a cloaking key. Define as a {}+ character network-wide secret, at {}", minkeylen, tag->source.str())); // If we are the first cloak method then mandate a strong key. if (primary) { if (key.length() < minkeylen) - throw ModuleException(creator, "Your cloaking key is not secure. It should be at least " + ConvToStr(minkeylen) + " characters long, at " + tag->source.str()); + throw ModuleException(creator, INSP_FORMAT("Your cloaking key is not secure. It should be at least {} characters long, at {}", minkeylen, tag->source.str())); ServerInstance->Logs.Normal(MODNAME, "The {} cloak method is deprecated and will be removed in the next major version of InspIRCd. Consider migrating to cloak_sha256 instead. See " INSPIRCD_DOCS "modules/cloak_md5 for more information.", name.c_str() + 6); diff --git a/src/modules/m_cloak_sha256.cpp b/src/modules/m_cloak_sha256.cpp index dd040d0b6..4b842f615 100644 --- a/src/modules/m_cloak_sha256.cpp +++ b/src/modules/m_cloak_sha256.cpp @@ -311,7 +311,7 @@ public: // Ensure that we have the parameter. const std::string key = tag->getString("key"); if (key.length() < minkeylen) - throw ModuleException(creator, "Your cloak key should be at least " + ConvToStr(minkeylen) + " characters long, at " + tag->source.str()); + throw ModuleException(creator, INSP_FORMAT("Your cloak key should be at least {} characters long, at {}", minkeylen, tag->source.str())); psl_ctx_t* psl = nullptr; std::string psldb = tag->getString("psl"); diff --git a/src/modules/m_dnsbl.cpp b/src/modules/m_dnsbl.cpp index 18bd199ae..7b8e47417 100644 --- a/src/modules/m_dnsbl.cpp +++ b/src/modules/m_dnsbl.cpp @@ -710,9 +710,9 @@ public: dnsbl->name, dnsbl->stats_hits, dnsbl->stats_misses, dnsbl->stats_errors)); } - stats.AddGenericRow("Total DNSBL hits: " + ConvToStr(total_hits)); - stats.AddGenericRow("Total DNSBL misses: " + ConvToStr(total_misses)); - stats.AddGenericRow("Total DNSBL errors: " + ConvToStr(total_errors)); + stats.AddGenericRow(INSP_FORMAT("Total DNSBL hits: {}", total_hits)); + stats.AddGenericRow(INSP_FORMAT("Total DNSBL misses: {}", total_misses)); + stats.AddGenericRow(INSP_FORMAT("Total DNSBL errors: {}", total_errors)); return MOD_RES_DENY; } }; diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index 9ba0ce200..3f2d32f7b 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -995,7 +995,7 @@ bool ModuleFilter::WriteDatabase() return true; } - const std::string newfilterconf = filterconf + ".new." + ConvToStr(ServerInstance->Time()); + const auto newfilterconf = INSP_FORMAT("{}.new.{}", filterconf, ServerInstance->Time()); std::ofstream stream(newfilterconf.c_str()); if (!stream.is_open()) // Filesystem probably not writable. { diff --git a/src/modules/m_pbkdf2.cpp b/src/modules/m_pbkdf2.cpp index d13740128..037a2863e 100644 --- a/src/modules/m_pbkdf2.cpp +++ b/src/modules/m_pbkdf2.cpp @@ -67,7 +67,8 @@ public: { if (!IsValid()) return ""; - return ConvToStr(this->iterations) + ":" + Base64::Encode(this->hash) + ":" + Base64::Encode(this->salt); + + return INSP_FORMAT("{}:{}:{}", this->iterations, Base64::Encode(this->hash), Base64::Encode(this->salt)); } bool IsValid() const diff --git a/src/modules/m_permchannels.cpp b/src/modules/m_permchannels.cpp index 5c6713c73..413ac46fc 100644 --- a/src/modules/m_permchannels.cpp +++ b/src/modules/m_permchannels.cpp @@ -74,7 +74,7 @@ static bool WriteDatabase(PermChannel& permchanmode, bool save_listmodes, unsign if (permchannelsconf.empty()) return true; - const std::string permchannelsnewconf = permchannelsconf + ".new." + ConvToStr(ServerInstance->Time()); + const auto permchannelsnewconf = INSP_FORMAT("{}.new.{}", permchannelsconf, ServerInstance->Time()); std::ofstream stream(permchannelsnewconf); if (!stream.is_open()) { diff --git a/src/modules/m_setidle.cpp b/src/modules/m_setidle.cpp index e704f345d..eab34d76c 100644 --- a/src/modules/m_setidle.cpp +++ b/src/modules/m_setidle.cpp @@ -61,7 +61,7 @@ public: if (user->signon > user->idle_lastmsg) user->signon = user->idle_lastmsg; - ServerInstance->SNO.WriteToSnoMask('a', user->nick+" used SETIDLE to set their idle time to "+ConvToStr(idle)+" seconds"); + ServerInstance->SNO.WriteToSnoMask('a', "{} used SETIDLE to set their idle time to {} seconds", user->nick, idle); noterpl.SendIfCap(user, stdrplcap, this, "IDLE_TIME_SET", user->idle_lastmsg, "Idle time set."); return CmdResult::SUCCESS; } diff --git a/src/modules/m_spanningtree/capab.cpp b/src/modules/m_spanningtree/capab.cpp index 85b41f56c..43aaa5e1a 100644 --- a/src/modules/m_spanningtree/capab.cpp +++ b/src/modules/m_spanningtree/capab.cpp @@ -330,7 +330,7 @@ void TreeSocket::SendCapabilities(int phase) return; if (capab->capab_phase < 1 && phase >= 1) - WriteLine("CAPAB START " + ConvToStr(PROTO_NEWEST)); + WriteLine(INSP_FORMAT("CAPAB START {}", (uint16_t)PROTO_NEWEST)); capab->capab_phase = phase; if (phase < 2) diff --git a/src/modules/m_spanningtree/override_map.cpp b/src/modules/m_spanningtree/override_map.cpp index 12dbe931a..1dda7b9f8 100644 --- a/src/modules/m_spanningtree/override_map.cpp +++ b/src/modules/m_spanningtree/override_map.cpp @@ -100,7 +100,10 @@ static std::vector GetMap(User* user, TreeServer* current, size_t m if (user->IsOper()) { time_t secs_up = ServerInstance->Time() - current->age; - buffer += " [Up: " + Duration::ToString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]"); + buffer += INSP_FORMAT(" [Up: {}", Duration::ToString(secs_up)); + if (current->rtt) + buffer += INSP_FORMAT(" Lag: {}ms", current->rtt); + buffer += "]"; } std::vector map; diff --git a/src/modules/m_spanningtree/treesocket2.cpp b/src/modules/m_spanningtree/treesocket2.cpp index eae8cbe21..3d454eef9 100644 --- a/src/modules/m_spanningtree/treesocket2.cpp +++ b/src/modules/m_spanningtree/treesocket2.cpp @@ -161,11 +161,11 @@ void TreeSocket::ProcessLine(std::string& line) if (!params.empty()) { time_t them = ServerCommand::ExtractTS(params[0]); - time_t delta = them - ServerInstance->Time(); - if ((delta < -15) || (delta > 15)) + time_t delta = std::abs(them - ServerInstance->Time()); + if (delta > 15) { - ServerInstance->SNO.WriteGlobalSno('l', "\002ERROR\002: Your clocks are off by {} seconds (this is more than fifteen seconds). Link aborted, \002PLEASE SYNC YOUR CLOCKS!\002", labs((long)delta)); - SendError("Your clocks are out by "+ConvToStr(labs((long)delta))+" seconds (this is more than fifteen seconds). Link aborted, PLEASE SYNC YOUR CLOCKS!"); + ServerInstance->SNO.WriteGlobalSno('l', "\002ERROR\002: Your clocks are off by {} seconds (this is more than fifteen seconds). Link aborted, \002PLEASE SYNC YOUR CLOCKS!\002", delta); + SendError(INSP_FORMAT("Your clocks are out by {} seconds (this is more than fifteen seconds). Link aborted, PLEASE SYNC YOUR CLOCKS!", delta)); return; } else if ((delta < -5) || (delta > 5)) diff --git a/src/modules/m_spanningtree/utils.cpp b/src/modules/m_spanningtree/utils.cpp index e149db881..9a10450f9 100644 --- a/src/modules/m_spanningtree/utils.cpp +++ b/src/modules/m_spanningtree/utils.cpp @@ -301,7 +301,7 @@ void SpanningTreeUtilities::ReadConfiguration() throw ModuleException((Module*)Creator, "The link name '"+L->Name+"' is invalid as it must contain at least one '.' character"); if (L->Name.length() > ServerInstance->Config->Limits.MaxHost) - throw ModuleException((Module*)Creator, "The link name '"+L->Name+"' is invalid as it is longer than " + ConvToStr(ServerInstance->Config->Limits.MaxHost) + " characters"); + throw ModuleException((Module*)Creator, INSP_FORMAT("The link name '{}' is invalid as it is longer than {} characters", L->Name, ServerInstance->Config->Limits.MaxHost)); if (L->RecvPass.empty()) throw ModuleException((Module*)Creator, "Invalid configuration for server '"+L->Name+"', recvpass not defined"); diff --git a/src/modules/m_xline_db.cpp b/src/modules/m_xline_db.cpp index 4ccdc8279..6ef2ad815 100644 --- a/src/modules/m_xline_db.cpp +++ b/src/modules/m_xline_db.cpp @@ -124,7 +124,7 @@ public: * -- w00t */ ServerInstance->Logs.Debug(MODNAME, "Opening temporary database"); - const std::string xlinenewdbpath = xlinedbpath + ".new." + ConvToStr(ServerInstance->Time()); + const auto xlinenewdbpath = INSP_FORMAT("{}.new.{}", xlinedbpath, ServerInstance->Time()); std::ofstream stream(xlinenewdbpath); if (!stream.is_open()) { diff --git a/src/snomasks.cpp b/src/snomasks.cpp index 1285975c0..572f39d04 100644 --- a/src/snomasks.cpp +++ b/src/snomasks.cpp @@ -100,7 +100,7 @@ void Snomask::Flush() if (Count > 1) { std::string desc = GetDescription(LastLetter); - std::string msg = "(last message repeated " + ConvToStr(Count) + " times)"; + std::string msg = INSP_FORMAT("(last message repeated {} times)", Count); FOREACH_MOD(OnSendSnotice, (LastLetter, desc, msg)); Snomask::Send(LastLetter, desc, msg); diff --git a/src/socket.cpp b/src/socket.cpp index f8564b9d1..8419e1d6f 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -120,7 +120,7 @@ size_t InspIRCd::BindPorts(FailedPortList& failed_ports) // Check if the port is out of range. if (port <= std::numeric_limits::min() || port > std::numeric_limits::max()) { - failed_ports.emplace_back("Port is not valid: " + ConvToStr(port), bindspec, tag); + failed_ports.emplace_back(INSP_FORMAT("Port is not valid: {}", port), bindspec, tag); continue; } diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 2a9c6e0de..c6da619b8 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -65,7 +65,7 @@ namespace if (!user->lastping) { time_t secs = ServerInstance->Time() - (user->nextping - user->GetClass()->pingtime); - const std::string message = "Ping timeout: " + ConvToStr(secs) + (secs != 1 ? " seconds" : " second"); + const std::string message = INSP_FORMAT("Ping timeout: {} {}", secs, (secs != 1 ? "seconds" : "second")); ServerInstance->Users.QuitUser(user, message); return; }