Skip to content

Commit cf3a1e6

Browse files
committed
Fix MapViewOfFile permission issue
1 parent 2911527 commit cf3a1e6

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

include/slick_queue/slick_queue.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class SlickQueue {
206206

207207
#if defined(_MSC_VER)
208208
void allocate_shm_data(const char* const shm_name, bool open_only) {
209-
DWORD BF_SZ = 64 + sizeof(slot) * buffered_size_ + sizeof(T) * buffered_size_;
209+
DWORD BF_SZ;
210210
hMapFile_ = NULL;
211211

212212
#ifndef UNICODE
@@ -229,19 +229,28 @@ class SlickQueue {
229229
throw std::runtime_error("Failed to open shm. err=" + std::to_string(err));
230230
}
231231

232-
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
233-
if (!lpvMem_) {
232+
auto lpvMem = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, 64);
233+
if (!lpvMem) {
234234
auto err = GetLastError();
235235
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
236236
}
237-
size_ = *reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem_) + sizeof(std::atomic_uint_fast64_t));
237+
size_ = *reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem) + sizeof(std::atomic_uint_fast64_t));
238238
mask_ = size_ - 1;
239239
buffered_size_ = size_ + 1024;
240+
BF_SZ = 64 + sizeof(slot) * buffered_size_ + sizeof(T) * buffered_size_;
241+
UnmapViewOfFile(lpvMem);
240242
}
241243
else {
244+
BF_SZ = 64 + sizeof(slot) * buffered_size_ + sizeof(T) * buffered_size_;
245+
246+
SECURITY_ATTRIBUTES sa;
247+
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
248+
sa.lpSecurityDescriptor = NULL;
249+
sa.bInheritHandle = FALSE;
250+
242251
hMapFile_ = CreateFileMapping(
243252
INVALID_HANDLE_VALUE, // use paging file
244-
NULL, // default security
253+
&sa, // default security
245254
PAGE_READWRITE, // read/write access
246255
0, // maximum object size (high-order DWORD)
247256
BF_SZ, // maximum object size (low-order DWORD)
@@ -261,17 +270,17 @@ class SlickQueue {
261270
if (err != ERROR_ALREADY_EXISTS) {
262271
own_ = true;
263272
}
273+
}
264274

265-
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
266-
if (!lpvMem_) {
267-
auto err = GetLastError();
268-
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
269-
}
275+
lpvMem_ = MapViewOfFile(hMapFile_, FILE_MAP_ALL_ACCESS, 0, 0, BF_SZ);
276+
if (!lpvMem_) {
277+
auto err = GetLastError();
278+
throw std::runtime_error("Failed to map shm. err=" + std::to_string(err));
270279
}
271280

272281
if (own_) {
273282
reserved_ = new (lpvMem_) std::atomic_uint_fast64_t{ 0 };
274-
*reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem_) + sizeof(std::atomic_uint_fast64_t)) = mask_ + 1;
283+
*reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(lpvMem_) + sizeof(std::atomic_uint_fast64_t)) = size_;
275284
control_ = new ((uint8_t*)lpvMem_ + 64) slot[buffered_size_];
276285
data_ = new ((uint8_t*)lpvMem_ + 64 + sizeof(slot) * buffered_size_) T[buffered_size_];
277286
}

0 commit comments

Comments
 (0)