On Thu, 9 Oct 2025, Pali Rohár wrote:
On Thursday 09 October 2025 16:07:34 Martin Storsjö wrote:
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
Ou, I really forgot to git add it. Here is the missing part:
fixup! crt: Provide ftruncate64 function via wrapper around native CRT function
_chsize_s
Thanks; with this inline patch manually applied, I managed to get the
build to proceed a bit further.
Building GCC/libstdc++ configured for UCRT now fails:
https://github.com/mstorsjo/mingw-w64/actions/runs/18460823661/job/52591697068
/home/runner/work/mingw-w64/mingw-w64/gcc-14.1.0/build/../../prefix/x86_64-w64-mingw32/bin/ld:
../src/c++17/.libs/libc++17convenience.a(cow-fs_ops.o): in function
`std::filesystem::__gnu_posix::truncate64(wchar_t const*, long long)':
/home/runner/work/mingw-w64/mingw-w64/gcc-14.1.0/build/x86_64-w64-mingw32/libstdc++-v3/src/c++17/../../../../../libstdc++-v3/src/c++17/../filesystem/ops-common.h:175:(.text$_ZNSt10filesystem11resize_fileERKNS_4pathEyRSt10error_code+0x4b):
undefined reference to `ftruncate64'
/home/runner/work/mingw-w64/mingw-w64/gcc-14.1.0/build/../../prefix/x86_64-w64-mingw32/bin/ld:
../src/c++17/.libs/libc++17convenience.a(fs_ops.o): in function
`std::filesystem::__gnu_posix::truncate64(wchar_t const*, long long)':
/home/runner/work/mingw-w64/mingw-w64/gcc-14.1.0/build/x86_64-w64-mingw32/libstdc++-v3/src/c++17/../../../../../libstdc++-v3/src/c++17/../filesystem/ops-common.h:175:(.text$_ZNSt10filesystem11resize_fileERKNS_7__cxx114pathEyRSt10error_code+0x4b):
undefined reference to `ftruncate64'
collect2: error: ld returned 1 exit status
make[5]: *** [Makefile:764: libstdc++.la] Error 1
It really feels like a waste of time that I have to try to apply your
patches from email and feed them to a system for testing compilation, and
then manually feedback the test results to you.
// Martin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public