On bug-gnulib, there is a report about a crash in the gdtoa code.[1] I
mentioned this on IRC a few days ago and there was some discussion, but
no solution.

I built Gnulib's test-c-strtof-mt with mingw-w64 14.0.0 and GCC 16.2.0.
I see the crash with that mingw-w64 version and also after the recent
commits to gdtoa/misc.c. The report on bug-gnulib says that the crash
occurs in 3 out of 10 runs or 1 out of 10 runs. For me it could take
over a hundred runs until it occurred.

test-c-strtof-mt returns from main() when other threads are still
running and calling the gdtoa code. Returning from main() results in a
call to dtoa_lock_cleanup(). This breaks the CRITICAL_SECTIONs that are
still used by the other threads. As far as I know, returning from
main() when other threads are running isn't (always) a bug. At least
the libc should tolerate that.

If I remove the function dtoa_lock_cleanup() from gdtoa/misc.c, then
test-c-strtof-mt doesn't crash anymore (the test passed over a thousand
times). Removing it should be OK when the code is used in an
application: the leak occurs when the application is exiting anyway.

If the code is used in a DLL, unloading the DLL will then leak the
CRITICAL_SECTIONs. However, the locking protects freelist[], and the
memory allocated there isn't freed by anything now, so leaking also the
CRITICAL_SECTIONs might be OK at least as a temporary solution.

I attached a patch that removes dtoa_lock_cleanup(). It's a hack, but I
know too little about Windows to suggest anything better.

[1] https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00002.html

-- 
Lasse Collin
>From d1c13151f6a1bff34fa7c27199cfd765273f6774 Mon Sep 17 00:00:00 2001
From: Lasse Collin <[email protected]>
Date: Thu, 17 Sep 2026 18:14:11 +0300
Subject: [PATCH] crt: gdtoa: Avoid a crash with threads when main() returns

When main() returns or exit() is called, dtoa_lock_cleanup() was called.
If other threads were still running and calling the gdtoa code, the
application could crash.

Workaround the issue by removing dtoa_lock_cleanup(). This introduces
a leak, but this file already has a leak because the memory in freelist[]
isn't freed. It shouldn't matter when this code is used in applications
since the application is already exiting, but in DLLs it might matter.

Fixes: https://lists.gnu.org/archive/html/bug-gnulib/2026-09/msg00002.html
---
 mingw-w64-crt/gdtoa/misc.c | 11 -----------
 1 file changed, 11 deletions(-)

diff --git a/mingw-w64-crt/gdtoa/misc.c b/mingw-w64-crt/gdtoa/misc.c
index 960543a60..cda53fb95 100644
--- a/mingw-w64-crt/gdtoa/misc.c
+++ b/mingw-w64-crt/gdtoa/misc.c
@@ -60,17 +60,6 @@ static long dtoa_CS_init = 0;
    3 = deleted
 */
 
-__attribute__((destructor))
-static void dtoa_lock_cleanup (void)
-{
-       long last_CS_init = InterlockedExchange (&dtoa_CS_init,3);
-       if (2 == last_CS_init) {
-               int i;
-               for (i = 0; i < NLOCKS; i++)
-                       DeleteCriticalSection (&dtoa_CritSec[i]);
-       }
-}
-
 static void dtoa_lock (unsigned int n)
 {
        if (2 == dtoa_CS_init) {
-- 
2.55.0

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

Reply via email to