Rewrote getenv and _wgetenv to avoid returning a pointer to a freed

memory block.
This commit is contained in:
Alexandre Julliard 2003-09-24 18:57:28 +00:00
parent 7add08a303
commit b48d812433
1 changed files with 26 additions and 41 deletions

View File

@ -35,27 +35,20 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
*/
char *MSVCRT_getenv(const char *name)
{
char *environ = GetEnvironmentStringsA();
char *pp,*pos = NULL;
char **environ;
unsigned int length=strlen(name);
for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
for (environ = *__p__environ(); *environ; environ++)
{
pos =strchr(pp,'=');
if ((pos) && ((pos - pp) == length))
char *str = *environ;
char *pos = strchr(str,'=');
if (pos && ((pos - str) == length) && !strncasecmp(str,name,length))
{
if (!strncasecmp(pp,name,length)) break;
TRACE("(%s): got %s\n", debugstr_a(name), debugstr_a(pos + 1));
return pos + 1;
}
}
if ((*pp)&& (pos))
{
pp = pos+1;
TRACE("got %s\n",pp);
}
else
pp = 0;
FreeEnvironmentStringsA( environ );
return pp;
return NULL;
}
/*********************************************************************
@ -63,27 +56,19 @@ char *MSVCRT_getenv(const char *name)
*/
MSVCRT_wchar_t *_wgetenv(const MSVCRT_wchar_t *name)
{
MSVCRT_wchar_t* environ = GetEnvironmentStringsW();
MSVCRT_wchar_t* pp,*pos = NULL;
MSVCRT_wchar_t **environ;
unsigned int length=strlenW(name);
for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
for (environ = *__p__wenviron(); *environ; environ++)
{
pos = strchrW(pp,'=');
if ((pos) && ((pos - pp) == length))
MSVCRT_wchar_t *str = *environ;
MSVCRT_wchar_t *pos = strchrW(str,'=');
if (pos && ((pos - str) == length) && !strncmpiW(str,name,length))
{
if (!strncmpiW(pp,name,length))
{
pp = pos+1;
TRACE("got %s\n",debugstr_w(pp));
/* can't free pointer since we are returning it */
/* should probably use MSVCRT_wenviron instead */
FIXME( "memory leak\n" );
return pp;
TRACE("(%s): got %s\n", debugstr_w(name), debugstr_w(pos + 1));
return pos + 1;
}
}
}
FreeEnvironmentStringsW( environ );
return NULL;
}