Fixed GetEnvironmentVariable.
authorArmin Novak <armin.novak@thincast.com>
Fri, 3 Mar 2017 11:37:27 +0000 (12:37 +0100)
committerArmin Novak <armin.novak@thincast.com>
Fri, 3 Mar 2017 11:43:00 +0000 (12:43 +0100)
13 files changed:
libfreerdp/core/gateway/rts.c
libfreerdp/core/proxy.c
libfreerdp/locale/locale.c
winpr/libwinpr/environment/environment.c
winpr/libwinpr/path/shell.c
winpr/libwinpr/sspi/sspi.c
winpr/libwinpr/thread/process.c
winpr/libwinpr/utils/wlog/FileAppender.c
winpr/libwinpr/utils/wlog/JournaldAppender.c
winpr/libwinpr/utils/wlog/Layout.c
winpr/libwinpr/utils/wlog/UdpAppender.c
winpr/libwinpr/utils/wlog/wlog.c
winpr/libwinpr/wtsapi/wtsapi.c

index e38687c..05de370 100644 (file)
@@ -432,8 +432,8 @@ int rts_send_CONN_A1_pdu(rdpRpc* rpc)
        header.Flags = RTS_FLAG_NONE;
        header.NumberOfCommands = 4;
        WLog_DBG(TAG, "Sending CONN/A1 RTS PDU");
-       VirtualConnectionCookie = (BYTE*) & (connection->Cookie);
-       OUTChannelCookie = (BYTE*) & (outChannel->Cookie);
+       VirtualConnectionCookie = (BYTE*) &(connection->Cookie);
+       OUTChannelCookie = (BYTE*) &(outChannel->Cookie);
        ReceiveWindowSize = outChannel->ReceiveWindow;
        buffer = (BYTE*) malloc(header.frag_length);
 
@@ -479,9 +479,9 @@ int rts_send_CONN_B1_pdu(rdpRpc* rpc)
        header.Flags = RTS_FLAG_NONE;
        header.NumberOfCommands = 6;
        WLog_DBG(TAG, "Sending CONN/B1 RTS PDU");
-       VirtualConnectionCookie = (BYTE*) & (connection->Cookie);
-       INChannelCookie = (BYTE*) & (inChannel->Cookie);
-       AssociationGroupId = (BYTE*) & (connection->AssociationGroupId);
+       VirtualConnectionCookie = (BYTE*) &(connection->Cookie);
+       INChannelCookie = (BYTE*) &(inChannel->Cookie);
+       AssociationGroupId = (BYTE*) &(connection->AssociationGroupId);
        buffer = (BYTE*) malloc(header.frag_length);
 
        if (!buffer)
@@ -572,7 +572,7 @@ int rts_send_flow_control_ack_pdu(rdpRpc* rpc)
        WLog_DBG(TAG, "Sending FlowControlAck RTS PDU");
        BytesReceived = outChannel->BytesReceived;
        AvailableWindow = outChannel->AvailableWindowAdvertised;
-       ChannelCookie = (BYTE*) & (outChannel->Cookie);
+       ChannelCookie = (BYTE*) &(outChannel->Cookie);
        outChannel->ReceiverAvailableWindow = outChannel->AvailableWindowAdvertised;
        buffer = (BYTE*) malloc(header.frag_length);
 
@@ -752,7 +752,7 @@ static int rts_send_OUT_R2_A7_pdu(rdpRpc* rpc)
        header.Flags = RTS_FLAG_OUT_CHANNEL;
        header.NumberOfCommands = 3;
        WLog_DBG(TAG, "Sending OUT_R2/A7 RTS PDU");
-       SuccessorChannelCookie = (BYTE*) & (nextOutChannel->Cookie);
+       SuccessorChannelCookie = (BYTE*) &(nextOutChannel->Cookie);
        buffer = (BYTE*) malloc(header.frag_length);
 
        if (!buffer)
@@ -808,9 +808,9 @@ int rts_send_OUT_R1_A3_pdu(rdpRpc* rpc)
        header.Flags = RTS_FLAG_RECYCLE_CHANNEL;
        header.NumberOfCommands = 5;
        WLog_DBG(TAG, "Sending OUT_R1/A3 RTS PDU");
-       VirtualConnectionCookie = (BYTE*) & (connection->Cookie);
-       PredecessorChannelCookie = (BYTE*) & (outChannel->Cookie);
-       SuccessorChannelCookie = (BYTE*) & (nextOutChannel->Cookie);
+       VirtualConnectionCookie = (BYTE*) &(connection->Cookie);
+       PredecessorChannelCookie = (BYTE*) &(outChannel->Cookie);
+       SuccessorChannelCookie = (BYTE*) &(nextOutChannel->Cookie);
        ReceiveWindowSize = outChannel->ReceiveWindow;
        buffer = (BYTE*) malloc(header.frag_length);
 
