Skip to content
Tcl Source Code Artifact [35a9ed67af]
Login

Artifact [35a9ed67af]

Artifact 35a9ed67af117697ed126d06a636a5854b0b1f6c3d7b6a62fac5414582b06352:

Attachment "7371b6270b.diff" to ticket [7371b6270b] added by chrstphrchvz on 2023-08-04 04:44:01.
diff --git generic/tclBasic.c generic/tclBasic.c
index da8870d928..d02b8cb076 100644
--- generic/tclBasic.c
+++ generic/tclBasic.c
@@ -8816,6 +8816,13 @@ NRCoroutineExitCallback(
  *----------------------------------------------------------------------
  */
 
+#ifdef _MSC_VER
+#include <intrin.h> /* for _AddressOfReturnAddress() */
+#endif
+#ifndef __has_builtin
+#define __has_builtin(x) 0 /* for non-clang compilers */
+#endif
+
 int
 TclNRCoroutineActivateCallback(
     ClientData data[],
@@ -8824,8 +8831,20 @@ TclNRCoroutineActivateCallback(
 {
     CoroutineData *corPtr = (CoroutineData *)data[0];
     int type = PTR2INT(data[1]);
-    int numLevels, unused;
+    int numLevels;
+
+/*
+ * See bug 7371b6270b: Prefer an approach which works even when
+ * AddressSanitizer (ASan) use-after-return detection is enabled.
+ */
+#ifdef _MSC_VER
+    void *stackLevel = _AddressOfReturnAddress();
+#elif defined(__GNUC__) || __has_builtin(__builtin_frame_address)
+    void *stackLevel = __builtin_frame_address(0);
+#else
+    int unused;
     int *stackLevel = &unused;
+#endif
 
     if (!corPtr->stackLevel) {
         /*