On Sun, 5 Oct 2025, Pali Rohár wrote:

msvcrt and UCRT function _chsize() has same behavior and also ABI as POSIX
function ftruncate() with 32-bit length.

For 64-bit length msvcr80+ and UCRT provide function _chksize_s() but it
has slightly different usage as _chksize() / ftruncate() and so cannot be
directly aliased at symbol ABI level.

Function _chksize_s() calls invalid parameter exception handler and return
value is errno.

mingw-w64 currently provides its own ftruncate64() implementation which is
statically linked into every binary which calls ftruncate64().

For msvcr80+ and UCRT builds, provide a new ftruncate64() implementation as
a simple wrapper around the CRT _chksize_s() function. To prevent calling
invalid parameter exception handler, validate input parametes manually
before calling the function itself.

For pre-msvcr80 builds use the existing mingw-w64 implemenation of
ftruncate64() function.

And for OS-system msvcrt.dll builds use either the _chksize_s() or the
mingw-w64 implemation. Function symbol _chksize_s in msvcrt.dll is
available since Windows Vista, so for x86/x64 builds, resolve that symbol
at runtime.

Note that OS-system msvcrt.dll _chksize_s() does not call invalid parameter
exception handler, so call function directly without validation.

To prevent symbol conflicts between existing mingw-w64 implementation of
ftruncate64 and new wrapper implementation (around _chksize_s), rename the
existing mingw-w64 implementation to __mingw_ftruncate64.

With this change, calling the ftruncate64 function symbol results in using
the native _chksize_s CRT implementation for msvcr80+ and UCRT builds.
---
mingw-w64-crt/Makefile.am                     |  5 +-
.../{ftruncate64.c => mingw_ftruncate64.c}    |  6 ++-
mingw-w64-crt/stdio/msvcr80plus_ftruncate64.c | 30 ++++++++++++
mingw-w64-crt/stdio/msvcrtos_ftruncate64.c    | 47 +++++++++++++++++++
4 files changed, 86 insertions(+), 2 deletions(-)
rename mingw-w64-crt/stdio/{ftruncate64.c => mingw_ftruncate64.c} (98%)
create mode 100644 mingw-w64-crt/stdio/msvcr80plus_ftruncate64.c
create mode 100644 mingw-w64-crt/stdio/msvcrtos_ftruncate64.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 705624635acf..ca3e85264172 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -363,6 +363,7 @@ src_msvcrt=\
  secapi/wmemmove_s.c \
  stdio/_ftelli64.c \
  stdio/mingw_lock.c \
+  stdio/msvcrtos_ftruncate64.c \
  stdio/msvcr110pre_stat32.c \
  stdio/msvcr110pre_stat64i32.c \
  stdio/msvcr110pre_wstat32.c \
@@ -933,6 +934,7 @@ src_pre_msvcr80=\
  stdio/_stat64i32.c \
  stdio/_wstat64i32.c \
  stdio/mingw_lock.c \
+  stdio/msvcr80pre_ftruncate64.c \
  string/msvcr80pre_wcstok.c

There's no msvcr80pre_ftruncate64.c included in this patchset.

// Martin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to