This prevents memory corruption if a newer app or dll is used with an
older cygwin dll.  This is an unsupported scenario, but it's still a
good idea to avoid corrupting memory if possible.

Fixes: 7d5c55faa1 ("Cygwin: add wrappers for newer new/delete overloads")
Co-authored-by: Corinna Vinschen <cori...@vinschen.de>
Signed-off-by: Jeremy Drake <cyg...@jdrake.com>
---

I left out initializing dll_major/dll_minor in dcrt0.cc as these fields
are initialized in globals.cc already.  I also continue to update the
__cygwin_cxx_malloc struct even though I don't think anything should be
using it (rather the default_cygwin_cxx_malloc via the user_data pointer).
It's not static for some reason, so something *could* be accessing it I
guess.

 winsup/cygwin/dcrt0.cc                   |  6 ++++
 winsup/cygwin/include/cygwin/version.h   |  3 ++
 winsup/cygwin/lib/_cygwin_crt0_common.cc | 37 ++++++++++++++----------
 3 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 69c233c247..1a4ffc8925 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -724,6 +724,12 @@ dll_crt0_0 ()
   lock_process::init ();
   user_data->impure_ptr = _impure_ptr;
   user_data->impure_ptr_ptr = &_impure_ptr;
+  /* API version info is used by newer _cygwin_crt0_common to handle
+     certain issues in a forward compatible way.  _cygwin_crt0_common
+     overwrites these values with the application's version info at the
+     time of building the app, as usual. */
+  user_data->api_major = cygwin_version.api_major;
+  user_data->api_minor = cygwin_version.api_minor;

   DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
                   GetCurrentProcess (), &hMainThread,
diff --git a/winsup/cygwin/include/cygwin/version.h 
b/winsup/cygwin/include/cygwin/version.h
index f3321020f7..00eedeb27a 100644
--- a/winsup/cygwin/include/cygwin/version.h
+++ b/winsup/cygwin/include/cygwin/version.h
@@ -36,6 +36,9 @@ details. */
 #define CYGWIN_VERSION_CHECK_FOR_EXTRA_TM_MEMBERS \
   (CYGWIN_VERSION_USER_API_VERSION_COMBINED >= 272)

+#define CYGWIN_VERSION_CHECK_FOR_CXX17_OVERLOADS(u) \
+  (CYGWIN_VERSION_PER_PROCESS_API_VERSION_COMBINED (u) >= 359)
+
 /* API_MAJOR 0.0: Initial version.  API_MINOR changes:
     1: Export cygwin32_ calls as cygwin_ as well.
     2: Export j1, jn, y1, yn.
diff --git a/winsup/cygwin/lib/_cygwin_crt0_common.cc 
b/winsup/cygwin/lib/_cygwin_crt0_common.cc
index 5900e6315d..87f3e8042b 100644
--- a/winsup/cygwin/lib/_cygwin_crt0_common.cc
+++ b/winsup/cygwin/lib/_cygwin_crt0_common.cc
@@ -124,6 +124,9 @@ _cygwin_crt0_common (MainFunc f, per_process *u)
 {
   per_process *newu = (per_process *) cygwin_internal (CW_USER_DATA);
   bool uwasnull;
+  bool new_dll_with_additional_operators =
+       newu ? CYGWIN_VERSION_CHECK_FOR_CXX17_OVERLOADS (newu)
+            : false;

   /* u is non-NULL if we are in a DLL, and NULL in the main exe.
      newu is the Cygwin DLL's internal per_process and never NULL.  */
@@ -180,8 +183,11 @@ _cygwin_crt0_common (MainFunc f, per_process *u)
     {
       /* Inherit what we don't override.  */
 #define CONDITIONALLY_OVERRIDE(MEMBER) \
-      if (!__cygwin_cxx_malloc.MEMBER) \
+      if (__cygwin_cxx_malloc.MEMBER) \
+       newu->cxx_malloc->MEMBER = __cygwin_cxx_malloc.MEMBER; \
+      else \
        __cygwin_cxx_malloc.MEMBER = newu->cxx_malloc->MEMBER;
+
       CONDITIONALLY_OVERRIDE(oper_new);
       CONDITIONALLY_OVERRIDE(oper_new__);
       CONDITIONALLY_OVERRIDE(oper_delete);
@@ -190,20 +196,21 @@ _cygwin_crt0_common (MainFunc f, per_process *u)
       CONDITIONALLY_OVERRIDE(oper_new___nt);
       CONDITIONALLY_OVERRIDE(oper_delete_nt);
       CONDITIONALLY_OVERRIDE(oper_delete___nt);
-      CONDITIONALLY_OVERRIDE(oper_delete_sz);
-      CONDITIONALLY_OVERRIDE(oper_delete___sz);
-      CONDITIONALLY_OVERRIDE(oper_new_al);
-      CONDITIONALLY_OVERRIDE(oper_new___al);
-      CONDITIONALLY_OVERRIDE(oper_delete_al);
-      CONDITIONALLY_OVERRIDE(oper_delete___al);
-      CONDITIONALLY_OVERRIDE(oper_delete_sz_al);
-      CONDITIONALLY_OVERRIDE(oper_delete___sz_al);
-      CONDITIONALLY_OVERRIDE(oper_new_al_nt);
-      CONDITIONALLY_OVERRIDE(oper_new___al_nt);
-      CONDITIONALLY_OVERRIDE(oper_delete_al_nt);
-      CONDITIONALLY_OVERRIDE(oper_delete___al_nt);
-      /* Now update the resulting set into the global redirectors.  */
-      *newu->cxx_malloc = __cygwin_cxx_malloc;
+      if (new_dll_with_additional_operators)
+       {
+         CONDITIONALLY_OVERRIDE(oper_delete_sz);
+         CONDITIONALLY_OVERRIDE(oper_delete___sz);
+         CONDITIONALLY_OVERRIDE(oper_new_al);
+         CONDITIONALLY_OVERRIDE(oper_new___al);
+         CONDITIONALLY_OVERRIDE(oper_delete_al);
+         CONDITIONALLY_OVERRIDE(oper_delete___al);
+         CONDITIONALLY_OVERRIDE(oper_delete_sz_al);
+         CONDITIONALLY_OVERRIDE(oper_delete___sz_al);
+         CONDITIONALLY_OVERRIDE(oper_new_al_nt);
+         CONDITIONALLY_OVERRIDE(oper_new___al_nt);
+         CONDITIONALLY_OVERRIDE(oper_delete_al_nt);
+         CONDITIONALLY_OVERRIDE(oper_delete___al_nt);
+       }
     }

   /* Setup the module handle so fork can get the path name.  */
-- 
2.50.1.windows.1

Reply via email to