From 7c77cfd603e49a2ec4c36c545a2237f565159361 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 9 Dec 2021 22:19:00 +0100 Subject: [PATCH] fixed a few bugs in new TorManager --- libretroshare/src/tor/CryptoKey.cpp | 2 +- libretroshare/src/tor/GetConfCommand.cpp | 2 +- libretroshare/src/tor/StrUtil.cpp | 10 +++++----- libretroshare/src/tor/TorManager.cpp | 7 ++++--- libretroshare/src/tor/TorProcess.cpp | 3 ++- libretroshare/src/tor/bytearray.h | 2 ++ libretroshare/src/util/rsrandom.cc | 12 ++++++++---- 7 files changed, 23 insertions(+), 15 deletions(-) diff --git a/libretroshare/src/tor/CryptoKey.cpp b/libretroshare/src/tor/CryptoKey.cpp index a914b03aa..641c1e4dc 100644 --- a/libretroshare/src/tor/CryptoKey.cpp +++ b/libretroshare/src/tor/CryptoKey.cpp @@ -82,7 +82,7 @@ bool CryptoKey::loadFromFile(const std::string& path) ByteArray data ; int c; while(EOF != (c=fgetc(file))) - data.append((unsigned char)c); + data.push_back((unsigned char)c); fclose(file); diff --git a/libretroshare/src/tor/GetConfCommand.cpp b/libretroshare/src/tor/GetConfCommand.cpp index 02e1df1ce..02f903b4a 100644 --- a/libretroshare/src/tor/GetConfCommand.cpp +++ b/libretroshare/src/tor/GetConfCommand.cpp @@ -58,7 +58,7 @@ ByteArray GetConfCommand::build(const std::list &keys) } for(const ByteArray &key: keys) { - out.append(' '); + out.push_back(' '); out.append(key); } diff --git a/libretroshare/src/tor/StrUtil.cpp b/libretroshare/src/tor/StrUtil.cpp index b0d035845..8e4ac9d20 100644 --- a/libretroshare/src/tor/StrUtil.cpp +++ b/libretroshare/src/tor/StrUtil.cpp @@ -37,7 +37,7 @@ ByteArray quotedString(const ByteArray &string) ByteArray out; out.reserve(string.size() * 2); - out.append('"'); + out.push_back('"'); for (uint i = 0; i < string.size(); ++i) { @@ -50,12 +50,12 @@ ByteArray quotedString(const ByteArray &string) out.append("\\\\"); break; default: - out.append(string[i]); + out.push_back(string[i]); break; } } - out.append('"'); + out.push_back('"'); return out; } @@ -73,12 +73,12 @@ ByteArray unquotedString(const ByteArray& string) { case '\\': if (++i < string.size()) - out.append(string[i]); + out.push_back(string[i]); break; case '"': return out; default: - out.append(string[i]); + out.push_back(string[i]); } } diff --git a/libretroshare/src/tor/TorManager.cpp b/libretroshare/src/tor/TorManager.cpp index e06dec02b..e19e8a10d 100644 --- a/libretroshare/src/tor/TorManager.cpp +++ b/libretroshare/src/tor/TorManager.cpp @@ -168,13 +168,14 @@ bool TorManager::setupHiddenService() } std::cerr << "Using legacy dir: " << legacyDir << std::endl; + auto key_path = RsDirUtil::makePath(legacyDir,"/private_key"); - if (!legacyDir.empty() && RsDirUtil::fileExists(RsDirUtil::makePath(legacyDir,"/private_key"))) + if (!legacyDir.empty() && RsDirUtil::fileExists(key_path)) { - std::cerr << "Attempting to load key from legacy filesystem format in " << legacyDir << std::endl; + std::cerr << "Attempting to load key from legacy filesystem format from file \"" << key_path << "\"" << std::endl; CryptoKey key; - if (!key.loadFromFile(RsDirUtil::makePath(legacyDir , "/private_key"))) + if (!key.loadFromFile(key_path)) { RsWarn() << "Cannot load legacy format key from" << legacyDir << "for conversion"; return false; diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 3a7f06fbc..7cb4c832e 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -46,9 +46,10 @@ using namespace Tor; static const int INTERVAL_BETWEEN_CONTROL_PORT_READ_TRIES = 5; // try every 5 secs. TorProcess::TorProcess(TorProcessClient *client) - : m_client(client), mLastTryReadControlPort(0) + : m_client(client), mState(TorProcess::NotStarted), mControlPort(0), mLastTryReadControlPort(0) { mControlPortReadNbTries=0; + } TorProcess::~TorProcess() diff --git a/libretroshare/src/tor/bytearray.h b/libretroshare/src/tor/bytearray.h index ebf2b9f85..6d8897ae9 100644 --- a/libretroshare/src/tor/bytearray.h +++ b/libretroshare/src/tor/bytearray.h @@ -32,6 +32,8 @@ public: void append(const ByteArray& b) { for(auto c:b) push_back(c); } void append(const char *b) { for(uint32_t n=0;b[n]!=0;++n) push_back(b[n]); } + template void append(const T) = delete;// Prevents any implicit when calling the preceding functions which actually causes real bugs. + ByteArray& operator+=(const ByteArray& b) { for(auto c:b) push_back(c); return *this; } ByteArray& operator+=(const char *b) { for(uint32_t n=0;b[n]!=0;++n) push_back(b[n]); return *this;} diff --git a/libretroshare/src/util/rsrandom.cc b/libretroshare/src/util/rsrandom.cc index dc7d9ac67..96a24b140 100644 --- a/libretroshare/src/util/rsrandom.cc +++ b/libretroshare/src/util/rsrandom.cc @@ -139,8 +139,12 @@ double RsRandom::random_f64() /*static*/ std::string RsRandom::printable(uint32_t length) { - std::string ret(length, 0); - random_bytes(reinterpret_cast(&ret[0]), length); - for(uint32_t i=0; i