From 6c1f0a9871fd02dfa7965deb412492a6f7aa710c Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 14 Apr 2010 13:55:57 -0700 Subject: [PATCH] (SIGAR-192) avoid possible stack corruption in Windows proc_env impl --- src/os/win32/win32_sigar.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index 5f2878bd..af6901e2 100755 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -1491,7 +1491,8 @@ int sigar_os_proc_args_get(sigar_t *sigar, sigar_pid_t pid, } } -static int sigar_proc_env_parse(UCHAR *ptr, sigar_proc_env_t *procenv) +static int sigar_proc_env_parse(UCHAR *ptr, sigar_proc_env_t *procenv, + int multi) { while (*ptr) { char *val; @@ -1524,6 +1525,10 @@ static int sigar_proc_env_parse(UCHAR *ptr, sigar_proc_env_t *procenv) return status; } + if (!multi) { + break; /* caller only provided 1 key=val pair */ + } + ptr += klen + 1 + vlen + 1; } @@ -1535,7 +1540,7 @@ static int sigar_local_proc_env_get(sigar_t *sigar, sigar_pid_t pid, { UCHAR *env = (UCHAR*)GetEnvironmentStrings(); - sigar_proc_env_parse(env, procenv); + sigar_proc_env_parse(env, procenv, TRUE); FreeEnvironmentStrings(env); @@ -1564,8 +1569,9 @@ static int sigar_remote_proc_env_get(sigar_t *sigar, sigar_pid_t pid, while ((size > 0) && (*ptr != L'\0')) { DWORD len = (wcslen((LPWSTR)ptr) + 1) * sizeof(WCHAR); + /* multi=FALSE so no need to: memset(ent, '\0', sizeof(ent)) */ SIGAR_W2A((WCHAR *)ptr, ent, sizeof(ent)); - if (sigar_proc_env_parse(ent, procenv) != SIGAR_OK) { + if (sigar_proc_env_parse(ent, procenv, FALSE) != SIGAR_OK) { break; } size -= len;