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 358365f66674b7fb
parent 3582 ecfc42323d0c
child 3584 41d01d70659c
Allocate memory only if we're going to overwrite an environment variable
src/stdlib/SDL_getenv.c
     1.1 --- a/src/stdlib/SDL_getenv.c	Wed Dec 16 11:03:38 2009 +0000
     1.2 +++ b/src/stdlib/SDL_getenv.c	Wed Dec 16 16:08:17 2009 +0000
     1.3 @@ -93,6 +93,11 @@
     1.4          return (-1);
     1.5      }
     1.6  
     1.7 +    /* See if it already exists */
     1.8 +    if (!overwrite && SDL_getenv(name)) {
     1.9 +        return 0;
    1.10 +    }
    1.11 +
    1.12      /* Allocate memory for the variable */
    1.13      len = SDL_strlen(name) + SDL_strlen(value) + 2;
    1.14      new_variable = (char *) SDL_malloc(len);
    1.15 @@ -117,10 +122,6 @@
    1.16          }
    1.17          /* If we found it, just replace the entry */
    1.18          if (SDL_env[i]) {
    1.19 -            if (!overwrite) {
    1.20 -                SDL_free(new_variable);
    1.21 -                return 0;
    1.22 -            }
    1.23              SDL_free(SDL_env[i]);
    1.24              SDL_env[i] = new_variable;
    1.25              added = 1;