File core-levelzero-Set-ZES_ENABLE_SYSMAN-via-setenv-instead-of-putenv.patch of Package hwloc.28837

From: Egbert Eich <eich@suse.com>
Date: Tue Apr 11 14:25:19 2023 +0200
Subject: core+levelzero: Set ZES_ENABLE_SYSMAN via setenv instead of putenv
Patch-mainline: Not yet
Git-commit: 649d47b7a7f15456c75613d5d54f22b5e6623ee5
References: 

Backported from:
Git-repo: https://github.com/open-mpi/hwloc
Git-commit: fe363de1647013c190ac3e9d25bb1d7d3fbe574d

Setting `ZES_ENABLE_SYSMAN` via `putenv` placed a constant string
in the environ array which cannot be touched. If the user is
manipulating that environ array then touching this envar will
result in a segv.
- Instead of using `putenv` use `setenv` which will put a copy
  of the constant string in the `environ` array allowing the
  end user to manipulate that array as needed.
- Note that I could not find a `setenv` function for windows
  so I left a comment and did not touch that code.

Signed-off-by: Joshua Hursey <jhursey@us.ibm.com>
Also change the putenv() inside topology-levelzero.c for consistency
and update some comments.
Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
Signed-off-by: Egbert Eich <eich@suse.com>
Signed-off-by: Egbert Eich <eich@suse.de>
---
 hwloc/topology-levelzero.c | 7 ++++++-
 hwloc/topology.c           | 8 +++++---
 2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/hwloc/topology-levelzero.c b/hwloc/topology-levelzero.c
index dc8f4fe..42401aa 100644
--- a/hwloc/topology-levelzero.c
+++ b/hwloc/topology-levelzero.c
@@ -44,7 +44,12 @@ hwloc_levelzero_discover(struct hwloc_backend *backend, struct hwloc_disc_status
    */
   env = getenv("ZES_ENABLE_SYSMAN");
   if (!env) {
-    putenv((char *) "ZES_ENABLE_SYSMAN=1");
+    /* setenv() is safer than putenv() but not available on Windows */
+#ifdef HWLOC_WIN_SYS
+    putenv("ZES_ENABLE_SYSMAN=1")
+#else
+    setenv("ZES_ENABLE_SYSMAN", "1", 1);
+#endif
     /* we'll warn below if we fail to get zes devices */
     sysman_maybe_missing = 1;
   } else if (!atoi(env)) {
diff --git a/hwloc/topology.c b/hwloc/topology.c
index 01e5a86..ca04b4d 100644
--- a/hwloc/topology.c
+++ b/hwloc/topology.c
@@ -3,6 +3,7 @@
  * Copyright © 2009-2021 Inria.  All rights reserved.
  * Copyright © 2009-2012, 2020 Université Bordeaux
  * Copyright © 2009-2011 Cisco Systems, Inc.  All rights reserved.
+ * Copyright © 2022 IBM Corporation.  All rights reserved.
  * See COPYING in top-level directory.
  */
 
@@ -60,21 +61,21 @@
  *
  * L0 seems to be using getenv() to check this variable on Windows
  * (at least in the Intel Compute-Runtime of March 2021),
- * so use putenv() to set the variable.
+ * but setenv() doesn't seem to exist on Windows, hence use putenv() to set the variable.
  *
  * For the record, Get/SetEnvironmentVariable() is not exactly the same as getenv/putenv():
  * - getenv() doesn't see what was set with SetEnvironmentVariable()
  * - GetEnvironmentVariable() doesn't see putenv() in cygwin (while it does in MSVC and MinGW).
  * Hence, if L0 ever switches from getenv() to GetEnvironmentVariable(),
  * it will break in cygwin, we'll have to use both putenv() and SetEnvironmentVariable().
- * Hopefully L0 will be provide a way to enable Sysman without env vars before it happens.
+ * Hopefully L0 will provide a way to enable Sysman without env vars before it happens.
  */
 #ifdef HWLOC_HAVE_ATTRIBUTE_CONSTRUCTOR
 static void hwloc_constructor(void) __attribute__((constructor));
 static void hwloc_constructor(void)
 {
   if (!getenv("ZES_ENABLE_SYSMAN"))
-    putenv((char *) "ZES_ENABLE_SYSMAN=1");
+    setenv("ZES_ENABLE_SYSMAN", "1", 1);
 }
 #endif
 #ifdef HWLOC_WIN_SYS
@@ -82,6 +83,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
 {
   if (fdwReason == DLL_PROCESS_ATTACH) {
     if (!getenv("ZES_ENABLE_SYSMAN"))
+      /* Windows does not have a setenv, so use putenv. */
       putenv((char *) "ZES_ENABLE_SYSMAN=1");
   }
   return TRUE;
openSUSE Build Service is sponsored by