diff --git a/reactos/lib/msvcrt/stdlib/getenv.c b/reactos/lib/msvcrt/stdlib/getenv.c index 3fd11b4e57b..359ac0d3f8a 100644 --- a/reactos/lib/msvcrt/stdlib/getenv.c +++ b/reactos/lib/msvcrt/stdlib/getenv.c @@ -1,23 +1,35 @@ #include #include +#define NDEBUG +#include + char *getenv(const char *name) { - char *buffer; - buffer = (char *)malloc(MAX_PATH); - buffer[0] = 0; - if ( GetEnvironmentVariableA(name,buffer,MAX_PATH) == 0 ) + char *buffer = (char*)0xffffffff; + int len = GetEnvironmentVariableA(name,buffer,0) + 1; + DPRINT("getenv(%s)\n", name); + buffer = (char *)malloc(len); + DPRINT("getenv('%s') %d %x\n", name, len, buffer); + if (buffer == NULL || GetEnvironmentVariableA(name,buffer,len) == 0 ) + { + free(buffer); return NULL; + } return buffer; } wchar_t *_wgetenv(const wchar_t *name) { - wchar_t *buffer; - buffer = (wchar_t *)malloc(MAX_PATH * sizeof(wchar_t)); - buffer[0] = 0; - if ( GetEnvironmentVariableW(name,buffer,MAX_PATH) == 0 ) + wchar_t *buffer = (wchar_t*)0xffffffff; + int len = GetEnvironmentVariableW(name, buffer,0) + 1; + DPRINT("_wgetenv(%S)\n", name); + buffer = (wchar_t *)malloc(len * sizeof(wchar_t)); + if (buffer == NULL || GetEnvironmentVariableW(name,buffer,len) == 0) + { + free(buffer); return NULL; + } return buffer; } diff --git a/reactos/lib/msvcrt/stdlib/putenv.c b/reactos/lib/msvcrt/stdlib/putenv.c index de3cab4444a..ce68245ba87 100644 --- a/reactos/lib/msvcrt/stdlib/putenv.c +++ b/reactos/lib/msvcrt/stdlib/putenv.c @@ -2,41 +2,50 @@ #include #include +#define NDEBUG +#include + extern int BlockEnvToEnviron(); // defined in misc/dllmain.c int _putenv(const char *val) { - char buffer[1024]; + char *buffer; char *epos; int res; - strcpy(buffer,val); - epos = strchr(buffer, '='); + DPRINT("_putenv('%s')\n", val); + epos = strchr(val, '='); if ( epos == NULL ) return -1; - - *epos = 0; - + buffer = (char*)malloc(epos - val + 1); + if (buffer == NULL) + return -1; + strncpy(buffer, val, epos - val); + buffer[epos - val] = 0; res = SetEnvironmentVariableA(buffer,epos+1); - if (BlockEnvToEnviron() ) return 0; + free(buffer); + if (BlockEnvToEnviron()) return 0; return res; } int _wputenv(const wchar_t *val) { - wchar_t buffer[1024]; + wchar_t *buffer; wchar_t *epos; int res; - wcscpy(buffer,val); - epos = wcschr(buffer, L'='); + DPRINT("_wputenv('%S')\n", val); + epos = wcsrchr(val, L'='); if ( epos == NULL ) return -1; - - *epos = 0; - + buffer = (char*)malloc((epos - val + 1) * sizeof (wchar_t)); + if (buffer == NULL) + return -1; + wcsncpy(buffer, val, epos - val); + buffer[epos - val] = 0; res = SetEnvironmentVariableW(buffer,epos+1); + free(buffer); if (BlockEnvToEnviron() ) return 0; return res; }