Skip to content

Commit 78cc485

Browse files
committed
fix: robust macOS main-thread detection in TorManager and ByteArray memory safety
1 parent a7b9864 commit 78cc485

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/tor/TorManager.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,15 @@ std::string TorManager::torDataDirectory() const
154154

155155
void TorManager::setTorDataDirectory(const std::string &path)
156156
{
157-
assert(RsDirUtil::checkCreateDirectory(std::string(path)));
157+
if(!RsDirUtil::checkCreateDirectory(path))
158+
{
159+
RsErr() << "TorManager::setTorDataDirectory() cannot create directory: " << path ;
160+
return ;
161+
}
158162

159163
d->dataDir = path;
160164

161-
if (!d->dataDir.empty() && !ByteArray(d->dataDir).endsWith('/'))
165+
if (!d->dataDir.empty() && !(d->dataDir.back() == '/'))
162166
d->dataDir += '/';
163167
}
164168

@@ -937,10 +941,18 @@ void RsTor::setHiddenServiceDirectory(const std::string& dir)
937941
instance()->setHiddenServiceDirectory(dir);
938942
}
939943

944+
#ifdef __APPLE__
945+
#include <pthread.h>
946+
#endif
947+
940948
TorManager *RsTor::instance()
941949
{
950+
#ifdef __APPLE__
951+
assert(pthread_main_np() != 0); // On macOS, ensure we are on the main thread
952+
#else
942953
static std::thread::id main_thread_id = std::this_thread::get_id();
943954
assert(std::this_thread::get_id() == main_thread_id); // make sure we're not in a different thread
955+
#endif
944956

945957
if(rsTor == nullptr)
946958
rsTor = new TorManager;

src/tor/bytearray.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ class ByteArray: public std::vector<unsigned char>
5353

5454
return res;
5555
}
56-
bool endsWith(const ByteArray& b) const { return size() >= b.size() && (b.empty() || !memcmp(&data()[size()-b.size()],b.data(),b.size())); }
56+
bool endsWith(const ByteArray& b) const { return size() >= b.size() && (b.empty() || !memcmp(data() + (size() - b.size()), b.data(), b.size())); }
5757
bool endsWith(char b) const { return size() > 0 && back()==b; }
58-
bool startsWith(const ByteArray& b) const { return b.size() <= size() && (b.empty() || !strncmp((char*)b.data(),(char*)data(),std::min(size(),b.size()))); }
58+
bool startsWith(const ByteArray& b) const { return size() >= b.size() && (b.empty() || !memcmp(data(), b.data(), b.size())); }
5959
bool startsWith(const char *b) const
6060
{
6161
for(uint32_t n=0;b[n]!=0;++n)
62-
if(n >= size() || b[n]!=(*this)[n])
62+
if(n >= size() || (unsigned char)b[n]!=(*this)[n])
6363
return false;
6464

6565
return true;
@@ -69,16 +69,19 @@ class ByteArray: public std::vector<unsigned char>
6969
{
7070
uint32_t n;
7171
for(n=0;b[n]!=0;++n)
72-
if(n >= size() || b[n]!=(*this)[n])
72+
if(n >= size() || (unsigned char)b[n]!=(*this)[n])
7373
return false;
7474

7575
return n==size();
7676
}
7777

78-
ByteArray mid(uint32_t n,int s=-1) const
78+
ByteArray mid(uint32_t n, int s = -1) const
7979
{
80-
ByteArray res((s>=0)?s:(size()-n));
81-
if (!res.empty() && n < size()) memcpy(res.data(),&data()[n],res.size());
80+
if (n >= size()) return ByteArray();
81+
uint32_t max_len = (uint32_t)size() - n;
82+
uint32_t len = (s >= 0) ? std::min((uint32_t)s, max_len) : max_len;
83+
ByteArray res(len);
84+
if (len > 0) memcpy(res.data(), data() + n, len);
8285
return res;
8386
}
8487

0 commit comments

Comments
 (0)