...
Code Block | ||
---|---|---|
| ||
char *temp; char *copy; if ((temp = getenv("TEST_ENV")) != NULL) { copy = (char *)malloc(strlen(temp) + 1); if (copy != NULL) { strcpy(copy, temp); } else { /* handle error condition */ } copy[0] = 'a'; setenv("TEST_ENV", copy, 1); } else { return -1; } |
Compliant Solution (glibc)
Depending on the implementation, a program may not consistently choose the same value if there are multiple environment variables with the same name. The GNU glibc library addresses this issue in getenv()
and setenv()
by always using the first variable it encounters and ignoring the rest. Other implementations are following suit.
...