Allocate memory only if we're going to overwrite an environment variable
authorSam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 16:08:17 +0000
changeset 3583 65f66674b7fb
parent 3582 ecfc42323d0c
child 3584 41d01d70659c
Allocate memory only if we're going to overwrite an environment variable
src/stdlib/SDL_getenv.c
--- a/src/stdlib/SDL_getenv.c	Wed Dec 16 11:03:38 2009 +0000
+++ b/src/stdlib/SDL_getenv.c	Wed Dec 16 16:08:17 2009 +0000
@@ -93,6 +93,11 @@
         return (-1);
     }
 
+    /* See if it already exists */
+    if (!overwrite && SDL_getenv(name)) {
+        return 0;
+    }
+
     /* Allocate memory for the variable */
     len = SDL_strlen(name) + SDL_strlen(value) + 2;
     new_variable = (char *) SDL_malloc(len);
@@ -117,10 +122,6 @@
         }
         /* If we found it, just replace the entry */
         if (SDL_env[i]) {
-            if (!overwrite) {
-                SDL_free(new_variable);
-                return 0;
-            }
             SDL_free(SDL_env[i]);
             SDL_env[i] = new_variable;
             added = 1;