From 470ab93cbff6eb2828cc02079d42e4e2cc62fbe6 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Fri, 14 Oct 2022 15:06:26 +0200 Subject: [PATCH 1/2] 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". --- TSRM/tsrm_win32.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c index 81278d5ab4bd..0a07f614c9ce 100644 --- a/TSRM/tsrm_win32.c +++ b/TSRM/tsrm_win32.c @@ -656,8 +656,12 @@ TSRM_API int shmget(key_t key, size_t size, int flags) } shm->segment = shm_handle; shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); + if (shm->descriptor == NULL) { + SET_ERRNO_FROM_WIN32_CODE(GetLastError()); + return -1; + } - if (NULL != shm->descriptor && created) { + if (created) { shm->descriptor->shm_perm.key = key; shm->descriptor->shm_segsz = size; shm->descriptor->shm_ctime = time(NULL); @@ -671,7 +675,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags) shm->descriptor->shm_perm.mode = shm->descriptor->shm_perm.seq = 0; } - if (NULL != shm->descriptor && (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz)) { + if (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz) { if (NULL != shm->segment) { CloseHandle(shm->segment); } From d0f637027b768cd36ebbaae0f937fdb2eedaffdf Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Fri, 14 Oct 2022 15:39:38 +0200 Subject: [PATCH 2/2] Properly close shm->segment --- TSRM/tsrm_win32.c | 1 + 1 file changed, 1 insertion(+) diff --git a/TSRM/tsrm_win32.c b/TSRM/tsrm_win32.c index 0a07f614c9ce..1f3f2d964484 100644 --- a/TSRM/tsrm_win32.c +++ b/TSRM/tsrm_win32.c @@ -658,6 +658,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags) shm->descriptor = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL); if (shm->descriptor == NULL) { SET_ERRNO_FROM_WIN32_CODE(GetLastError()); + CloseHandle(shm->segment); return -1; }