Merge pull request #4621 from alexanderkyte/strdup_env
authorAlexander Kyte <alexmkyte@gmail.com>
Thu, 30 Mar 2017 22:17:29 +0000 (18:17 -0400)
committerGitHub <noreply@github.com>
Thu, 30 Mar 2017 22:17:29 +0000 (18:17 -0400)
[runtime] Switch getenv to use heap memory

44 files changed:
eglib/src/glib.h
eglib/src/gmisc-unix.c
eglib/src/gmisc-win32.c
mono/arch/ia64/codegen.c
mono/metadata/appdomain.c
mono/metadata/assembly.c
mono/metadata/boehm-gc.c
mono/metadata/class.c
mono/metadata/cominterop.c
mono/metadata/console-unix.c
mono/metadata/icall.c
mono/metadata/image.c
mono/metadata/locales.c
mono/metadata/lock-tracer.c
mono/metadata/mono-config.c
mono/metadata/threadpool-io.c
mono/metadata/w32file-unix.c
mono/mini/aot-runtime.c
mono/mini/debugger-agent.c
mono/mini/driver.c
mono/mini/helpers.c
mono/mini/ir-emit.h
mono/mini/liveness.c
mono/mini/llvm-jit.cpp
mono/mini/method-to-ir.c
mono/mini/mini-arm.c
mono/mini/mini-cross-helpers.c
mono/mini/mini-gc.c
mono/mini/mini-llvm.c
mono/mini/mini-ppc.h
mono/mini/mini-runtime.c
mono/mini/mini-x86.c
mono/mini/mini.c
mono/profiler/mono-profiler-log.c
mono/sgen/sgen-gc.c
mono/utils/checked-build.c
mono/utils/mono-hwcap.c
mono/utils/mono-io-portability.c
mono/utils/mono-logger.c
mono/utils/mono-mmap.c
mono/utils/mono-rand.c
mono/utils/mono-threads-coop.c
mono/utils/mono-threads.c
mono/utils/strenc.c

index 8ceec487041d6bb7c3a2a72d3f8e85cfd2ecaa78..6f16fcb292bb692f2e2fc5daa5d7c3a5a8cd0189 100644 (file)
@@ -160,7 +160,8 @@ typedef struct _GMemChunk GMemChunk;
  * Misc.
  */
 
-const gchar *    g_getenv(const gchar *variable);
+gboolean         g_hasenv(const gchar *variable);
+gchar *          g_getenv(const gchar *variable);
 gboolean         g_setenv(const gchar *variable, const gchar *value, gboolean overwrite);
 void             g_unsetenv(const gchar *variable);
 
index 273024871c5f890e01410f8a12094cf256cd33d8..2529069a981047ac63b392340c801857ca4760d4 100644 (file)
 #include <unistd.h>
 #endif
 
+static pthread_mutex_t env_lock = PTHREAD_MUTEX_INITIALIZER;
 
-const gchar *
-g_getenv(const gchar *variable)
+/* MONO Comment
+ * 
+ * As per the UNIX spec, 
+ * "The return value from getenv() may point to static data which may be overwritten by subsequent calls to getenv(), setenv(), or unsetenv()."
+ * Source: Unix Manual Pages for getenv, IEEE Std 1003.1
+ *
+ * This means that using pointers returned from getenv may (and does) lead to many
+ * pointers which refer to the same piece of memory. When one is freed, all will be freed.
+ *
+ * This is unsafe and an ergonomics risk to fix in the callers. While the caller could lock,
+ * this introduces the risk for looping or exiting while inside of a lock. For this reason,
+ * g_getenv does not mimic the behavior of POSIX getenv anymore.
+ *
+ * The memory address returned will be unique to the invocaton, and must be freed.
+ * */ 
+gchar *
+g_getenv (const gchar *variable)
+{
+       gchar *ret = NULL;
+       pthread_mutex_lock (&env_lock);
+       gchar *res = getenv(variable);
+       if (res)
+               ret = g_strdup(res);
+       pthread_mutex_unlock (&env_lock);
+
+       return ret;
+}
+
+/*
+ * This function checks if the given variable is non-NULL
+ * in the environment. It's useful because it removes memory
+ * freeing requirements.
+ *
+ */
+gboolean
+g_hasenv (const gchar *variable)
 {
-       return getenv(variable);
+       pthread_mutex_lock (&env_lock);
+       gchar *res = getenv(variable);
+       gboolean not_null = (res != NULL);
+       pthread_mutex_unlock (&env_lock);
+
+       return not_null;
 }
 
 gboolean
 g_setenv(const gchar *variable, const gchar *value, gboolean overwrite)
 {
-       return setenv(variable, value, overwrite) == 0;
+       gboolean res;
+       pthread_mutex_lock (&env_lock);
+       res = (setenv(variable, value, overwrite) == 0);
+       pthread_mutex_unlock (&env_lock);
+       return res;
 }
 
 void
 g_unsetenv(const gchar *variable)
 {
+       pthread_mutex_lock (&env_lock);
        unsetenv(variable);
+       pthread_mutex_unlock (&env_lock);
 }
 
 gchar*
index 4aac0ef69460fe70c432dfb78f8fcfd2bd931f5a..696332bcb5c5c2ab6fdfc3637e87641f2901447c 100644 (file)
 #include <io.h>
 #include <assert.h>
 
