Page MenuHome GnuPG

Fix MinGW compilation error with 'struct _stat32' in common/sysutils.c from gnupg-2.3.4
Closed, ResolvedPublic

Description

The use of struct _stat32 in sysutils.c from gnupg-2.3.4 causes compilation errors when using mingw.org's MinGW.
The attached patch fixes that.

Revisions and Commits

Event Timeline

werner added a subscriber: werner.

Turned into a feature request because native building on Windows is not supported.

I guess I don't understand what you mean by "native building". This build was with MinGW, which is as "native" as MinGW64 is.
The reason for the problem is (AFAIU) that MinGW64 went after Microsoft's change in stat due to the 32-bit vs 64-bit time and off_t values issue.s That change breaks backward compatibility in more than one way: programs compiled on some versions of Windows will not run on other versions. mingw.org's MinGW kept the original semantics and symbols, which is why _stat32 exists in the mingw.org's headers, but is not exposed by default.

I hope a single #ifdef for a single line is not a significant price to pay for the solution, especially since this is already in a w32-specific code part, and that you will agree to accept that patch.

I encountered this issue of struct stat when compiling for x86_64 of Windows.
I'm considering this patch:

diff --git a/common/sysutils.c b/common/sysutils.c
index c30f9a0ce..bbed309a8 100644
--- a/common/sysutils.c
+++ b/common/sysutils.c
@@ -1237,10 +1237,20 @@ int
 gnupg_stat (const char *name, struct stat *statbuf)
 {
 # ifdef HAVE_W32_SYSTEM
+#  if __MINGW32_MAJOR_VERSION > 3
+    /* mingw.org's MinGW */
+#   define STRUCT_STAT _stat
+#  elif defined(_USE_32BIT_TIME_T)
+    /* MinGW64 for i686 */
+#   define STRUCT_STAT _stat32
+#  else
+    /* MinGW64 for x86_64 */
+#   define STRUCT_STAT _stat64i32
+#  endif
   if (any8bitchar (name))
     {
       wchar_t *wname;
-      struct _stat32 st32;
+      struct STRUCT_STAT st32;
       int ret;
 
       wname = utf8_to_wchar (name);

Should be OK for mingw.org's MinGW. I cannot test the MinGW64 bits, but I trust that you did.

Thanks.

gniibe changed the task status from Open to Testing.Oct 14 2022, 7:09 AM

Pushed.

gniibe claimed this task.