Skip to content

Commit d0d3837

Browse files
committed
Properly handle shmget() ENOMEM error conditions on Windows
We need to properly handle `MapViewOfFileEx()` failures; otherwise strange error messages might be reported in the following. E.g. bug72858.phpt is likely to fail on 32bit Windows with "Warning: shm_attach(): Failed for key 0x64: File exists".
1 parent ca4a841 commit d0d3837

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

TSRM/tsrm_win32.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,12 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
694694
}
695695
shm->segment = shm_handle;
696696
shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
697+
if (shm->descriptor == NULL) {
698+
SET_ERRNO_FROM_WIN32_CODE(GetLastError());
699+
return -1;
700+
}
697701

698-
if (NULL != shm->descriptor && created) {
702+
if (created) {
699703
shm->descriptor->shm_perm.key = key;
700704
shm->descriptor->shm_segsz = size;
701705
shm->descriptor->shm_ctime = time(NULL);
@@ -709,7 +713,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
709713
shm->descriptor->shm_perm.mode = shm->descriptor->shm_perm.seq = 0;
710714
}
711715

712-
if (NULL != shm->descriptor && (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz)) {
716+
if (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz) {
713717
if (NULL != shm->segment) {
714718
CloseHandle(shm->segment);
715719
shm->segment = INVALID_HANDLE_VALUE;

0 commit comments

Comments
 (0)