Skip to content

Commit 895afe5

Browse files
committed
stdinc: On Windows, uppercase the system environment table's var names.
Fixes #15086.
1 parent 82141a2 commit 895afe5

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

include/SDL3/SDL_stdinc.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,12 @@ typedef struct SDL_Environment SDL_Environment;
17021702
* SDL_setenv_unsafe() or SDL_unsetenv_unsafe() if you want changes to persist
17031703
* in the C runtime environment after SDL_Quit().
17041704
*
1705+
* Note that on Windows, the variable names pulled in from the system at
1706+
* startup have their ASCII values uppercased, to match what most platforms
1707+
* expect even though the Windows system environment table is
1708+
* case-insensitive. Once those uppercased variable names are in an
1709+
* SDL_Environment, SDL treats them as case-sensitive.
1710+
*
17051711
* \returns a pointer to the environment for the process or NULL on failure;
17061712
* call SDL_GetError() for more information.
17071713
*
@@ -1717,7 +1723,13 @@ typedef struct SDL_Environment SDL_Environment;
17171723
extern SDL_DECLSPEC SDL_Environment * SDLCALL SDL_GetEnvironment(void);
17181724

17191725
/**
1720-
* Create a set of environment variables
1726+
* Create a set of environment variables.
1727+
*
1728+
* Note that on Windows, the variable names pulled in from the system, if
1729+
* `populated` is true, have their ASCII values uppercased, to match what most
1730+
* platforms expect even though the Windows system environment table is
1731+
* case-insensitive. Once those uppercased variable names are in an
1732+
* SDL_Environment, SDL treats them as case-sensitive.
17211733
*
17221734
* \param populated true to initialize it from the C runtime environment,
17231735
* false to create an empty environment.

src/stdlib/SDL_getenv.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ SDL_Environment *SDL_CreateEnvironment(bool populated)
119119
}
120120
*value++ = '\0';
121121

122+
// uppercase ASCII chars in environment variable names on Windows, since the system environment table is case-insensitive.
123+
for (char *ptr = variable; *ptr; ptr++) {
124+
*ptr = SDL_toupper(*ptr); // the only UTF-8 bytes that don't have the high-bit set are single-byte ASCII chars, so SDL_toupper will leave multichar stuff alone.
125+
}
126+
122127
SDL_InsertIntoHashTable(env->strings, variable, value, true);
123128
}
124129
FreeEnvironmentStringsW(strings);
@@ -143,6 +148,13 @@ SDL_Environment *SDL_CreateEnvironment(bool populated)
143148
}
144149
*value++ = '\0';
145150

151+
#ifdef SDL_PLATFORM_CYGWIN
152+
// uppercase ASCII chars in environment variable names on Windows, since the system environment table is case-insensitive.
153+
for (char *ptr = variable; *ptr; ptr++) {
154+
*ptr = SDL_toupper(*ptr); // the only UTF-8 bytes that don't have the high-bit set are single-byte ASCII chars, so SDL_toupper will leave multichar stuff alone.
155+
}
156+
#endif
157+
146158
SDL_InsertIntoHashTable(env->strings, variable, value, true);
147159
}
148160
}

0 commit comments

Comments
 (0)