For POSIX stat compatibility, it is not possible to use msvcrt _stat32() or
_stat64i32() from crtdll.dll - msvcr100.dll CRT libraries as their
implementations do not signal EOVERFLOW error when file size does not fit
into the stat's st_size field. Signalling EOVERFLOW is required for POSIX
compatibility. New CRT libraries (msvcr110.dll, msvcr120.dll and UCRT) have
implementation of _stat32() and _stat64i32() more compatible with POSIX and
properly return EOVERFLOW when file size does not fit into the struct
stat's st_size field.

So for pre-msvcr110 builds, use only _stat64() function for implementing
POSIX stat in mingw-w64 and do check overflow in mingw-w64 the wrapper.
---
 mingw-w64-crt/Makefile.am                     | 39 +++++++++++++++----
 .../stdio/{stat32.c => msvcr110plus_stat32.c} |  0
 .../{stat64i32.c => msvcr110plus_stat64i32.c} |  0
 .../{wstat32.c => msvcr110plus_wstat32.c}     |  0
 ...wstat64i32.c => msvcr110plus_wstat64i32.c} |  0
 .../stdio/{stat32.c => msvcr110pre_stat32.c}  | 28 ++++++++++++-
 .../{stat64i32.c => msvcr110pre_stat64i32.c}  | 28 ++++++++++++-
 .../{wstat32.c => msvcr110pre_wstat32.c}      | 28 ++++++++++++-
 ...{wstat64i32.c => msvcr110pre_wstat64i32.c} | 28 ++++++++++++-
 9 files changed, 136 insertions(+), 15 deletions(-)
 copy mingw-w64-crt/stdio/{stat32.c => msvcr110plus_stat32.c} (100%)
 copy mingw-w64-crt/stdio/{stat64i32.c => msvcr110plus_stat64i32.c} (100%)
 copy mingw-w64-crt/stdio/{wstat32.c => msvcr110plus_wstat32.c} (100%)
 copy mingw-w64-crt/stdio/{wstat64i32.c => msvcr110plus_wstat64i32.c} (100%)
 rename mingw-w64-crt/stdio/{stat32.c => msvcr110pre_stat32.c} (56%)
 rename mingw-w64-crt/stdio/{stat64i32.c => msvcr110pre_stat64i32.c} (57%)
 rename mingw-w64-crt/stdio/{wstat32.c => msvcr110pre_wstat32.c} (57%)
 rename mingw-w64-crt/stdio/{wstat64i32.c => msvcr110pre_wstat64i32.c} (58%)

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 0a5626d031bd..c6794fa6616f 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -186,14 +186,10 @@ src_msvcrt_common=\
   stdio/vsnwprintf_alias.c \
   stdio/vswprintf.c \
   stdio/vswprintf_alias.c \
-  stdio/stat32.c \
   stdio/stat32i64.c \
   stdio/stat64.c \
-  stdio/stat64i32.c \
-  stdio/wstat32.c \
   stdio/wstat32i64.c \
   stdio/wstat64.c \
-  stdio/wstat64i32.c \
   math/cbrt.c math/cbrtf.c \
   math/copysign.c math/copysignf.c \
   math/coshf.c \
@@ -351,7 +347,11 @@ src_msvcrt=\
   secapi/wmemcpy_s.c \
   secapi/wmemmove_s.c \
   stdio/_ftelli64.c \
-  stdio/mingw_lock.c
+  stdio/mingw_lock.c \
+  stdio/msvcr110pre_stat32.c \
+  stdio/msvcr110pre_stat64i32.c \
+  stdio/msvcr110pre_wstat32.c \
+  stdio/msvcr110pre_wstat64i32.c
 
 src_msvcrt_add_x86=\
   secapi/_access_s.c \
@@ -865,6 +865,12 @@ src_pre_msvcr80=\
 src_pre_msvcr100=\
   misc/imaxdiv.c
 
+src_pre_msvcr110=\
+  stdio/msvcr110pre_stat32.c \
+  stdio/msvcr110pre_stat64i32.c \
+  stdio/msvcr110pre_wstat32.c \
+  stdio/msvcr110pre_wstat64i32.c
+
 src_pre_msvcr120=\
   $(src_msvcrt_add_vscanf) \
   math/i386__copysignf.c \
@@ -881,6 +887,12 @@ src_post_msvcr80=\
   misc/__p__osplatform_emul.c \
   misc/__p__osver_emul.c
 