+gboolean
+g_hasenv (const gchar *variable)
+{
+       return g_getenv (variable) != NULL;
+}
+
 const gchar *
 g_getenv(const gchar *variable)
 {
index 97e1aefcb1ed5299f1aae55d93c34caf14685029..93d85a57df3ec32061809e92309d1b064a4d5649 100644 (file)
@@ -52,11 +52,12 @@ mono_disassemble_code (guint8 *code, int size, char *id)
        system (cmd); 
        g_free (cmd);
        if (!objdump_args)
-               objdump_args = "";
+               objdump_args = strdup("");
        
        cmd = g_strdup_printf (DIS_CMD " %s %s", objdump_args, o_file);
        system (cmd);
        g_free (cmd);
+       g_free (objdump_args);
        
        g_free (o_file);
        g_free (as_file);
index bfd0a23e506637525fb7cc13ec5a10c9f611398c..790dc504ed56b80fb71f8b2249a5eb0070eb2531 100644 (file)
@@ -2208,8 +2208,9 @@ ves_icall_System_AppDomain_InternalUnload (gint32 domain_id, MonoError *error)
         * Unloading seems to cause problems when running NUnit/NAnt, hence
         * this workaround.
         */
-       if (g_getenv ("MONO_NO_UNLOAD"))
+       if (g_hasenv ("MONO_NO_UNLOAD"))
                return;
+
 #ifdef __native_client__
        return;
 #endif
index 097ce878007517266c8bdec27627ea0dd25267bb..9178bb837d557a1ba439480048a49c1718a94d89 100644 (file)
@@ -295,7 +295,7 @@ mono_set_assemblies_path (const char* path)
        }
        *dest = *splitted;
 
-       if (g_getenv ("MONO_DEBUG") == NULL)
+       if (g_hasenv ("MONO_DEBUG"))
                return;
 
        splitted = assemblies_path;
@@ -316,21 +316,25 @@ char* nacl_mono_path = NULL;
 static void
 check_path_env (void)
 {
-       const char* path;
-       path = g_getenv ("MONO_PATH");
+       if (assemblies_path != NULL)
+               return;
+
+       char* path = g_getenv ("MONO_PATH");
 #ifdef __native_client__
        if (!path)
-               path = nacl_mono_path;
+               path = strdup (nacl_mono_path);
 #endif
-       if (!path || assemblies_path != NULL)
+       if (!path)
                return;
 
        mono_set_assemblies_path(path);
+       g_free (path);
 }
 
 static void
-check_extra_gac_path_env (void) {
-       const char *path;
+check_extra_gac_path_env (void) 
+{
+       char *path;
        char **splitted, **dest;
        
        path = g_getenv ("MONO_GAC_PREFIX");
@@ -338,6 +342,8 @@ check_extra_gac_path_env (void) {
                return;
 
        splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
+       g_free (path);
+
        if (extra_gac_paths)
                g_strfreev (extra_gac_paths);
        extra_gac_paths = dest = splitted;
@@ -348,7 +354,7 @@ check_extra_gac_path_env (void) {
        }
        *dest = *splitted;
        
-       if (g_getenv ("MONO_DEBUG") == NULL)
+       if (!g_hasenv ("MONO_DEBUG"))
                return;
 
        while (*splitted) {
index 107022576d02563a69f22a4e2c76e193d77f5b5b..8adf077e6b1956fca0f4f3accd195b8b72394126 100644 (file)
@@ -199,6 +199,7 @@ mono_gc_base_init (void)
                                        log_finalizers = 1;
                                }
                        }
+                       g_free (env);
                }
        }
 
@@ -242,6 +243,7 @@ mono_gc_base_init (void)
                                */
                        }
                }
+               g_free (env);
                g_strfreev (opts);
        }
 
