How about using _alloca to allocate buffer? Assert messages are usually not 
very long.
________________________________
From: Pali Rohár <[email protected]>
Sent: Sunday, November 16, 2025 4:40 AM
To: [email protected] 
<[email protected]>
Subject: [Mingw-w64-public] [PATCH] crt: Check for malloc failures in mingw-w64 
_wassert emulation

If malloc fails then use prepared static buffers. They are thread unsafe,
but works always. The last byte of buffers is not touched so buffers are
always nul terminated and so accessing buffers in thread unsafe way do not
cause invalid memory access.
---
 mingw-w64-crt/misc/wassert.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/mingw-w64-crt/misc/wassert.c b/mingw-w64-crt/misc/wassert.c
index 690239423b75..104cbd00302c 100644
--- a/mingw-w64-crt/misc/wassert.c
+++ b/mingw-w64-crt/misc/wassert.c
@@ -11,18 +11,30 @@
 __MINGW_ATTRIB_NORETURN
 static void __cdecl emu__wassert(const wchar_t *_Message, const wchar_t 
*_File, unsigned _Line)
 {
+    static char static_message_buf[128]; /* thread unsafe */
+    static char static_file_buf[_MAX_PATH]; /* thread unsafe */
     char *message = NULL, *file = NULL;
     size_t len;

     if ((len = wcstombs(NULL, _Message, 0)) != (size_t)-1)
     {
         message = malloc(len + 1);
+        if (!message)
+        {
+            message = static_message_buf;
+            len = sizeof(static_message_buf) - 2; /* -1 to not touch the last 
nul byte */
+        }
         wcstombs(message, _Message, len + 1);
     }

     if ((len = wcstombs(NULL, _File, 0)) != (size_t)-1)
     {
         file = malloc(len + 1);
+        if (!file)
+        {
+            file = static_file_buf;
+            len = sizeof(static_file_buf) - 2; /* -1 to not touch the last nul 
byte */
+        }
         wcstombs(file, _File, len + 1);
     }

--
2.20.1



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

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

Reply via email to