+src_post_msvcr100=\
+  stdio/msvcr110plus_stat32.c \
+  stdio/msvcr110plus_stat64i32.c \
+  stdio/msvcr110plus_wstat32.c \
+  stdio/msvcr110plus_wstat64i32.c
+
 src_crtdll=\
   $(src_pre_msvcrt20) \
   $(src_pre_msvcrt40) \
@@ -889,6 +901,7 @@ src_crtdll=\
   $(src_pre_msvcr71) \
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/crtdll__getmainargs.c \
   misc/crtdll__ismbblead.c \
@@ -904,6 +917,7 @@ src_msvcrt10=\
   $(src_pre_msvcr71) \
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/crtdll__getmainargs.c \
   misc/msvcrt10__ismbblead.c
@@ -915,6 +929,7 @@ src_msvcrt20=\
   $(src_pre_msvcr71) \
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/msvcrt20__getmainargs.c \
   misc/msvcrt20__wgetmainargs.c
@@ -925,6 +940,7 @@ src_msvcrt40=\
   $(src_pre_msvcr71) \
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/msvcrt__getmainargs.c \
   misc/msvcrt__wgetmainargs.c
@@ -934,6 +950,7 @@ src_msvcrtd=\
   $(src_pre_msvcr71) \
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/msvcrt__getmainargs.c \
   misc/msvcrt__wgetmainargs.c
@@ -942,27 +959,32 @@ src_msvcr70=\
   $(src_pre_msvcr71) \
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/__p__osplatform.c
 
 src_msvcr71=\
   $(src_pre_msvcr80) \
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   misc/__p__osplatform.c
 
 src_msvcr80=\
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   $(src_pre_msvcr120_post_msvcr71)
 
 src_msvcr90=\
   $(src_pre_msvcr100) \
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   $(src_pre_msvcr120_post_msvcr71) \
   $(src_post_msvcr80)
 
 src_msvcr100=\
+  $(src_pre_msvcr110) \
   $(src_pre_msvcr120) \
   $(src_pre_msvcr120_post_msvcr71) \
   $(src_post_msvcr80)
@@ -970,13 +992,16 @@ src_msvcr100=\
 src_msvcr110=\
   $(src_pre_msvcr120) \
   $(src_pre_msvcr120_post_msvcr71) \
-  $(src_post_msvcr80)
+  $(src_post_msvcr80) \
+  $(src_post_msvcr100)
 
 src_msvcr120=\
-  $(src_post_msvcr80)
+  $(src_post_msvcr80) \
+  $(src_post_msvcr100)
 
 src_msvcr120_app=\
   $(src_post_msvcr80) \
+  $(src_post_msvcr100) \
   misc/__p___initenv.c \
   misc/__p___winitenv.c \
   misc/__p__environ.c \
diff --git a/mingw-w64-crt/stdio/stat32.c 
b/mingw-w64-crt/stdio/msvcr110plus_stat32.c
similarity index 100%
copy from mingw-w64-crt/stdio/stat32.c
copy to mingw-w64-crt/stdio/msvcr110plus_stat32.c
diff --git a/mingw-w64-crt/stdio/stat64i32.c 
b/mingw-w64-crt/stdio/msvcr110plus_stat64i32.c
similarity index 100%
copy from mingw-w64-crt/stdio/stat64i32.c
copy to mingw-w64-crt/stdio/msvcr110plus_stat64i32.c
diff --git a/mingw-w64-crt/stdio/wstat32.c 
b/mingw-w64-crt/stdio/msvcr110plus_wstat32.c
similarity index 100%
copy from mingw-w64-crt/stdio/wstat32.c
copy to mingw-w64-crt/stdio/msvcr110plus_wstat32.c
diff --git a/mingw-w64-crt/stdio/wstat64i32.c 
b/mingw-w64-crt/stdio/msvcr110plus_wstat64i32.c
similarity index 100%
copy from mingw-w64-crt/stdio/wstat64i32.c
copy to mingw-w64-crt/stdio/msvcr110plus_wstat64i32.c
diff --git a/mingw-w64-crt/stdio/stat32.c 
b/mingw-w64-crt/stdio/msvcr110pre_stat32.c
similarity index 56%
rename from mingw-w64-crt/stdio/stat32.c
rename to mingw-w64-crt/stdio/msvcr110pre_stat32.c
index 9623c80f5b35..4d5d33c61344 100644
--- a/mingw-w64-crt/stdio/stat32.c
+++ b/mingw-w64-crt/stdio/msvcr110pre_stat32.c
@@ -6,17 +6,41 @@
 
 #include <sys/stat.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 char *__mingw_fix_stat_path(const char *_path);
 
