On Thursday 27 March 2025 14:57:39 Martin Storsjö wrote:
> On Thu, 20 Mar 2025, Pali Rohár wrote:
> 
> > They are natively available since msvcr80.dll, they are not available in
> > msvcrt.dll. Provide compatibility emulation into all import libraries where
> > they are not available.
> > 
> > Adjust check for _aligned_msize() which is also available since
> > msvcr80.dll.
> > ---
> > mingw-w64-crt/Makefile.am                     |  6 ++++++
> > mingw-w64-crt/misc/_aligned_offset_recalloc.c | 16 ++++++++++++++++
> > mingw-w64-crt/misc/_aligned_recalloc.c        | 16 ++++++++++++++++
> > mingw-w64-crt/misc/_recalloc.c                | 16 ++++++++++++++++
> > mingw-w64-headers/crt/crtdbg.h                |  2 +-
> > mingw-w64-headers/crt/malloc.h                |  2 +-
> > mingw-w64-headers/crt/stdlib.h                |  2 +-
> > 7 files changed, 57 insertions(+), 3 deletions(-)
> > create mode 100644 mingw-w64-crt/misc/_aligned_offset_recalloc.c
> > create mode 100644 mingw-w64-crt/misc/_aligned_recalloc.c
> > create mode 100644 mingw-w64-crt/misc/_recalloc.c
> > 
> > diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
> > index 71368648cca3..b90920e468fe 100644
> > --- a/mingw-w64-crt/Makefile.am
> > +++ b/mingw-w64-crt/Makefile.am
> > @@ -333,7 +333,10 @@ src_msvcrt=\
> >   misc/__iswcsymf.c \
> >   misc/__sys_errlist.c \
> >   misc/__sys_nerr.c \
> > +  misc/_aligned_offset_recalloc.c \
> > +  misc/_aligned_recalloc.c \
> >   misc/_configthreadlocale.c \
> > +  misc/_recalloc.c \
> >   misc/_set_purecall_handler.c \
> >   misc/imaxdiv.c \
> >   misc/invalid_parameter_handler.c \
> > @@ -820,9 +823,12 @@ src_pre_msvcr80=\
> >   misc/__iswcsymf.c \
> >   misc/__sys_errlist.c \
> >   misc/__sys_nerr.c \
> > +  misc/_aligned_offset_recalloc.c \
> > +  misc/_aligned_recalloc.c \
> >   misc/_configthreadlocale.c \
> >   misc/_get_errno.c \
> >   misc/_initterm_e.c \
> > +  misc/_recalloc.c \
> >   misc/_set_errno.c \
> >   misc/btowc.c \
> >   misc/imaxabs.c \
> > diff --git a/mingw-w64-crt/misc/_aligned_offset_recalloc.c 
> > b/mingw-w64-crt/misc/_aligned_offset_recalloc.c
> > new file mode 100644
> > index 000000000000..caccc29f2360
> > --- /dev/null
> > +++ b/mingw-w64-crt/misc/_aligned_offset_recalloc.c
> > @@ -0,0 +1,16 @@
> > +/**
> > + * This file has no copyright assigned and is placed in the Public Domain.
> > + * This file is part of the mingw-w64 runtime package.
> > + * No warranty is given; refer to the file DISCLAIMER.PD within this 
> > package.
> > + */
> > +
> > +#include <malloc.h>
> > +
> > +void * __cdecl _aligned_offset_recalloc(void *memory, size_t count, size_t 
> > size, size_t alignment, size_t offset)
> > +{
> > +  size_t total_size;
> > +  if (__builtin_mul_overflow(count, size, &total_size))
> > +    return NULL;
> > +  return _aligned_offset_realloc(memory, total_size, alignment, offset);
> > +}
> 
> FWIW, this fails to build on non-x86 platforms; NULL requires a header
> declaring it. On x86, malloc.h includes mm_malloc.h and gets a definition
> from there, but for other architectures, we explicitly need to include e.g.
> stddef.h here (and in most other files added in this commit).
> 
> // Martin

