From 4ce2368c4f2ad2a4f1d0a6beb1e73cbcb3376d16 Mon Sep 17 00:00:00 2001 From: xarkes Date: Thu, 22 Jun 2017 10:56:37 +0200 Subject: [PATCH] Windows environment fix (buffer too small + handle return value) --- libr/util/sys.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libr/util/sys.c b/libr/util/sys.c index 5541b9cddf..8235031129 100644 --- a/libr/util/sys.c +++ b/libr/util/sys.c @@ -352,14 +352,23 @@ R_API int r_sys_crash_handler(const char *cmd) { R_API char *r_sys_getenv(const char *key) { #if __WINDOWS__ && !__CYGWIN__ - static char envbuf[1024]; + static char envbuf[4096]; + DWORD dwRet; + if (!key) { return NULL; } - envbuf[0] = 0; - GetEnvironmentVariableA (key, (LPSTR)&envbuf, sizeof (envbuf)); - // TODO: handle return value of GEV - return *envbuf? strdup (envbuf): NULL; + dwRet = GetEnvironmentVariableA (key, (LPSTR)&envbuf, sizeof (envbuf)); + if (dwRet == 0) { + /* Variable not found. */ + return NULL; + } + if (dwRet == sizeof(envbuf)) { + /* The contents of envbuf are undefined, so return NULL */ + eprintf ("Buffer too small to read `%s' environment variable.\n", key); + return NULL; + } + return strdup (envbuf); #else char *b; if (!key) {