diff --git a/libbitdht/src/bitdht/bdmsgs.cc b/libbitdht/src/bitdht/bdmsgs.cc index 1af0c9814..07114dbab 100644 --- a/libbitdht/src/bitdht/bdmsgs.cc +++ b/libbitdht/src/bitdht/bdmsgs.cc @@ -827,9 +827,10 @@ int decodeCompactPeerId(struct sockaddr_in *addr, char *enc, int len) memset(addr, 0, sizeof(struct sockaddr_in)); - uint32_t *ip = (uint32_t *) (enc); + //uint32_t *ip = (uint32_t *) (enc); uint16_t *port = (uint16_t *) (&enc[4]); - addr->sin_addr.s_addr = (*ip); + + memcpy(& addr->sin_addr.s_addr, enc, sizeof(in_addr_t)); // aligned version of "addr->sin_addr.s_addr = (*ip); " addr->sin_port = (*port); addr->sin_family = AF_INET; diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index ff825f691..abaf05560 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -1945,7 +1945,10 @@ p3FileDatabase::DirSyncRequestId p3FileDatabase::makeDirSyncReqId(const RsPeerId // This is kind of arbitrary. The important thing is that the same ID needs to be generated every time for a given (peer_id,entry index) pair, in a way // that cannot be brute-forced or reverse-engineered, which explains the random bias and the usage of the hash, that is itself random. - uint64_t r = random_bias ^ *((uint64_t*)tmp.toByteArray()) ; + uint64_t tmp2 ; + memcpy(&tmp2,tmp.toByteArray(),sizeof(uint64_t)); + + uint64_t r = random_bias ^ tmp2; #ifdef DEBUG_P3FILELISTS std::cerr << "Creating ID " << std::hex << r << std::dec << " from peer id " << peer_id << " and hash " << hash << std::endl; diff --git a/libretroshare/src/gxstrans/p3gxstrans.cc b/libretroshare/src/gxstrans/p3gxstrans.cc index 899dcd177..b0fa4b3f0 100644 --- a/libretroshare/src/gxstrans/p3gxstrans.cc +++ b/libretroshare/src/gxstrans/p3gxstrans.cc @@ -533,6 +533,14 @@ void p3GxsTrans::service_tick() for(std::map::const_iterator it(per_user_statistics.begin());it!=per_user_statistics.end();++it) std::cerr << " " << it->first << ": " << it->second.count << " " << it->second.size << std::endl; #endif + // Waiting here is very important because the thread may still be updating its semaphores after setting isDone() to true + // If we delete it during this operation it will corrupt the stack and cause unpredictable errors. + + while(mCleanupThread->isRunning()) + { + std::cerr << "Waiting for mCleanupThread to terminate..." << std::endl; + rstime::rs_usleep(500*1000); + } delete mCleanupThread; mCleanupThread=NULL ; diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index 13796d350..bcef2eed4 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -101,14 +101,20 @@ peerAddrInfo::peerAddrInfo() } peerConnectState::peerConnectState() - : connecttype(0), - lastavailable(0), - lastattempt(0), - name(""), - state(0), actions(0), - source(0), - inConnAttempt(0), - wasDeniedConnection(false), deniedTS(false), deniedInConnAttempt(false) + : dhtVisible(false), + connecttype(0), + actAsServer(false), + lastavailable(0), + lastattempt(0), + name(""), + state(0), + actions(0), + linkType(0), + source(0), + inConnAttempt(false), + wasDeniedConnection(false), + deniedTS(0), + deniedInConnAttempt(false) { } diff --git a/libretroshare/src/retroshare/rsgxschannels.h b/libretroshare/src/retroshare/rsgxschannels.h index 9ff20cd11..655d87ff2 100644 --- a/libretroshare/src/retroshare/rsgxschannels.h +++ b/libretroshare/src/retroshare/rsgxschannels.h @@ -47,6 +47,8 @@ extern RsGxsChannels* rsGxsChannels; struct RsGxsChannelGroup : RsSerializable { + RsGxsChannelGroup() : mAutoDownload(false) {} + RsGroupMetaData mMeta; std::string mDescription; RsGxsImage mImage; diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 4d6189d12..e474633f7 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -25,6 +25,7 @@ #include // for errno #include #include "util/rstime.h" +#include "util/rsdebug.h" #include "util/rstime.h" @@ -66,8 +67,8 @@ void RsThread::go() runloop(); - mHasStoppedSemaphore.set(1); mShouldStopSemaphore.set(0); + mHasStoppedSemaphore.set(1); // last value that we modify because this is interpreted as a signal that the object can be deleted. } void *RsThread::rsthread_init(void* p) { @@ -103,6 +104,15 @@ RsThread::RsThread() mShouldStopSemaphore.set(0) ; } +RsThread::~RsThread() +{ + if(isRunning()) + { + RsErr() << "Deleting a thread that is still running! Something is very wrong here and Retroshare is likely to crash because of this." << std::endl; + print_stacktrace(); + } +} + bool RsThread::isRunning() { // do we need a mutex for this ? diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index 6a61a9b4e..15c776e64 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -243,11 +243,11 @@ class RsThread { public: RsThread(); - virtual ~RsThread() {} + virtual ~RsThread() ; void start(const std::string &threadName = ""); - // Returns true of the thread is still running. + // Returns true if the thread is still running. bool isRunning(); diff --git a/libretroshare/src/util/smallobject.h b/libretroshare/src/util/smallobject.h index 2f3ac592c..452037ac6 100644 --- a/libretroshare/src/util/smallobject.h +++ b/libretroshare/src/util/smallobject.h @@ -61,7 +61,7 @@ namespace RsMemoryManagement inline bool chunkOwnsPointer(const Chunk& c,void *p) const { - return p >= c._data && (static_cast(p)-c._data)/_blockSize < _numBlocks ; + return intptr_t(p) >= intptr_t(c._data) && (intptr_t(static_cast(p))-intptr_t(c._data))/intptr_t(_blockSize)< intptr_t( _numBlocks ); } void printStatistics() const ; diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 70050a727..a40d29047 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -92,6 +92,10 @@ linux-* { DEFINES *= HAVE_XSS # for idle time, libx screensaver extensions } +rs_sanitize { + LIBS *= -lasan -lubsan +} + unix { target.path = "$${BIN_DIR}" INSTALLS += target diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 8758f36c5..698a4c58e 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -58,6 +58,9 @@ linux-g++-64 { OBJECTS_DIR = temp/linux-g++-64/obj } +rs_sanitize { + LIBS *= -lasan -lubsan +} #################### Cross compilation for windows under Linux ################### win32-x-g++ { diff --git a/retroshare.pri b/retroshare.pri index 2d8130084..6cb2b3d21 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -461,6 +461,10 @@ rs_onlyhiddennode { message("QMAKE: You have enabled only hidden node.") } +rs_sanitize { + QMAKE_CXXFLAGS *= -fsanitize=address -fsanitize=bounds -fsanitize=undefined +} + no_rs_deprecatedwarning { QMAKE_CXXFLAGS += -Wno-deprecated QMAKE_CXXFLAGS += -Wno-deprecated-declarations