What about following fix? Just an idea, I have not tested it yet:

diff --git a/mingw-w64-crt/misc/_aligned_offset_recalloc.c 
b/mingw-w64-crt/misc/_aligned_offset_recalloc.c
index caccc29f2360..297393a2656a 100644
--- a/mingw-w64-crt/misc/_aligned_offset_recalloc.c
+++ b/mingw-w64-crt/misc/_aligned_offset_recalloc.c
@@ -5,12 +5,21 @@
  */
 
 #include <malloc.h>
+#include <string.h>
 
 void * __cdecl _aligned_offset_recalloc(void *memory, size_t count, size_t 
size, size_t alignment, size_t offset)
 {
+  void *new_memory;
+  size_t previous_size;
   size_t total_size;
   if (__builtin_mul_overflow(count, size, &total_size))
     return NULL;
-  return _aligned_offset_realloc(memory, total_size, alignment, offset);
+  previous_size = memory ? _aligned_msize(memory, alignment, offset) : 0;
+  if (previous_size == (size_t)-1)
+    return NULL;
+  new_memory = _aligned_offset_realloc(memory, total_size, alignment, offset);
+  if (new_memory && previous_size < total_size)
+    memset(new_memory + previous_size, 0, total_size - previous_size);
+  return new_memory;
 }
 void * (__cdecl *__MINGW_IMP_SYMBOL(_aligned_offset_recalloc))(void *, size_t, 
size_t, size_t, size_t) = _aligned_offset_recalloc;
diff --git a/mingw-w64-crt/misc/_aligned_recalloc.c 
b/mingw-w64-crt/misc/_aligned_recalloc.c
index b5ba8ed807ef..e374d0907d99 100644
--- a/mingw-w64-crt/misc/_aligned_recalloc.c
+++ b/mingw-w64-crt/misc/_aligned_recalloc.c
@@ -5,12 +5,21 @@
  */
 
 #include <malloc.h>
+#include <string.h>
 
 void * __cdecl _aligned_recalloc(void *memory, size_t count, size_t size, 
size_t alignment)
 {
+  void *new_memory;
+  size_t previous_size;
   size_t total_size;
   if (__builtin_mul_overflow(count, size, &total_size))
     return NULL;
-  return _aligned_realloc(memory, total_size, alignment);
+  previous_size = memory ? _aligned_msize(memory, alignment, 0) : 0;
+  if (previous_size == (size_t)-1)
+    return NULL;
+  new_memory = _aligned_realloc(memory, total_size, alignment);
+  if (new_memory && previous_size < total_size)
+    memset(new_memory + previous_size, 0, total_size - previous_size);
+  return new_memory;
 }
 void * (__cdecl *__MINGW_IMP_SYMBOL(_aligned_recalloc))(void *, size_t, 
size_t, size_t) = _aligned_recalloc;
diff --git a/mingw-w64-crt/misc/_recalloc.c b/mingw-w64-crt/misc/_recalloc.c
index 5eaecccf1306..b78c2078525e 100644
--- a/mingw-w64-crt/misc/_recalloc.c
+++ b/mingw-w64-crt/misc/_recalloc.c
@@ -5,12 +5,19 @@
  */
 
 #include <malloc.h>
+#include <string.h>
 
 void * __cdecl _recalloc(void *memory, size_t count, size_t size)
 {
+  void *new_memory;
+  size_t previous_size;
   size_t total_size;
   if (__builtin_mul_overflow(count, size, &total_size))
     return NULL;
-  return realloc(memory, total_size);
+  previous_size = memory ? _msize(memory) : 0;
+  new_memory = realloc(memory, total_size);
+  if (new_memory && previous_size < total_size)
+    memset(new_memory + previous_size, 0, total_size - previous_size);
+  return new_memory;
 }
 void * (__cdecl *__MINGW_IMP_SYMBOL(_recalloc))(void *, size_t, size_t) = 
_recalloc;


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

Reply via email to