https://gcc.gnu.org/g:1705d5fa6e08dd75f479de1bdf820b56e7727aa9
commit r17-6479-g1705d5fa6e08dd75f479de1bdf820b56e7727aa9 Author: Jerry DeLisle <[email protected]> Date: Tue Nov 17 17:09:34 2026 -0800 libgfortran: caf_shmem: check mmap against MAP_FAILED shared_memory_init compared the result of mmap with NULL, but mmap reports failure with MAP_FAILED. A failed mmap in the supervisor went unnoticed until the first access to the shared memory segfaulted. In an image it looked like a mapping at the wrong address, so the supervisor restarted the image until it gave up with "no common state on shared memory could be reached". Check for MAP_FAILED before comparing the address, and exit with a message. Assisted-by: Claude Opus 5.5 libgfortran/ChangeLog: * caf/shmem/shared_memory.c (shared_memory_init): Check the result of mmap against MAP_FAILED before comparing it with the requested address. Diff: --- libgfortran/caf/shmem/shared_memory.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libgfortran/caf/shmem/shared_memory.c b/libgfortran/caf/shmem/shared_memory.c index 281336c80bc6..dc78f66e8205 100644 --- a/libgfortran/caf/shmem/shared_memory.c +++ b/libgfortran/caf/shmem/shared_memory.c @@ -256,6 +256,11 @@ shared_memory_init (shared_memory_act *mem, size_t size) #ifdef HAVE_MMAP mem->glbl.base = mmap (base_ptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem->shm_fd, 0); + if (mem->glbl.base == MAP_FAILED) + { + perror ("mmap failed"); + exit (1); + } if (base_ptr && mem->glbl.base != base_ptr) { /* The supervisor will start us again. */ @@ -263,11 +268,6 @@ shared_memory_init (shared_memory_act *mem, size_t size) free (local); exit (210); } - else if (!base_ptr && !mem->glbl.base) - { - perror ("mmap failed"); - exit (1); - } #ifdef MADV_DONTDUMP /* Keep the unlinked object, which counts as anonymous shared memory, out of core dumps. */
