Skip to content

Commit 470ab93

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 aba82c7 commit 470ab93

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
@@ -656,8 +656,12 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
656656
}
657657
shm->segment = shm_handle;
658658
shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
659+
if (shm->descriptor == NULL) {
660+
SET_ERRNO_FROM_WIN32_CODE(GetLastError());
661+
return -1;
662+
}
659663

660-
if (NULL != shm->descriptor && created) {
664+
if (created) {
661665
shm->descriptor->shm_perm.key = key;
662666
shm->descriptor->shm_segsz = size;
663667
shm->descriptor->shm_ctime = time(NULL);
@@ -671,7 +675,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
671675
shm->descriptor->shm_perm.mode = shm->descriptor->shm_perm.seq = 0;
672676
}
673677

674-
if (NULL != shm->descriptor && (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz)) {
678+
if (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz) {
675679
if (NULL != shm->segment) {
676680
CloseHandle(shm->segment);
677681
}

0 commit comments

Comments
 (0)