Fixed bug 3284 - minor correction for SDL_setenv on _WIN32__ platform
authorSam Lantinga <slouken@libsdl.org>
Fri, 11 Aug 2017 21:30:06 -0700
changeset 11232b26929d6ca20
parent 11231 5235df1ce071
child 11233 554b2c683da2
Fixed bug 3284 - minor correction for SDL_setenv on _WIN32__ platform

Coriiander

Here is a minor correction for a non-breaking mistake in SDL_setenv for __WIN32__ platform. See below for details.

FILE:
"SDL/src/stdlib/SDL_getenv.c"

FUNCTION: (__WIN32__ platform)
int SDL_setenv(const char *name, const char *value, int overwrite)

CODE:
if (!overwrite) {
char ch = 0;
const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
if (len > 0) {
return 0; /* asked not to overwrite existing value. */
}
}


WHAT'S WRONG:
The 3th argument to GetEnvironmentVariable (being DWORD nSize) must be the number of characters, not the number of bytes. SDL currently passes "the size of 1 char", rather "1". While it is non-breaking (1=1 after all), it is incorrect. Furthermore there is no need to specify the 2nd and 3th arguments at all.

CORRECTION 1: (corrected argument_
if (!overwrite) {
char ch = 0;
const size_t len = GetEnvironmentVariableA(name, &ch, 1);
if (len > 0) {
return 0; /* asked not to overwrite existing value. */
}
}

CORRECTION 2: (stripped of unneeded code)
if (!overwrite) {
if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
return 0; /* asked not to overwrite existing value. */
}
}
src/stdlib/SDL_getenv.c
     1.1 --- a/src/stdlib/SDL_getenv.c	Fri Aug 11 21:17:10 2017 -0700
     1.2 +++ b/src/stdlib/SDL_getenv.c	Fri Aug 11 21:30:06 2017 -0700
     1.3 @@ -60,9 +60,7 @@
     1.4      }
     1.5      
     1.6      if (!overwrite) {
     1.7 -        char ch = 0;
     1.8 -        const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
     1.9 -        if (len > 0) {
    1.10 +        if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
    1.11              return 0;  /* asked not to overwrite existing value. */
    1.12          }
    1.13      }