[PATCH 2/2] libgfortran: Avoid undefined shift in caf_shmem

Peter Damianov <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
When building gcc 16 for 32-bit Windows, I noticed this warning:

gcc/libgfortran/caf/shmem/shared_memory.c: In function 'shared_memory_init':
gcc/libgfortran/caf/shmem/shared_memory.c:173:35: warning: right shift count >= width of type [-Wshift-count-overflow]
173 |                              size >> (sizeof (DWORD) * 8),
    |                                   ^~

CreateFileMapping takes the maximum mapping size split into high and low
DWORDs.  Cast size through uint64_t before computing the high DWORD so 32-bit
Windows targets do not shift a 32-bit size_t by 32 bits.

libgfortran/ChangeLog:

	* caf/shmem/shared_memory.c: Include stdint.h.
	(shared_memory_init): Cast size through uint64_t before computing the
	high-order CreateFileMapping size DWORD.
---
Resubmitting without changes from the last time.

 libgfortran/caf/shmem/shared_memory.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libgfortran/caf/shmem/shared_memory.c b/libgfortran/caf/shmem/shared_memory.c
index a2520a89cc7..168906b0dc0 100644
--- a/libgfortran/caf/shmem/shared_memory.c
+++ b/libgfortran/caf/shmem/shared_memory.c
@@ -35,6 +35,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #elif defined(WIN32)
@@ -170,8 +171,8 @@ shared_memory_init (shared_memory_act *mem, size_t size)
 #elif defined(WIN32)
       mem->shm_fd
 	= CreateFileMapping (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
-			     size >> (sizeof (DWORD) * 8),
-			     (DWORD) (size & ~((DWORD) 0)), shm_name);
+			     (DWORD) ((uint64_t) size >> 32),
+			     (DWORD) size, shm_name);
       if (mem->shm_fd == NULL)
 	{
 	  LPVOID lpMsgBuf;
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.