@@ -1320,7 +1322,7 @@ mono_gc_is_moving (void)
 gboolean
 mono_gc_is_disabled (void)
 {
-       if (GC_dont_gc || g_getenv ("GC_DONT_GC"))
+       if (GC_dont_gc || g_hasenv ("GC_DONT_GC"))
                return TRUE;
        else
                return FALSE;
index 1a2970b1e6bb37ad0d35ba84fa925cb3ef372aa4..6e086cf1442a451765cc3fdb8ad2586709f0e208 100644 (file)
@@ -3687,7 +3687,7 @@ is_wcf_hack_disabled (void)
        static gboolean disabled;
        static gboolean inited = FALSE;
        if (!inited) {
-               disabled = g_getenv ("MONO_DISABLE_WCF_HACK") != NULL;
+               disabled = g_hasenv ("MONO_DISABLE_WCF_HACK");
                inited = TRUE;
        }
        return disabled;
index 84ddd824129bb829934999f43fa70cb5bf633ec1..86309ef20234d4dd559726f0c9fcfe1a32f08e32 100644 (file)
@@ -600,13 +600,15 @@ cominterop_type_from_handle (MonoType *handle)
 void
 mono_cominterop_init (void)
 {
-       const char* com_provider_env;
+       char* com_provider_env;
 
        mono_os_mutex_init_recursive (&cominterop_mutex);
 
        com_provider_env = g_getenv ("MONO_COM");
        if (com_provider_env && !strcmp(com_provider_env, "MS"))
                com_provider = MONO_COM_MS;
+       if (com_provider_env)
+               g_free (com_provider_env);
 
        register_icall (cominterop_get_method_interface, "cominterop_get_method_interface", "ptr ptr", FALSE);
        register_icall (cominterop_get_function_pointer, "cominterop_get_function_pointer", "ptr ptr int32", FALSE);
index 2ad4210f7563f413d0175f37021fcfe1b6fd8b40..98b3ba551cb005a3af4cbadc884cb576a81e4984 100644 (file)
@@ -457,12 +457,16 @@ ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardow
        if (dims == -1){
                int cols = 0, rows = 0;
                                      
-               const char *str = g_getenv ("COLUMNS");
-               if (str != NULL)
+               char *str = g_getenv ("COLUMNS");
+               if (str != NULL) {
                        cols = atoi (str);
+                       g_free (str);
+               }
                str = g_getenv ("LINES");
-               if (str != NULL)
+               if (str != NULL) {
                        rows = atoi (str);
+                       g_free (str);
+               }
 
                if (cols != 0 && rows != 0)
                        cols_and_lines = (cols << 16) | rows;
index 693c0f4135e8ca3fb83f738db147bd33392052d9..cb562cde7613af2419d55b049d8a3a7ece434449 100644 (file)
@@ -6518,7 +6518,7 @@ ves_icall_System_Environment_GetIs64BitOperatingSystem (void)
 ICALL_EXPORT MonoStringHandle
 ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_name, MonoError *error)
 {
-       const gchar *value;
+       gchar *value;
 
        if (utf8_name == NULL)
                return NULL_HANDLE_STRING;
@@ -6528,7 +6528,9 @@ ves_icall_System_Environment_GetEnvironmentVariable_native (const gchar *utf8_na
        if (value == 0)
                return NULL_HANDLE_STRING;
        
-       return mono_string_new_handle (mono_domain_get (), value, error);
+       MonoStringHandle res = mono_string_new_handle (mono_domain_get (), value, error);
+       g_free (value);
+       return res;
 }
 
 /*
index 688bc3c50ba3decfab81a59a1f5b476dfbc7aea0..eb4d686412e586b822e0f064748cc7e7117c2b64 100644 (file)
@@ -256,7 +256,7 @@ mono_images_init (void)
        for(hash_idx = 0; hash_idx < IMAGES_HASH_COUNT; hash_idx++)
                loaded_images_hashes [hash_idx] = g_hash_table_new (g_str_hash, g_str_equal);
 
-       debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
+       debug_assembly_unload = g_hasenv ("MONO_DEBUG_ASSEMBLY_UNLOAD");
 
        install_pe_loader ();
 
index 403199d0c9a2cc2d5fe1cfc6629f1bbd7d889ccf..c3800d5325fbc6e599fe7188b4150a52ee78b0e4 100644 (file)
@@ -479,22 +479,27 @@ get_darwin_locale (void)
 static char *
 get_posix_locale (void)
 {
-       const char *locale;
+       char *locale;
 
        locale = g_getenv ("LC_ALL");
        if (locale == NULL) {
                locale = g_getenv ("LANG");
-               if (locale == NULL)
-                       locale = setlocale (LC_ALL, NULL);
+               if (locale == NULL) {
+                       char *static_locale = setlocale (LC_ALL, NULL);
+                       if (static_locale)
+                               locale = g_strdup (static_locale);
+               }
        }
        if (locale == NULL)
                return NULL;
 
        /* Skip English-only locale 'C' */
-       if (strcmp (locale, "C") == 0)
+       if (strcmp (locale, "C") == 0) {
+               g_free (locale);
                return NULL;
+       }
 
-       return g_strdup (locale);
+       return locale;
 }
 
 
index 1ec84e6533be4aa4c2f718d06ab82e93e5010b3f..bda05487d5fde39a960e535e54c5e513a8e9b18d 100644 (file)
@@ -74,8 +74,10 @@ mono_locks_tracer_init (void)
        int res;
        char *name;
        mono_os_mutex_init_recursive (&tracer_lock);
-       if (!g_getenv ("MONO_ENABLE_LOCK_TRACER"))
+
+       if (!g_hasenv ("MONO_ENABLE_LOCK_TRACER"))
                return;
+
        name = g_strdup_printf ("locks.%d", getpid ());
        trace_file = fopen (name, "w+");
        g_free (name);
index 413d7c40831df779ddf96fb371932d1f9f1dd9c3..e598348f9ba23e0bdd12ccd7ae48bdd79cf8116b 100644 (file)
@@ -163,8 +163,7 @@ mono_parser = {
 
 static GHashTable *config_handlers;
 
-static const char *mono_cfg_dir = NULL;
-static char *mono_cfg_dir_allocated = NULL;
+static char *mono_cfg_dir = NULL;
 
 /* when this interface is stable, export it. */
 typedef struct MonoParseHandler MonoParseHandler;
@@ -473,7 +472,7 @@ mono_config_cleanup (void)
 {
        if (config_handlers)
                g_hash_table_destroy (config_handlers);
-       g_free (mono_cfg_dir_allocated);
+       g_free (mono_cfg_dir);
 }
 
 /* FIXME: error handling */
@@ -661,9 +660,10 @@ mono_config_parse (const char *filename) {
                return;
        }
 
-       home = g_getenv ("MONO_CONFIG");
-       if (home) {
-               mono_config_parse_file (home);
+       // FIXME: leak, do we store any references to home
+       char *env_home = g_getenv ("MONO_CONFIG");
+       if (env_home) {
+               mono_config_parse_file (env_home);
                return;
        }
 
@@ -686,10 +686,12 @@ mono_config_parse (const char *filename) {
 void
 mono_set_config_dir (const char *dir)
 {
-       /* If this variable is set, overrides the directory computed */
-       mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
-       if (mono_cfg_dir == NULL)
-               mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
+       /* If this environment variable is set, overrides the directory computed */
+       char *env_mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
+       if (env_mono_cfg_dir == NULL && dir != NULL)
+               env_mono_cfg_dir = strdup (dir);
+
+       mono_cfg_dir = env_mono_cfg_dir;
 }
 
 /**
index efff08c45240111f006b7388b90f9eb498258c29..a40ed6de5ea1e62c8c98d1082d18b5f3b919b365 100644 (file)
@@ -546,7 +546,7 @@ initialize (void)
        threadpool_io->updates_size = 0;
 
        threadpool_io->backend = backend_poll;
-       if (g_getenv ("MONO_ENABLE_AIO") != NULL) {
+       if (g_hasenv ("MONO_ENABLE_AIO")) {
 #if defined(HAVE_EPOLL)
                threadpool_io->backend = backend_epoll;
 #elif defined(HAVE_KQUEUE)
index 06aed504381819cb8b07c308a21a7a89493c8466..734b43b41f629ccad056873ed12050c78d9db1a1 100644 (file)
@@ -5078,7 +5078,7 @@ mono_w32file_init (void)
 /*     mono_w32handle_register_capabilities (MONO_W32HANDLE_CONSOLE, */
 /*                                         MONO_W32HANDLE_CAP_WAIT); */
 
-       if (g_getenv ("MONO_STRICT_IO_EMULATION"))
+       if (g_hasenv ("MONO_STRICT_IO_EMULATION"))
                lock_while_writing = TRUE;
 }
 
index 8cd1999b0857569f330efbad6c882ebc5bd68533..9279967b31afbe1f14bc3e47ee00a9625c7be3c5 100644 (file)
@@ -2356,8 +2356,11 @@ mono_aot_init (void)
 #endif
        mono_counters_register ("Async JIT info size", MONO_COUNTER_INT|MONO_COUNTER_JIT, &async_jit_info_size);
 
-       if (g_getenv ("MONO_LASTAOT"))
-               mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
+       char *lastaot = g_getenv ("MONO_LASTAOT");
+       if (lastaot) {
+               mono_last_aot_method = atoi (lastaot);
+               g_free (lastaot);
+       }
        aot_cache_init ();
 }
 
index 8cd39fc9b4329a01158cabb7e50620a08e1e4f72..8e3691db62bf3bbadad4e51ba32de3e81b8b3a57 100644 (file)
@@ -883,7 +883,7 @@ mono_debugger_agent_parse_options (char *options)
        char **args, **ptr;
        char *host;
        int port;
-       const char *extra;
+       char *extra;
 
 #ifndef MONO_ARCH_SOFT_DEBUG_SUPPORTED
        fprintf (stderr, "--debugger-agent is not supported on this platform.\n");
@@ -891,8 +891,10 @@ mono_debugger_agent_parse_options (char *options)
 #endif
 
        extra = g_getenv ("MONO_SDB_ENV_OPTIONS");
-       if (extra)
+       if (extra) {
                options = g_strdup_printf ("%s,%s", options, extra);
+               g_free (extra);
+       }
 
        agent_config.enabled = TRUE;
        agent_config.suspend = TRUE;
index cd896155a121e3cce8d4d235712cfec6a7845638..26966768f4038e98dbee439b2f59485b88d0593f 100644 (file)
@@ -1607,7 +1607,7 @@ mono_main (int argc, char* argv[])
        darwin_change_default_file_handles ();
 #endif
 
-       if (g_getenv ("MONO_NO_SMP"))
+       if (g_hasenv ("MONO_NO_SMP"))
                mono_set_use_smp (FALSE);
        
        g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
@@ -1978,7 +1978,7 @@ mono_main (int argc, char* argv[])
        }
 #endif
 
-       if (g_getenv ("MONO_XDEBUG"))
+       if (g_hasenv ("MONO_XDEBUG"))
                enable_debugging = TRUE;
 
 #ifdef MONO_CROSS_COMPILE
@@ -2564,10 +2564,11 @@ mono_parse_env_options (int *ref_argc, char **ref_argv [])
 {
        char *ret;
        
-       const char *env_options = g_getenv ("MONO_ENV_OPTIONS");
+       char *env_options = g_getenv ("MONO_ENV_OPTIONS");
        if (env_options == NULL)
                return;
        ret = mono_parse_options_from (env_options, ref_argc, ref_argv);
+       g_free (env_options);
        if (ret == NULL)
                return;
        fprintf (stderr, "%s", ret);
index d5f9545776ef4c46362d301961cd2b2bbb0a00b3..7f681b23bb5ba355da140cd5fb52c4b0fac81acb 100644 (file)
@@ -145,7 +145,7 @@ mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
 #ifdef HOST_WIN32
        const char *tmp = g_get_tmp_dir ();
 #endif
-       const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
+       char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
        char *as_file;
        char *o_file;
        char *cmd;
@@ -276,7 +276,7 @@ mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
        unused = system (cmd); 
        g_free (cmd);
        if (!objdump_args)
-               objdump_args = "";
+               objdump_args = g_strdup ("");
 
        fflush (stdout);
 
@@ -293,6 +293,7 @@ mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
        cmd = g_strdup_printf (ARCH_PREFIX DIS_CMD " %s %s", objdump_args, o_file);
        unused = system (cmd);
        g_free (cmd);
+       g_free (objdump_args);
 #else
        g_assert_not_reached ();
 #endif /* HAVE_SYSTEM */
index 859cccd112dac40e219798182f98dc0b5b98dcec..d535f25b6d311f3e566db35914720fb03e133f95 100644 (file)
@@ -813,10 +813,12 @@ static int ccount = 0;
             ins->inst_false_bb = NULL; \
             mono_link_bblock ((cfg), (cfg)->cbb, (truebb)); \
             MONO_ADD_INS ((cfg)->cbb, ins); \
-            if (g_getenv ("COUNT2") && ccount == atoi (g_getenv ("COUNT2")) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
-            if (g_getenv ("COUNT2") && ccount < atoi (g_getenv ("COUNT2"))) { \
+            char *count2 = g_getenv ("COUNT2"); \
+            if (count2 && ccount == atoi (count2) - 1) { printf ("HIT: %d\n", cfg->cbb->block_num); } \
+            if (count2 && ccount < atoi (count2)) { \
                  cfg->cbb->extended = TRUE; \
             } else { NEW_BBLOCK ((cfg), falsebb); ins->inst_false_bb = (falsebb); mono_link_bblock ((cfg), (cfg)->cbb, (falsebb)); MONO_START_BB ((cfg), falsebb); } \
+            if (count2) g_free (count2); \
         } \
        } while (0)
 #else
index b98c7a57528eb1827e5a95997bcffd63780a8d4f..eaa656e059c1b9c3894a321a9a4d6b19b24704e6 100644 (file)
@@ -857,7 +857,7 @@ mono_analyze_liveness2 (MonoCompile *cfg)
        static guint32 disabled = -1;
 
        if (disabled == -1)
-               disabled = g_getenv ("DISABLED") != NULL;
+               disabled = g_hasenv ("DISABLED");
 
        if (disabled)
                return;
index 868097e617085fc86c9dfb0007a9666922bce742..e10f65766c97c3db26ecd53d7415bbc893ff44c2 100644 (file)
@@ -530,7 +530,11 @@ static void
 force_pass_linking (void)
 {
        // Make sure the rest is linked in, but never executed
-       if (g_getenv ("FOO") != (char*)-1)
+       char *foo = g_getenv ("FOO");
+       gboolean ret = (foo != (char*)-1);
+       g_free (foo);
+
+       if (ret) 
                return;
 
        // This is a subset of the passes in LinkAllPasses.h
index 2f96bebabc34afb0c6557ceb46ff9595dafdace0..62bc0f5fc52ad32eaa28fe5b38d88c038492544f 100644 (file)
@@ -4585,9 +4585,11 @@ mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
        /* also consider num_locals? */
        /* Do the size check early to avoid creating vtables */
        if (!inline_limit_inited) {
-               if (g_getenv ("MONO_INLINELIMIT"))
-                       inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
-               else
+               char *inlinelimit;
+               if ((inlinelimit = g_getenv ("MONO_INLINELIMIT"))) {
+                       inline_limit = atoi (inlinelimit);
+                       g_free (inlinelimit);
+               } else
                        inline_limit = INLINE_LENGTH_LIMIT;
                inline_limit_inited = TRUE;
        }
index 658311340e14b515b538477422c0ade899bbcf71..f80d5f5b0272e5f6315be7c26c65cf5941b91f7c 100644 (file)
@@ -812,6 +812,7 @@ mono_arch_init (void)
 
        if (soft && !strncmp (soft, "1", 1))
                arm_fpu = MONO_ARM_FPU_NONE;
+       g_free (soft);
 #endif
 #endif
 
@@ -859,6 +860,7 @@ mono_arch_init (void)
 
                thumb_supported = strstr (cpu_arch, "thumb") != NULL;
                thumb2_supported = strstr (cpu_arch, "thumb2") != NULL;
+               g_free (cpu_arch);
        }
 }
 
index 14bd722aca127a552c040e3981bad1c77974822a..8aeb6ffb2f48e88b4a227ad352ad2b78a995d54c 100644 (file)
@@ -62,7 +62,7 @@ mono_cross_helpers_run (void)
 #endif
 
 #ifndef USED_CROSS_COMPILER_OFFSETS
-       if (g_getenv ("DUMP_CROSS_OFFSETS"))
+       if (g_hasenv ("DUMP_CROSS_OFFSETS"))
                mono_dump_jit_offsets ();
 #endif
        
index 31e775ecc9e5c7f004307a52a8ba1d897d5a0c53..3b5463e80fb1c16626c54ad26547553a3fbee682 100644 (file)
@@ -925,8 +925,11 @@ conservative_pass (TlsData *tls, guint8 *stack_start, guint8 *stack_end)
                 * Debugging aid to control the number of frames scanned precisely
                 */
                if (!precise_frame_limit_inited) {
-                       if (g_getenv ("MONO_PRECISE_COUNT"))
-                               precise_frame_limit = atoi (g_getenv ("MONO_PRECISE_COUNT"));
+                       char *mono_precise_count = g_getenv ("MONO_PRECISE_COUNT");
+                       if (mono_precise_count) {
+                               precise_frame_limit = atoi (mono_precise_count);
+                               g_free (mono_precise_count);
+                       }
                        precise_frame_limit_inited = TRUE;
                }
                                
@@ -1281,10 +1284,13 @@ mini_gc_init_gc_map (MonoCompile *cfg)
                static int precise_count;
 
                precise_count ++;
-               if (g_getenv ("MONO_GCMAP_COUNT")) {
-                       if (precise_count == atoi (g_getenv ("MONO_GCMAP_COUNT")))
+               char *mono_gcmap_count = g_getenv ("MONO_GCMAP_COUNT");
+               if (mono_gcmap_count) {
+                       int count = atoi (mono_gcmap_count);
+                       g_free (mono_gcmap_count);
+                       if (precise_count == count)
                                printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
-                       if (precise_count > atoi (g_getenv ("MONO_GCMAP_COUNT")))
+                       if (precise_count > count)
                                return;
                }
        }
@@ -2504,6 +2510,7 @@ parse_debug_options (void)
                exit (1);
        }
        g_strfreev (opts);
+       g_free (env);
 }
 
 void
