On Thu, 17 Sep 2026, LIU Hao wrote:
As v1 undoes some changes in Pali's first patch, I have squashed the two
patches.
From 739c2b04470adfc476bc7738b43b9651fc1dd2a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
Date: Sun, 13 Sep 2026 21:09:03 +0200
Subject: [PATCH 1/2] crt: gdtoa: Fix race conditions during initialization of
dtoa lock
Use InterlockedExchange() for changing the dtoa_CS_init variable to ensure
that new value would be immediately visible in all threads.
When a thread saw `dtoa_CS_init == 0`, it was possible that another thread
got
scheduled and completed initialization, so `dtoa_CS_init` became 2; and then
the
current thread did `last_CS_init = InterlockedExchange (&dtoa_CS_init, 1)`
and
saw `last_CS_init == 2` and restored it with `InterlockedExchange
(&dtoa_CS_init, 2)`.
The value of `dtoa_CS_init` went from 2 to 1 to 2, which was an ABA pattern.
The other thread which actually completed initialization should only call
`EnterCriticalSection(&dtoa_CritSec[n])` when it saw `dtoa_CS_init == 2`. In
the
middle of this ABA sequence, the call might get missed.
The fix is to update `dtoa_CS_init` to 1 with a CAS from 0 so it will not be
updated from anything else.
Co-authored-by: LIU Hao <[email protected]>
Signed-off-by: LIU Hao <[email protected]>
---
mingw-w64-crt/gdtoa/misc.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/mingw-w64-crt/gdtoa/misc.c b/mingw-w64-crt/gdtoa/misc.c
index adc955ec0..74a2ca605 100644
--- a/mingw-w64-crt/gdtoa/misc.c
+++ b/mingw-w64-crt/gdtoa/misc.c
@@ -76,16 +76,13 @@ static void dtoa_lock (unsigned int n)
return;
}
else if (0 == dtoa_CS_init) {
- long last_CS_init = InterlockedExchange (&dtoa_CS_init, 1);
- if (0 == last_CS_init) {
+ if (0 == InterlockedCompareExchange (&dtoa_CS_init, 1, 0)) {
int i;
for (i = 0; i < NLOCKS; i++)
InitializeCriticalSection (&dtoa_CritSec[i]);
atexit (dtoa_lock_cleanup);
- dtoa_CS_init = 2;
+ (void)InterlockedExchange (&dtoa_CS_init, 2);
}
- else if (2 == last_CS_init)
- dtoa_CS_init = 2;
}
/* Another thread is initializing. Wait. */
while (1 == dtoa_CS_init)
--
2.55.0
Looks reasonable to me.
// Martin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public