summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2017-07-16 17:36:11 +0200
committerStanislaw Halik <sthalik@misaki.pl>2017-07-16 17:36:33 +0200
commit1352d0750ef1ee871388a610e9d8813bc07c754d (patch)
tree039bdb88527479edf6924815cc80873a1ed3ce2b
parente016dca372bf69f3bb4967a8aba374bc04aea3b4 (diff)
compat/shm: reorder code
Hopefully prevent getting misdetected by an antivirus. Issue: #654
-rw-r--r--compat/shm.cpp55
-rw-r--r--compat/shm.h28
2 files changed, 45 insertions, 38 deletions
diff --git a/compat/shm.cpp b/compat/shm.cpp
index 6aeb8ca9..88a2811d 100644
--- a/compat/shm.cpp
+++ b/compat/shm.cpp
@@ -7,6 +7,8 @@
#include "shm.h"
+#include <QDebug>
+
#if defined(_WIN32)
#include <cstring>
@@ -109,11 +111,13 @@ PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName,
secattr sa(GENERIC_ALL|SYNCHRONIZE);
hMutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutexName);
+
if (!hMutex)
{
- fprintf(stderr, "CreateMutexA: %d\n", (int) GetLastError());
- fflush(stderr);
+ qDebug() << "CreateMutexA:" << (int) GetLastError();
+ return;
}
+
hMapFile = CreateFileMappingA(
INVALID_HANDLE_VALUE,
sa.success ? &sa.attrs : nullptr,
@@ -121,38 +125,49 @@ PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName,
0,
mapSize,
shmName);
+
if (!hMapFile)
{
- fprintf(stderr, "CreateFileMappingA: %d\n", (int) GetLastError());
- fflush(stderr);
+ qDebug() << "CreateFileMappingA:", (int) GetLastError();
+
+ return;
}
+
mem = MapViewOfFile(hMapFile,
FILE_MAP_WRITE,
0,
0,
mapSize);
+
if (!mem)
- {
- fprintf(stderr, "MapViewOfFile: %d\n", (int) GetLastError());
- fflush(stderr);
- }
+ qDebug() << "MapViewOfFile:" << (int) GetLastError();
}
PortableLockedShm::~PortableLockedShm()
{
- UnmapViewOfFile(mem);
- CloseHandle(hMapFile);
- CloseHandle(hMutex);
+ if(!UnmapViewOfFile(mem))
+ goto fail;
+
+ if (!CloseHandle(hMapFile))
+ goto fail;
+
+ if (!CloseHandle(hMutex))
+ goto fail;
+
+ return;
+
+fail:
+ qDebug() << "failed to close mapping";
}
-void PortableLockedShm::lock()
+bool PortableLockedShm::lock()
{
- (void) WaitForSingleObject(hMutex, INFINITE);
+ return WaitForSingleObject(hMutex, INFINITE) == WAIT_OBJECT_0;
}
-void PortableLockedShm::unlock()
+bool PortableLockedShm::unlock()
{
- (void) ReleaseMutex(hMutex);
+ return ReleaseMutex(hMutex);
}
#else
@@ -161,7 +176,7 @@ void PortableLockedShm::unlock()
#pragma GCC diagnostic ignored "-Wunused-result"
PortableLockedShm::PortableLockedShm(const char *shmName, const char* /*mutexName*/, int mapSize) : size(mapSize)
{
- char filename[PATH_MAX+2] = {0};
+ char filename[PATH_MAX+2] {};
strcpy(filename, "/");
strcat(filename, shmName);
fd = shm_open(filename, O_RDWR | O_CREAT, 0600);
@@ -175,14 +190,14 @@ PortableLockedShm::~PortableLockedShm()
(void) close(fd);
}
-void PortableLockedShm::lock()
+bool PortableLockedShm::lock()
{
- flock(fd, LOCK_EX);
+ return flock(fd, LOCK_EX) == 0;
}
-void PortableLockedShm::unlock()
+bool PortableLockedShm::unlock()
{
- flock(fd, LOCK_UN);
+ return flock(fd, LOCK_UN) == 0;
}
#endif
diff --git a/compat/shm.h b/compat/shm.h
index ffd1e1dc..3b37a8a3 100644
--- a/compat/shm.h
+++ b/compat/shm.h
@@ -19,30 +19,22 @@
#include <sys/types.h>
#endif
-#ifdef __GNUC__
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wattributes"
-#endif
-
#include "export.hpp"
-class OTR_COMPAT_EXPORT PortableLockedShm {
-public:
- PortableLockedShm(const char *shmName, const char *mutexName, int mapSize);
- ~PortableLockedShm();
- void lock();
- void unlock();
- bool success();
- inline void* ptr() { return mem; }
-private:
+class OTR_COMPAT_EXPORT PortableLockedShm final
+{
void* mem;
#if defined(_WIN32)
HANDLE hMutex, hMapFile;
#else
int fd, size;
#endif
-};
-#ifdef __GNUC__
-# pragma GCC diagnostic pop
-#endif
+public:
+ PortableLockedShm(const char *shmName, const char *mutexName, int mapSize);
+ ~PortableLockedShm();
+ bool lock();
+ bool unlock();
+ bool success();
+ inline void* ptr() { return mem; }
+};