+/* For pre-msvcr110 builds, we cannot use _stat32() function as it does
+ * not signal EOVERFLOW when file size does not fit into the st_size field,
+ * as it is required by POSIX stat() function.
+ * This file is used only for pre-msvcr110 builds.
+ */
 int __cdecl stat32(const char *_Filename, struct _stat32 *_Stat);
 int __cdecl stat32(const char *_Filename, struct _stat32 *_Stat)
 {
+  struct _stat32i64 st;
   char *_path = __mingw_fix_stat_path(_Filename);
-  int ret = _stat32(_path, _Stat);
+  int ret = _stat32i64(_path, &st);
   if (_path != _Filename)
     free(_path);
-  return ret;
+  if (ret != 0)
+    return ret;
+  if (st.st_size > UINT32_MAX) {
+    errno = EOVERFLOW;
+    return -1;
+  }
+  _Stat->st_dev=st.st_dev;
+  _Stat->st_ino=st.st_ino;
+  _Stat->st_mode=st.st_mode;
+  _Stat->st_nlink=st.st_nlink;
+  _Stat->st_uid=st.st_uid;
+  _Stat->st_gid=st.st_gid;
+  _Stat->st_rdev=st.st_rdev;
+  _Stat->st_size=(_off_t) st.st_size;
+  _Stat->st_atime=st.st_atime;
+  _Stat->st_mtime=st.st_mtime;
+  _Stat->st_ctime=st.st_ctime;
+  return 0;
 }
 int (__cdecl *__MINGW_IMP_SYMBOL(stat32))(const char *, struct _stat32 *) = 
stat32;
 
diff --git a/mingw-w64-crt/stdio/stat64i32.c 
b/mingw-w64-crt/stdio/msvcr110pre_stat64i32.c
similarity index 57%
rename from mingw-w64-crt/stdio/stat64i32.c
rename to mingw-w64-crt/stdio/msvcr110pre_stat64i32.c
index 2799ed42e368..8a1d6dbab74c 100644
--- a/mingw-w64-crt/stdio/stat64i32.c
+++ b/mingw-w64-crt/stdio/msvcr110pre_stat64i32.c
@@ -6,17 +6,41 @@
 
 #include <sys/stat.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 char *__mingw_fix_stat_path(const char *_path);
 
+/* For pre-msvcr110 builds, we cannot use _stat64i32() function as it does
+ * not signal EOVERFLOW when file size does not fit into the st_size field,
+ * as it is required by POSIX stat() function.
+ * This file is used only for pre-msvcr110 builds.
+ */
 int __cdecl stat64i32(const char *_Filename, struct _stat64i32 *_Stat);
 int __cdecl stat64i32(const char *_Filename, struct _stat64i32 *_Stat)
 {
+  struct _stat64 st;
   char *_path = __mingw_fix_stat_path(_Filename);
-  int ret = _stat64i32(_path, _Stat);
+  int ret = _stat64(_path, &st);
   if (_path != _Filename)
     free(_path);
-  return ret;
+  if (ret != 0)
+    return ret;
+  if (st.st_size > UINT32_MAX) {
+    errno = EOVERFLOW;
+    return -1;
+  }
+  _Stat->st_dev=st.st_dev;
+  _Stat->st_ino=st.st_ino;
+  _Stat->st_mode=st.st_mode;
+  _Stat->st_nlink=st.st_nlink;
+  _Stat->st_uid=st.st_uid;
+  _Stat->st_gid=st.st_gid;
+  _Stat->st_rdev=st.st_rdev;
+  _Stat->st_size=(_off_t) st.st_size;
+  _Stat->st_atime=st.st_atime;
+  _Stat->st_mtime=st.st_mtime;
+  _Stat->st_ctime=st.st_ctime;
+  return 0;
 }
 int (__cdecl *__MINGW_IMP_SYMBOL(stat64i32))(const char *, struct _stat64i32 
*) = stat64i32;
 