index b3517ab58aa0abeb7af420553c95d8459bd15d28..0500f102a5729388457926e6b6554c9ab7ca574c 100644 (file)
@@ -6930,13 +6930,16 @@ emit_method_inner (EmitContext *ctx)
                static int count = 0;
                count ++;
 
-               if (g_getenv ("LLVM_COUNT")) {
-                       if (count == atoi (g_getenv ("LLVM_COUNT"))) {
+               char *llvm_count_str = g_getenv ("LLVM_COUNT");
+               if (llvm_count_str) {
+                       int lcount = atoi (llvm_count_str);
+                       g_free (llvm_count_str);
+                       if (count == lcount) {
                                printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
                                fflush (stdout);
                                last = TRUE;
                        }
-                       if (count > atoi (g_getenv ("LLVM_COUNT"))) {
+                       if (count > lcount) {
                                set_failure (ctx, "count");
                                return;
                        }
index 9b404ff7c1c3b384c616b20f3b368bab1b514441..aaa6ddda3ecf8a2310758f067c1a1eaa89911693 100644 (file)
@@ -369,18 +369,18 @@ void mono_ppc_set_func_into_sigctx (void *sigctx, void *func);
 #ifdef DEBUG_ELFABIV2
 
 #define DEBUG_ELFABIV2_printf(a, ...) \
-{if (getenv("DEBUG_ELFABIV2")) { printf(a, ##__VA_ARGS__); fflush(stdout); } }
+{char *debug_env; if (debug_env = getenv("DEBUG_ELFABIV2")) { printf(a, ##__VA_ARGS__); fflush(stdout); g_free (debug_env); } }
 
 #define DEBUG_ELFABIV2_mono_print_ins(a) \
-{if (getenv("DEBUG_ELFABIV2")) { if (!a) {printf("null\n");} else {mono_print_ins(a);} fflush(stdout); } }
+{char *debug_env; if (debug_env = getenv("DEBUG_ELFABIV2")) { if (!a) {printf("null\n");} else {mono_print_ins(a);} fflush(stdout); g_free (debug_env); } }
 
 extern char* mono_type_full_name (MonoType *type);
 
 #define DEBUG_ELFABIV2_mono_print_type(a) \
-{if (getenv("DEBUG_ELFABIV2")) { printf("%s, size: %d\n", mono_type_get_name(a), mini_type_stack_size (a, 0)); fflush(stdout); } }
+{char *debug_env; if (debug_env = getenv("DEBUG_ELFABIV2")) { printf("%s, size: %d\n", mono_type_get_name(a), mini_type_stack_size (a, 0)); fflush(stdout); g_free (debug_env); } }
 
 #define DEBUG_ELFABIV2_mono_print_class(a) \
-{if (getenv("DEBUG_ELFABIV2")) { printf("%s\n", mono_type_get_name(&a->byval_arg)); fflush(stdout); } }
+{char *debug_env; if (debug_env = getenv("DEBUG_ELFABIV2")) { printf("%s\n", mono_type_get_name(&a->byval_arg)); fflush(stdout); g_free (debug_env); } }
 
 #else
 
index 656499033cf76d28962e81fe5fc2db12515c8e63..190af5cec63edf4eb598a5d9811ec21587a59e26 100644 (file)
@@ -568,7 +568,7 @@ mono_debug_count (void)
 {
        static int count = 0;
        static gboolean inited;
-       static const char *value;
+       static char *value;
 
        count ++;
 
@@ -580,10 +580,13 @@ mono_debug_count (void)
        if (!value)
                return TRUE;
 
-       if (count == atoi (value))
+       int int_val = atoi (value);
+       g_free (value);
+
+       if (count == int_val)
                break_count ();
 
-       if (count > atoi (value))
+       if (count > int_val)
                return FALSE;
 
        return TRUE;
@@ -3208,13 +3211,14 @@ mini_parse_debug_option (const char *option)
 static void
 mini_parse_debug_options (void)
 {
-       const char *options = g_getenv ("MONO_DEBUG");
+       char *options = g_getenv ("MONO_DEBUG");
        gchar **args, **ptr;
 
        if (!options)
                return;
 
        args = g_strsplit (options, ",", -1);
+       g_free (options);
 
        for (ptr = args; ptr && *ptr; ptr++) {
                const char *arg = *ptr;
@@ -3604,8 +3608,9 @@ mini_init (const char *filename, const char *runtime_version)
 
        mono_threads_runtime_init (&ticallbacks);
 
-       if (g_getenv ("MONO_DEBUG") != NULL)
+       if (g_hasenv ("MONO_DEBUG")) {
                mini_parse_debug_options ();
+       }
 
        mono_code_manager_init ();
 
@@ -3624,15 +3629,16 @@ mini_init (const char *filename, const char *runtime_version)
 
        mono_unwind_init ();
 
-       if (mini_get_debug_options ()->lldb || g_getenv ("MONO_LLDB")) {
+       if (mini_get_debug_options ()->lldb || g_hasenv ("MONO_LLDB")) {
                mono_lldb_init ("");
                mono_dont_free_domains = TRUE;
        }
 
 #ifdef XDEBUG_ENABLED
-       if (g_getenv ("MONO_XDEBUG")) {
-               const char *xdebug_opts = g_getenv ("MONO_XDEBUG");
-               mono_xdebug_init (xdebug_opts);
+       char *mono_xdebug = g_getenv ("MONO_XDEBUG");
+       if (mono_xdebug) {
+               mono_xdebug_init (mono_xdebug);
+               g_free (mono_xdebug);
                /* So methods for multiple domains don't have the same address */
                mono_dont_free_domains = TRUE;
                mono_using_xdebug = TRUE;
index 4a03047c8b33cbb4e6f2c72477ce3a7ceb66bf23..1afbca76287e9cf3ebdd5fc52803f6a01c91292e 100644 (file)
@@ -5505,13 +5505,16 @@ mono_arch_is_inst_imm (gint64 imm)
 void
 mono_arch_finish_init (void)
 {
-       if (!g_getenv ("MONO_NO_TLS")) {
+       char *mono_no_tls = g_getenv ("MONO_NO_TLS");
+       if (!mono_no_tls) {
 #ifndef TARGET_WIN32
 #if MONO_XEN_OPT
                optimize_for_xen = access ("/proc/xen", F_OK) == 0;
 #endif
 #endif
-       }               
+       } else {
+               g_free (mono_no_tls);
+       }
 }
 
 void
index e5503e27d34eda01528da1feeb022e497edfc0d5..efe23ea8f63c35d5abf5b2bfd3a1816bc5e18b88 100644 (file)
@@ -3114,7 +3114,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl
        gboolean llvm = (flags & JIT_FLAG_LLVM) ? 1 : 0;
 #endif
        static gboolean verbose_method_inited;
-       static const char *verbose_method_name;
+       static char *verbose_method_name;
 
        InterlockedIncrement (&mono_jit_stats.methods_compiled);
        if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
@@ -3406,6 +3406,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl
                        if (strcmp (cfg->method->name, name) == 0)
                                cfg->verbose_level = 4;
                }
+               g_free (verbose_method_name);
        }
 
        cfg->intvars = (guint16 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
index b4b2751e34a872615453c3bb2ffa1629d256b534..3a7500e0b1247d4ad4a40b7a421a1c19832716d3 100644 (file)
@@ -4932,7 +4932,7 @@ mono_profiler_startup (const char *desc)
                if ((opt = match_option (p, "coverage", NULL)) != p) {
                        do_coverage = 1;
                        events |= MONO_PROFILE_ENTER_LEAVE;
-                       debug_coverage = (g_getenv ("MONO_PROFILER_DEBUG_COVERAGE") != NULL);
+                       debug_coverage = g_hasenv ("MONO_PROFILER_DEBUG_COVERAGE");
                        continue;
                }
                if ((opt = match_option (p, "onlycoverage", NULL)) != p) {
index fb6082ab6ab097da6911bab9a45fa1e0280ecead..ab46a371126ca76de0901d9cb657988d91409d4f 100644 (file)
@@ -2924,7 +2924,7 @@ parse_double_in_interval (const char *env_var, const char *opt_name, const char
 void
 sgen_gc_init (void)
 {
-       const char *env;
+       char *env;
        char **opts, **ptr;
        char *major_collector_opt = NULL;
        char *minor_collector_opt = NULL;
@@ -2969,6 +2969,7 @@ sgen_gc_init (void)
 
        if ((env = g_getenv (MONO_GC_PARAMS_NAME)) || gc_params_options) {
                params_opts = g_strdup_printf ("%s,%s", gc_params_options ? gc_params_options : "", env ? env : "");
+               g_free (env);
        }
 
        if (params_opts) {
@@ -3186,6 +3187,7 @@ sgen_gc_init (void)
 
        if ((env = g_getenv (MONO_GC_DEBUG_NAME)) || gc_debug_options) {
                debug_opts = g_strdup_printf ("%s,%s", gc_debug_options ? gc_debug_options  : "", env ? env : "");
+               g_free (env);
        }
 
        if (debug_opts) {
index 71a0d8d62577fff3eba913fe17b1a65e7514d338..a1d0d0ebcd3081341a7821c44b447642b8889b00 100644 (file)
@@ -59,6 +59,7 @@ mono_check_mode_enabled (MonoCheckMode query)
 #endif
                        }
                        g_strfreev (env_split);
+                       g_free (env_string);
                }
 
                check_mode = env_check_mode;
@@ -72,10 +73,12 @@ mono_check_transition_limit (void)
        static int transition_limit = -1;
        if (transition_limit < 0) {
                const gchar *env_string = g_getenv ("MONO_CHECK_THREAD_TRANSITION_HISTORY");
-               if (env_string)
+               if (env_string) {
                        transition_limit = atoi (env_string);
-               else
+                       g_free (env_string);
+               } else {
                        transition_limit = 3;
+               }
        }
        return transition_limit;
 }
index 9d5722e33a1be79c5e67a36369b814bf4ece1811..c0e68ee034fffc8368b0ce06660a2f9ae4eadcd6 100644 (file)
@@ -34,8 +34,8 @@ static gboolean hwcap_inited = FALSE;
 void
 mono_hwcap_init (void)
 {
-       const char *verbose = g_getenv ("MONO_VERBOSE_HWCAP");
-       const char *conservative = g_getenv ("MONO_CONSERVATIVE_HWCAP");
+       char *verbose = g_getenv ("MONO_VERBOSE_HWCAP");
+       char *conservative = g_getenv ("MONO_CONSERVATIVE_HWCAP");
 
        if (hwcap_inited)
                return;
@@ -45,6 +45,9 @@ mono_hwcap_init (void)
 
        if (verbose && !strncmp (verbose, "1", 1))
                mono_hwcap_print ();
+
+       g_free (verbose);
+       g_free (conservative);
 }
 
 void
index abbe812f5928d6ea6a83795111dbd928f0da9bc2..b164b6b56db21e607b97501f5855bc9b79660bdf 100644 (file)
@@ -23,7 +23,7 @@ static inline gchar *mono_portability_find_file_internal (GString **report, cons
 
 void mono_portability_helpers_init (void)
 {
-        const gchar *env;
+        gchar *env;
 
        if (mono_io_portability_helpers != PORTABILITY_UNKNOWN)
                return;
@@ -56,6 +56,7 @@ void mono_portability_helpers_init (void)
                                 mono_io_portability_helpers |= (PORTABILITY_DRIVE | PORTABILITY_CASE);
                        }
                 }
+               g_free (env);
        }
 }
 
index 4b32dfb0334b17fe02402fde748554536f770b1b..aeaa3f97ceefab0bc79fe685d042ade9a91d5368 100644 (file)
@@ -47,10 +47,20 @@ mono_trace_init (void)
                mono_internal_current_level = G_LOG_LEVEL_ERROR;
                level_stack = g_queue_new();
 
-               mono_trace_set_mask_string(g_getenv("MONO_LOG_MASK"));
-               mono_trace_set_level_string(g_getenv("MONO_LOG_LEVEL"));
-               mono_trace_set_logheader_string(g_getenv("MONO_LOG_HEADER"));
-               mono_trace_set_logdest_string(g_getenv("MONO_LOG_DEST"));
+               char *mask = g_getenv ("MONO_LOG_MASK");
+               char *level = g_getenv ("MONO_LOG_LEVEL");
+               char *header = g_getenv ("MONO_LOG_HEADER");
+               char *dest = g_getenv ("MONO_LOG_DEST");
+
+               mono_trace_set_mask_string(mask);
+               mono_trace_set_level_string(level);
+               mono_trace_set_logheader_string(header);
+               mono_trace_set_logdest_string(dest);
+
+               g_free (mask);
+               g_free (level);
+               g_free (header);
+               g_free (dest);
        }
 }
 
index ccfcb0b8ac029d3e64c318cc5d98f447d9aea6cf..62266d34d25eac954dd6aa1c5ccf6cf551c7b0c2 100644 (file)
@@ -407,7 +407,7 @@ static gboolean
 shared_area_disabled (void)
 {
        if (!use_shared_area) {
-               if (g_getenv ("MONO_DISABLE_SHARED_AREA"))
+               if (g_hasenv ("MONO_DISABLE_SHARED_AREA"))
                        use_shared_area = -1;
                else
                        use_shared_area = 1;
index 6b2bb98eef90e81d7b6e9a95a063d4f31e076482..13b8a32393716ec9473ce9d9e4bbd9ef6e849087 100644 (file)
@@ -136,7 +136,7 @@ mono_rand_open (void)
                file = open (NAME_DEV_RANDOM, O_RDONLY);
 #endif
        if (file < 0)
-               use_egd = g_getenv("MONO_EGD_SOCKET") != NULL;
+               use_egd = g_hasenv ("MONO_EGD_SOCKET");
 
        status = 2;
 
@@ -158,13 +158,14 @@ mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gint buffer_size, Mon
        error_init (error);
 
        if (use_egd) {
-               const char *socket_path = g_getenv ("MONO_EGD_SOCKET");
+               char *socket_path = g_getenv ("MONO_EGD_SOCKET");
                /* exception will be thrown in managed code */
                if (socket_path == NULL) {
                        *handle = NULL;
                        return FALSE;
                }
                get_entropy_from_egd (socket_path, buffer, buffer_size, error);
+               g_free (socket_path);
        } else {
                /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
                gint count = 0;
index c7ad2cedd416318e210e14ebcaebb3134b3ba431..0d5f1e9b73577e3ce129005e5dedf4de73dc3972 100644 (file)
@@ -430,7 +430,7 @@ mono_threads_is_coop_enabled (void)
 #else
        static int is_coop_enabled = -1;
        if (G_UNLIKELY (is_coop_enabled == -1))
-               is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? 1 : 0;
+               is_coop_enabled = g_hasenv ("MONO_ENABLE_COOP") ? 1 : 0;
        return is_coop_enabled == 1;
 #endif
 }
index ddc0a02ac183c904def40e251be8f4697fd3f31e..e6d151ce2566c841cfdba4ae8d1f083d30bfcfa6 100644 (file)
@@ -681,7 +681,7 @@ mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
        gboolean res;
        threads_callbacks = *callbacks;
        thread_info_size = info_size;
-       const char *sleepLimit;
+       char *sleepLimit;
 #ifdef HOST_WIN32
        res = mono_native_tls_alloc (&thread_info_key, NULL);
        res = mono_native_tls_alloc (&thread_exited_key, NULL);
@@ -705,6 +705,7 @@ mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
                        sleepWarnDuration = threshold / 20;
                } else
                        g_warning("MONO_SLEEP_ABORT_LIMIT must be a number >= 40");
+               g_free (sleepLimit);
        }
 
        mono_os_sem_init (&global_suspend_semaphore, 1);
index 9dd245e29cc21af0c7a8088303dcb6ba0034f66b..2ece0733d122a42d8ac969a7027081faf6af501a 100644 (file)
@@ -45,7 +45,7 @@ mono_unicode_from_external (const gchar *in, gsize *bytes)
 {
        gchar *res=NULL;
        gchar **encodings;
-       const gchar *encoding_list;
+       gchar *encoding_list;
        int i;
        glong lbytes;
        
@@ -55,10 +55,11 @@ mono_unicode_from_external (const gchar *in, gsize *bytes)
        
        encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS");
        if(encoding_list==NULL) {
-               encoding_list = "";
+               encoding_list = g_strdup("");
        }
        
        encodings=g_strsplit (encoding_list, ":", 0);
+       g_free (encoding_list);
        for(i=0;encodings[i]!=NULL; i++) {
                /* "default_locale" is a special case encoding */
                if(!strcmp (encodings[i], "default_locale")) {
@@ -118,7 +119,7 @@ gchar *mono_utf8_from_external (const gchar *in)
 {
        gchar *res=NULL;
        gchar **encodings;
-       const gchar *encoding_list;
+       gchar *encoding_list;
        int i;
        
        if(in==NULL) {
@@ -127,10 +128,11 @@ gchar *mono_utf8_from_external (const gchar *in)
        
        encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS");
        if(encoding_list==NULL) {
-               encoding_list = "";
+               encoding_list = g_strdup("");
        }
        
        encodings=g_strsplit (encoding_list, ":", 0);
+       g_free (encoding_list);
        for(i=0;encodings[i]!=NULL; i++) {
                
                /* "default_locale" is a special case encoding */
@@ -171,7 +173,7 @@ gchar *mono_utf8_from_external (const gchar *in)
 gchar *mono_unicode_to_external (const gunichar2 *uni)
 {
        gchar *utf8;
-       const gchar *encoding_list;
+       gchar *encoding_list;
        
        /* Turn the unicode into utf8 to start with, because its
         * easier to work with gchar * than gunichar2 *
@@ -188,6 +190,7 @@ gchar *mono_unicode_to_external (const gunichar2 *uni)
                int i;
                
                encodings=g_strsplit (encoding_list, ":", 0);
+               g_free (encoding_list);
                for(i=0; encodings[i]!=NULL; i++) {
                        if(!strcmp (encodings[i], "default_locale")) {
                                res=g_locale_from_utf8 (utf8, -1, NULL, NULL,