From 3f7a7df7409e177f158bdd677321e4a61eb6a440 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 27 Aug 2024 16:20:49 +0100 Subject: Deprecate the raw overload of GenRandomStr in favour of GenRandom. The raw overload was almost always misused where GenRandom would be better. While we're making changes to this code switch the printable mode to use a static array like Anope does. --- src/helperfuncs.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/helperfuncs.cpp') diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 5ebb6e44d..5ccb8ed6c 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -443,13 +443,31 @@ std::string Time::ToString(time_t curtime, const char* format, bool utc) return buffer; } +std::string InspIRCd::GenRandomStr(size_t length) const +{ + static const char chars[] = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + }; + + std::string buf; + buf.reserve(length); + for (size_t idx = 0; idx < length; ++idx) + buf.push_back(chars[GenRandomInt(std::size(chars))]); + return buf; +} + std::string InspIRCd::GenRandomStr(size_t length, bool printable) const { + if (printable) + return GenRandomStr(length); + + // DEPRECATED std::vector str(length); GenRandom(str.data(), length); - if (printable) - for (size_t i = 0; i < length; i++) - str[i] = 0x3F + (str[i] & 0x3F); return std::string(str.data(), str.size()); } -- cgit v1.3.1-10-gc9f91