diff --git a/mingw-w64-crt/stdio/wstat32.c 
b/mingw-w64-crt/stdio/msvcr110pre_wstat32.c
similarity index 57%
rename from mingw-w64-crt/stdio/wstat32.c
rename to mingw-w64-crt/stdio/msvcr110pre_wstat32.c
index 0123148d56b6..d6346834f056 100644
--- a/mingw-w64-crt/stdio/wstat32.c
+++ b/mingw-w64-crt/stdio/msvcr110pre_wstat32.c
@@ -6,17 +6,41 @@
 
 #include <sys/stat.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 wchar_t *__mingw_fix_wstat_path(const wchar_t *_path);
 
+/* For pre-msvcr110 builds, we cannot use _wstat32() function as it does
+ * not signal EOVERFLOW when file size does not fit into the st_size field,
+ * as it is required by POSIX stat() function.
+ * This file is used only for pre-msvcr110 builds.
+ */
 int __cdecl wstat32(const wchar_t *_Filename, struct _stat32 *_Stat);
 int __cdecl wstat32(const wchar_t *_Filename, struct _stat32 *_Stat)
 {
+  struct _stat32i64 st;
   wchar_t *_path = __mingw_fix_wstat_path(_Filename);
-  int ret = _wstat32(_path, _Stat);
+  int ret = _wstat32i64(_path, &st);
   if (_path != _Filename)
     free(_path);
-  return ret;
+  if (ret != 0)
+    return ret;
+  if (st.st_size > UINT32_MAX) {
+    errno = EOVERFLOW;
+    return -1;
+  }
+  _Stat->st_dev=st.st_dev;
+  _Stat->st_ino=st.st_ino;
+  _Stat->st_mode=st.st_mode;
+  _Stat->st_nlink=st.st_nlink;
+  _Stat->st_uid=st.st_uid;
+  _Stat->st_gid=st.st_gid;
+  _Stat->st_rdev=st.st_rdev;
+  _Stat->st_size=(_off_t) st.st_size;
+  _Stat->st_atime=st.st_atime;
+  _Stat->st_mtime=st.st_mtime;
+  _Stat->st_ctime=st.st_ctime;
+  return 0;
 }
 int (__cdecl *__MINGW_IMP_SYMBOL(wstat32))(const wchar_t *, struct _stat32 *) 
= wstat32;
 
diff --git a/mingw-w64-crt/stdio/wstat64i32.c 
b/mingw-w64-crt/stdio/msvcr110pre_wstat64i32.c
similarity index 58%
rename from mingw-w64-crt/stdio/wstat64i32.c
rename to mingw-w64-crt/stdio/msvcr110pre_wstat64i32.c
index 1ad9b53b45f3..42aa46de743f 100644
--- a/mingw-w64-crt/stdio/wstat64i32.c
+++ b/mingw-w64-crt/stdio/msvcr110pre_wstat64i32.c
@@ -6,17 +6,41 @@
 
 #include <sys/stat.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 wchar_t *__mingw_fix_wstat_path(const wchar_t *_path);
 
+/* For pre-msvcr110 builds, we cannot use _wstat64i32() function as it does
+ * not signal EOVERFLOW when file size does not fit into the st_size field,
+ * as it is required by POSIX stat() function.
+ * This file is used only for pre-msvcr110 builds.
+ */
 int __cdecl wstat64i32(const wchar_t *_Filename, struct _stat64i32 *_Stat);
 int __cdecl wstat64i32(const wchar_t *_Filename, struct _stat64i32 *_Stat)
 {
+  struct _stat64 st;
   wchar_t *_path = __mingw_fix_wstat_path(_Filename);
-  int ret = _wstat64i32(_path, _Stat);
+  int ret = _wstat64(_path, &st);
   if (_path != _Filename)
     free(_path);
-  return ret;
+  if (ret != 0)
+    return ret;
+  if (st.st_size > UINT32_MAX) {
+    errno = EOVERFLOW;
+    return -1;
+  }
+  _Stat->st_dev=st.st_dev;
+  _Stat->st_ino=st.st_ino;
+  _Stat->st_mode=st.st_mode;
+  _Stat->st_nlink=st.st_nlink;
+  _Stat->st_uid=st.st_uid;
+  _Stat->st_gid=st.st_gid;
+  _Stat->st_rdev=st.st_rdev;
+  _Stat->st_size=(_off_t) st.st_size;
+  _Stat->st_atime=st.st_atime;
+  _Stat->st_mtime=st.st_mtime;
+  _Stat->st_ctime=st.st_ctime;
+  return 0;
 }
 int (__cdecl *__MINGW_IMP_SYMBOL(wstat64i32))(const wchar_t *, struct 
_stat64i32 *) = wstat64i32;
 
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to