index 22462a7..3ef2a62 100644 (file)
@@ -60,7 +60,7 @@ void proxy_read_environment(rdpSettings* settings, char* envname)
        if (!envlen)
                return;
 
-       env = calloc(1, envlen + 1);
+       env = calloc(1, envlen);
 
        if (!env)
        {
@@ -68,7 +68,7 @@ void proxy_read_environment(rdpSettings* settings, char* envname)
                return;
        }
 
-       if (GetEnvironmentVariableA(envname, env, envlen) == envlen)
+       if (GetEnvironmentVariableA(envname, env, envlen) == envlen - 1)
                proxy_parse_uri(settings, env);
 
        free(env);
index 1833a4f..c461397 100644 (file)
@@ -637,9 +637,10 @@ BOOL freerdp_get_system_language_and_country_codes(char* language, char* country
        DWORD nSize;
        int underscore;
        char* env_lang = NULL;
+       LPCSTR lang = "LANG";
 
        /* LANG = <language>_<country>.<encoding> */
-       nSize = GetEnvironmentVariableA("LANG", NULL, 0);
+       nSize = GetEnvironmentVariableA(lang, NULL, 0);
 
        if (!nSize)
                return FALSE; /* LANG environment variable was not set */
@@ -649,7 +650,11 @@ BOOL freerdp_get_system_language_and_country_codes(char* language, char* country
        if (!env_lang)
                return FALSE;
 
-       GetEnvironmentVariableA("LANG", env_lang, nSize); /* Get locale from environment variable LANG */
+       if (GetEnvironmentVariableA(lang, env_lang, nSize) != nSize - 1) /* Get locale from environment variable LANG */
+       {
+               free (env_lang);
+               return FALSE;
+       }
 
        underscore = strcspn(env_lang, "_");
 
index 6227619..84d354c 100644 (file)
@@ -155,7 +155,8 @@ DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
        if ((length + 1 > nSize) || (!lpBuffer))
                return length + 1;
 
-       CopyMemory(lpBuffer, env, length + 1);
+       CopyMemory(lpBuffer, env, length);
+       lpBuffer[length] = '\0';
 
        return length;
 #else
index 4f27b43..386cac3 100644 (file)
@@ -66,13 +66,16 @@ static char* GetEnvAlloc(LPCSTR lpName)
 
        if (length > 0)
        {
-               env = malloc(length + 1);
+               env = malloc(length);
 
                if (!env)
                        return NULL;
 
-               GetEnvironmentVariableA(lpName, env, length + 1);
-               env[length] = '\0';
+               if (GetEnvironmentVariableA(lpName, env, length) != length - 1)
+               {
+                       free(env);
+                       return NULL;
+               }
        }
 
        return env;
@@ -366,7 +369,7 @@ char* GetEnvironmentPath(char* name)
                if (!env)
                        return NULL;
 
-               if (GetEnvironmentVariableA(name, env, nSize) != nSize)
+               if (GetEnvironmentVariableA(name, env, nSize) != nSize - 1)
                {
                        free(env);
                        return NULL;
index 8feb847..ac5e922 100644 (file)
@@ -53,9 +53,10 @@ BOOL ShouldUseNativeSspi(void)
 {
        BOOL status = FALSE;
 #ifdef _WIN32
+       LPCSTR sspi = "WINPR_NATIVE_SSPI";
        DWORD nSize;
        char* env = NULL;
-       nSize = GetEnvironmentVariableA("WINPR_NATIVE_SSPI", NULL, 0);
+       nSize = GetEnvironmentVariableA(sspi, NULL, 0);
 
        if (!nSize)
                return TRUE;
@@ -65,7 +66,11 @@ BOOL ShouldUseNativeSspi(void)
        if (!env)
                return TRUE;
 
-       nSize = GetEnvironmentVariableA("WINPR_NATIVE_SSPI", env, nSize);
+       if (GetEnvironmentVariableA(sspi, env, nSize) != nSize - 1)
+       {
+               free(env);
+               return TRUE;
+       }
 
        if (strcmp(env, "0") == 0)
                status = FALSE;
index 2c179c7..f6a8b28 100644 (file)
@@ -89,6 +89,7 @@
 
 static char* FindApplicationPath(char* application)
 {
+       LPCSTR pathName = "PATH";
        char* path;
        char* save;
        DWORD nSize;
@@ -101,7 +102,7 @@ static char* FindApplicationPath(char* application)
        if (application[0] == '/')
                return _strdup(application);
 
-       nSize = GetEnvironmentVariableA("PATH", NULL, 0);
+       nSize = GetEnvironmentVariableA(pathName, NULL, 0);
 
        if (!nSize)
                return _strdup(application);
@@ -110,7 +111,11 @@ static char* FindApplicationPath(char* application)
        if (!lpSystemPath)
                return NULL;
 
-       nSize = GetEnvironmentVariableA("PATH", lpSystemPath, nSize);
+       if (GetEnvironmentVariableA(pathName, lpSystemPath, nSize) != nSize - 1)
+       {
+               free(lpSystemPath);
+               return NULL;
+       }
 
        save = NULL;
        path = strtok_s(lpSystemPath, ":", &save);
index 6f58ac4..5eeb77d 100644 (file)
@@ -225,7 +225,6 @@ wLogAppender* WLog_FileAppender_New(wLog* log)
        LPCSTR name;
        DWORD nSize;
        wLogFileAppender* FileAppender;
-       BOOL status;
        FileAppender = (wLogFileAppender*) calloc(1, sizeof(wLogFileAppender));
 
        if (!FileAppender)
@@ -244,12 +243,13 @@ wLogAppender* WLog_FileAppender_New(wLog* log)
 
        if (nSize)
        {
+               BOOL status;
                env = (LPSTR) malloc(nSize);
 
                if (!env)
                        goto error_free;
 
-               if (GetEnvironmentVariableA(name, env, nSize) != nSize)
+               if (GetEnvironmentVariableA(name, env, nSize) != nSize - 1)
                {
                        free(env);
                        goto error_free;
@@ -267,13 +267,14 @@ wLogAppender* WLog_FileAppender_New(wLog* log)
 
        if (nSize)
        {
+               BOOL status = FALSE;
                env = (LPSTR) malloc(nSize);
 
                if (!env)
                        goto error_output_file_name;
 
-               GetEnvironmentVariableA(name, env, nSize);
-               status = WLog_FileAppender_SetOutputFileName(FileAppender, env);
+               if (GetEnvironmentVariableA(name, env, nSize) == nSize - 1)
+                       status = WLog_FileAppender_SetOutputFileName(FileAppender, env);
                free(env);
 
                if (!status)
index 7064d19..f733c88 100644 (file)
@@ -173,7 +173,7 @@ wLogAppender* WLog_JournaldAppender_New(wLog* log)
 {
        wLogJournaldAppender* appender;
        DWORD nSize;
-       LPCSTR name;
+       LPCSTR name = "WLOG_JOURNALD_ID";;
 
        appender = (wLogJournaldAppender*) calloc(1, sizeof(wLogJournaldAppender));
        if (!appender)
@@ -188,15 +188,15 @@ wLogAppender* WLog_JournaldAppender_New(wLog* log)
        appender->Set = WLog_JournaldAppender_Set;
        appender->Free = WLog_JournaldAppender_Free;
 
-       name = "WLOG_JOURNALD_ID";
        nSize = GetEnvironmentVariableA(name, NULL, 0);
        if (nSize)
        {
                appender->identifier = (LPSTR) malloc(nSize);
                if (!appender->identifier)
-                       goto error_env_malloc;
+                       goto error_open;
 
-               GetEnvironmentVariableA(name, appender->identifier, nSize);
+               if (GetEnvironmentVariableA(name, appender->identifier, nSize) != nSize -1)
+                       goto error_open;
 
                if (!WLog_JournaldAppender_Open(log, (wLogAppender *)appender))
                        goto error_open;
@@ -206,7 +206,6 @@ wLogAppender* WLog_JournaldAppender_New(wLog* log)
 
 error_open:
        free(appender->identifier);
-error_env_malloc:
        free(appender);
        return NULL;
 }
index 73d1844..058076a 100644 (file)
@@ -353,6 +353,7 @@ BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* form
 
 wLogLayout* WLog_Layout_New(wLog* log)
 {
+       LPCSTR prefix = "WLOG_PREFIX";
        DWORD nSize;
        char* env = NULL;
        wLogLayout* layout;
@@ -361,7 +362,7 @@ wLogLayout* WLog_Layout_New(wLog* log)
        if (!layout)
                return NULL;
 
-       nSize = GetEnvironmentVariableA("WLOG_PREFIX", NULL, 0);
+       nSize = GetEnvironmentVariableA(prefix, NULL, 0);
 
        if (nSize)
        {
@@ -373,7 +374,7 @@ wLogLayout* WLog_Layout_New(wLog* log)
                        return NULL;
                }
 
-               if (GetEnvironmentVariableA("WLOG_PREFIX", env, nSize) != nSize)
+               if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1)
                {
                        free(env);
                        free(layout);
index 90c871b..abda0ca 100644 (file)
@@ -198,9 +198,10 @@ wLogAppender* WLog_UdpAppender_New(wLog* log)
        {
                appender->host = (LPSTR) malloc(nSize);
                if (!appender->host)
-                       goto error_host_alloc;
+                       goto error_open;
 
-               GetEnvironmentVariableA(name, appender->host, nSize);
+               if (GetEnvironmentVariableA(name, appender->host, nSize) != nSize - 1)
+                       goto error_open;
 
                if (!WLog_UdpAppender_Open(log, (wLogAppender *)appender))
                        goto error_open;
@@ -209,14 +210,13 @@ wLogAppender* WLog_UdpAppender_New(wLog* log)
        {
                appender->host = _strdup("127.0.0.1:20000");
                if (!appender->host)
-                       goto error_host_alloc;
+                       goto error_open;
        }
 
        return (wLogAppender*)appender;
 
 error_open:
        free(appender->host);
-error_host_alloc:
        closesocket(appender->sock);
 error_sock:
        free(appender);
index d84708b..2734e37 100644 (file)
@@ -538,12 +538,13 @@ BOOL WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
 
 BOOL WLog_ParseFilters(void)
 {
-       BOOL res;
+       LPCSTR filter = "WLOG_FILTER";
+       BOOL res = FALSE;
        char* env;
        DWORD nSize;
        g_Filters = NULL;
        g_FilterCount = 0;
-       nSize = GetEnvironmentVariableA("WLOG_FILTER", NULL, 0);
+       nSize = GetEnvironmentVariableA(filter, NULL, 0);
 
        if (nSize < 1)
                return TRUE;
@@ -553,10 +554,8 @@ BOOL WLog_ParseFilters(void)
        if (!env)
                return FALSE;
 
-       if (!GetEnvironmentVariableA("WLOG_FILTER", env, nSize))
-               return FALSE;
-
-       res = WLog_AddStringLogFilters(env);
+       if (GetEnvironmentVariableA(filter, env, nSize) == nSize - 1)
+               res = WLog_AddStringLogFilters(env);
        free(env);
        return res;
 }
@@ -684,8 +683,10 @@ wLog* WLog_New(LPCSTR name, wLog* rootLogger)
        }
        else
        {
+               LPCSTR level = "WLOG_LEVEL";
+
                log->Level = WLOG_INFO;
-               nSize = GetEnvironmentVariableA("WLOG_LEVEL", NULL, 0);
+               nSize = GetEnvironmentVariableA(level, NULL, 0);
 
                if (nSize)
                {
@@ -694,9 +695,9 @@ wLog* WLog_New(LPCSTR name, wLog* rootLogger)
                        if (!env)
                                goto out_fail;
 
-                       if (!GetEnvironmentVariableA("WLOG_LEVEL", env, nSize))
+                       if (GetEnvironmentVariableA(level, env, nSize) != nSize - 1)
                        {
-                               fprintf(stderr, "WLOG_LEVEL environment variable changed in my back !\n");
+                               fprintf(stderr, "%s environment variable changed in my back !\n", level);
                                free(env);
                                goto out_fail;
                        }
@@ -750,13 +751,15 @@ wLog* WLog_GetRoot(void)
 
        if (!g_RootLog)
        {
+               LPCSTR appender = "WLOG_APPENDER";
+
                if (!(g_RootLog = WLog_New("", NULL)))
                        return NULL;
 
                g_RootLog->IsRoot = TRUE;
                WLog_ParseFilters();
                logAppenderType = WLOG_APPENDER_CONSOLE;
-               nSize = GetEnvironmentVariableA("WLOG_APPENDER", NULL, 0);
+               nSize = GetEnvironmentVariableA(appender, NULL, 0);
 
                if (nSize)
                {
@@ -765,9 +768,9 @@ wLog* WLog_GetRoot(void)
                        if (!env)
                                goto fail;
 
-                       if (!GetEnvironmentVariableA("WLOG_APPENDER", env, nSize))
+                       if (GetEnvironmentVariableA(appender, env, nSize) != nSize - 1)
                        {
-                               fprintf(stderr, "WLOG_APPENDER environment variable modified in my back");
+                               fprintf(stderr, "%s environment variable modified in my back", appender);
                                free(env);
                                goto fail;
                        }
index 3865d22..878e896 100644 (file)
@@ -727,11 +727,12 @@ static void InitializeWtsApiStubs_Env()
 {
        DWORD nSize;
        char *env = NULL;
+       LPCSTR wts = "WTSAPI_LIBRARY";
 
        if (g_WtsApi)
                return;
 
-       nSize = GetEnvironmentVariableA("WTSAPI_LIBRARY", NULL, 0);
+       nSize = GetEnvironmentVariableA(wts, NULL, 0);
 
        if (!nSize)
                return;
@@ -739,7 +740,7 @@ static void InitializeWtsApiStubs_Env()
        env = (LPSTR) malloc(nSize);
        if (env)
        {
-               if (GetEnvironmentVariableA("WTSAPI_LIBRARY", env, nSize))
+               if (GetEnvironmentVariableA(wts, env, nSize) == nSize - 1)
                        LoadAndInitialize(env);
                free(env);
        }