Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Allocate memory only if we're going to overwrite an environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Dec 16, 2009
1 parent 34f721b commit 2ffecc5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/stdlib/SDL_getenv.c
Expand Up @@ -93,6 +93,11 @@ SDL_setenv(const char *name, const char *value, int overwrite)
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);
Expand All @@ -117,10 +122,6 @@ SDL_setenv(const char *name, const char *value, int overwrite)
}
/* 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;
Expand Down

0 comments on commit 2ffecc5

Please sign in to comment.