Code cleanups (strlen, casts, size_t, ...)
authorArmin Novak <armin.novak@thincast.com>
Tue, 29 Oct 2019 09:18:09 +0000 (10:18 +0100)
committerArmin Novak <armin.novak@thincast.com>
Tue, 29 Oct 2019 10:58:43 +0000 (11:58 +0100)
54 files changed:
channels/audin/client/alsa/audin_alsa.c
channels/client/addin.c
channels/cliprdr/cliprdr_common.c
channels/cliprdr/server/cliprdr_main.c
channels/printer/client/printer_main.c
channels/remdesk/client/remdesk_main.c
channels/remdesk/server/remdesk_main.c
channels/serial/client/serial_main.c
channels/tsmf/client/tsmf_media.c
client/X11/xf_client.c
client/X11/xf_floatbar.c
client/X11/xf_window.c
client/common/cmdline.c
client/common/compatibility.c
libfreerdp/common/addin.c
libfreerdp/common/settings.c
libfreerdp/core/client.c
libfreerdp/core/gateway/http.c
libfreerdp/core/gateway/rdg.c
libfreerdp/core/mcs.h
libfreerdp/core/nego.c
libfreerdp/core/peer.c
libfreerdp/core/proxy.c
libfreerdp/core/server.c
libfreerdp/core/test/TestConnect.c
libfreerdp/crypto/certificate.c
libfreerdp/crypto/crypto.c
libfreerdp/crypto/test/TestBase64.c
libfreerdp/crypto/test/TestKnownHosts.c
libfreerdp/crypto/test/Test_x509_cert_info.c
libfreerdp/crypto/tls.c
libfreerdp/gdi/test/TestGdiRop3.c
libfreerdp/locale/keyboard_xkbfile.c
rdtk/librdtk/rdtk_font.c
server/proxy/pf_server.c
server/shadow/shadow_server.c
winpr/libwinpr/clipboard/synthetic.c
winpr/libwinpr/clipboard/test/TestClipboardFormats.c
winpr/libwinpr/crt/string.c
winpr/libwinpr/crt/test/TestUnicodeConversion.c
winpr/libwinpr/crt/unicode.c
winpr/libwinpr/crypto/test/TestCryptoCipher.c
winpr/libwinpr/crypto/test/TestCryptoHash.c
winpr/libwinpr/crypto/test/TestCryptoProtectMemory.c
winpr/libwinpr/environment/environment.c
winpr/libwinpr/environment/test/TestEnvironmentMergeEnvironmentStrings.c
winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c
winpr/libwinpr/file/namedPipeClient.c
winpr/libwinpr/file/test/TestFileGetStdHandle.c
winpr/libwinpr/io/device.c
winpr/libwinpr/library/library.c
winpr/libwinpr/registry/registry.c
winpr/libwinpr/thread/argv.c
winpr/libwinpr/utils/wlog/BinaryAppender.c

index 169422a..fd1b5fe 100644 (file)
@@ -91,6 +91,7 @@ static BOOL audin_alsa_set_params(AudinALSADevice* alsa,
                                   snd_pcm_t* capture_handle)
 {
        int error;
+       SSIZE_T s;
        UINT32 channels = alsa->aformat.nChannels;
        snd_pcm_hw_params_t* hw_params;
        snd_pcm_format_t format = audin_alsa_format(alsa->aformat.wFormatTag, alsa->aformat.wBitsPerSample);
@@ -113,8 +114,13 @@ static BOOL audin_alsa_set_params(AudinALSADevice* alsa,
        snd_pcm_hw_params(capture_handle, hw_params);
        snd_pcm_hw_params_free(hw_params);
        snd_pcm_prepare(capture_handle);
-       alsa->aformat.nChannels = channels;
-       alsa->bytes_per_frame = snd_pcm_format_size(format, 1) * channels;
+       if (channels > UINT16_MAX)
+               return FALSE;
+       s = snd_pcm_format_size(format, 1);
+       if ((s < 0) || (s > UINT16_MAX))
+               return FALSE;
+       alsa->aformat.nChannels = (UINT16)channels;
+       alsa->bytes_per_frame = (size_t)s * channels;
        return TRUE;
 }
 
index 4c7b18a..d3b81b1 100644 (file)
@@ -163,10 +163,10 @@ static FREERDP_ADDIN** freerdp_channels_list_dynamic_addins(LPCSTR pszName, LPCS
        size_t cchInstallPrefix;
        FREERDP_ADDIN** ppAddins;
        WIN32_FIND_DATAA FindData;
-       cchAddinPath = strlen(pszAddinPath);
-       cchInstallPrefix = strlen(pszInstallPrefix);
+       cchAddinPath = strnlen(pszAddinPath, sizeof(FREERDP_ADDIN_PATH));
+       cchInstallPrefix = strnlen(pszInstallPrefix, sizeof(FREERDP_INSTALL_PREFIX));
        pszExtension = PathGetSharedLibraryExtensionA(0);
-       cchPattern = 128 + strlen(pszExtension) + 2;
+       cchPattern = 128 + strnlen(pszExtension, MAX_PATH) + 2;
        pszPattern = (LPSTR) malloc(cchPattern + 1);
 
        if (!pszPattern)
@@ -196,7 +196,7 @@ static FREERDP_ADDIN** freerdp_channels_list_dynamic_addins(LPCSTR pszName, LPCS
                          pszExtension);
        }
 
-       cchPattern = strlen(pszPattern);
+       cchPattern = strnlen(pszPattern, cchPattern);
        cchSearchPath = cchInstallPrefix + cchAddinPath + cchPattern + 3;
        pszSearchPath = (LPSTR) malloc(cchSearchPath + 1);
 
index 7160e26..31180b7 100644 (file)
@@ -216,7 +216,7 @@ wStream* cliprdr_packet_format_list_new(const CLIPRDR_FORMAT_LIST* formatList, B
                        if (asciiNames)
                        {
                                if (szFormatName)
-                                       formatNameLength = strlen(szFormatName);
+                                       formatNameLength = strnlen(szFormatName, 32);
 
                                if (formatNameLength > 31)
                                        formatNameLength = 31;
index c08ce9f..95b8cb6 100644 (file)
@@ -545,7 +545,7 @@ static UINT cliprdr_server_receive_temporary_directory(CliprdrServerContext*
                return ERROR_INVALID_DATA;
        }
 
-       length = strlen(cliprdr->temporaryDirectory);
+       length = strnlen(cliprdr->temporaryDirectory, 520);
 
        if (length > 519)
                length = 519;
index a4c5f30..4c1f359 100644 (file)
@@ -130,7 +130,9 @@ static BOOL printer_write_setting(const char* path, prn_conf_t type, const void*
                if (!base64)
                        goto fail;
 
-               b64len = strlen(base64);
+               /* base64 char represents 6bit -> 4*(n/3) is the length which is
+                * always smaller than 2*n */
+               b64len = strnlen(base64, 2 * length);
                rc = WriteFile(file, base64, b64len, &written, NULL);
 
                if (b64len != written)
index 9bcfc35..2ded478 100644 (file)
@@ -192,7 +192,7 @@ static UINT remdesk_write_channel_header(wStream* s,
                ChannelNameW[index] = (WCHAR) header->ChannelName[index];
        }
 
-       ChannelNameLen = (strlen(header->ChannelName) + 1) * 2;
+       ChannelNameLen = (strnlen(header->ChannelName, sizeof(header->ChannelName)) + 1) * 2;
        Stream_Write_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */
        Stream_Write_UINT32(s, header->DataLength); /* DataLen (4 bytes) */
        Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */
index 447ede4..0478177 100644 (file)
@@ -116,7 +116,7 @@ static UINT remdesk_write_channel_header(wStream* s,
                ChannelNameW[index] = (WCHAR) header->ChannelName[index];
        }
 
-       ChannelNameLen = (strlen(header->ChannelName) + 1) * 2;
+       ChannelNameLen = (strnlen(header->ChannelName, sizeof(header->ChannelName)) + 1) * 2;
        Stream_Write_UINT32(s, ChannelNameLen); /* ChannelNameLen (4 bytes) */
        Stream_Write_UINT32(s, header->DataLength); /* DataLen (4 bytes) */
        Stream_Write(s, ChannelNameW, ChannelNameLen); /* ChannelName (variable) */
index 5d6286b..b0a83ec 100644 (file)
@@ -825,7 +825,7 @@ UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
        char* driver;
        RDPDR_SERIAL* device;
 #if defined __linux__ && !defined ANDROID
-       int i, len;
+       size_t i, len;
        SERIAL_DEVICE* serial;
 #endif /* __linux__ */
        UINT error = CHANNEL_RC_OK;
index bf15835..0e20725 100644 (file)
@@ -491,7 +491,7 @@ static BOOL tsmf_sample_playback_video(TSMF_SAMPLE* sample)
                        fwrite("P5\n", 1, 3, fp);
                        sprintf_s(buf, sizeof(buf), "%"PRIu32" %"PRIu32"\n", sample->stream->width,
                                  sample->stream->height);
-                       fwrite(buf, 1, strlen(buf), fp);
+                       fwrite(buf, 1, strnlen(buf, sizeof(buf)), fp);
                        fwrite("255\n", 1, 4, fp);
                        fwrite(sample->data, 1, sample->stream->width * sample->stream->height, fp);
                        fflush(fp);
index 20998f0..d0f3be3 100644 (file)
@@ -532,7 +532,8 @@ static char* xf_window_get_title(rdpSettings* settings)
                return _strdup(settings->WindowTitle);
 
        port = (settings->ServerPort != 3389);
-       size = strlen(name) + 16;
+       /* Just assume a window title is never longer than a filename... */
+       size = strnlen(name, MAX_PATH) + 16;
        windowTitle = calloc(size, sizeof(char));
 
        if (!windowTitle)
index 1b85773..f504f06 100644 (file)
@@ -392,8 +392,8 @@ static void xf_floatbar_event_expose(xfFloatbar* floatbar, XEvent* event)
        /* draw an border for the floatbar */
        XSetForeground(display, gc, xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
        XDrawLines(display, floatbar->handle, gc, border, 5, CoordModeOrigin);
-       /* draw the host name connected to */
-       len = strlen(floatbar->title);
+       /* draw the host name connected to (limit to maximum file name) */
+       len = strnlen(floatbar->title, MAX_PATH);
        XSetForeground(display, gc, xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_FOREGROUND));
        XDrawString(display, floatbar->handle, gc, floatbar->width / 2 - len * 2, 15,
                    floatbar->title, len);
index ece2966..83c2553 100644 (file)
@@ -107,7 +107,7 @@ typedef struct _PropMotifWmHints PropMotifWmHints;
 
 static void xf_SetWindowTitleText(xfContext* xfc, Window window, const char* name)
 {
-       const size_t i = strlen(name);
+       const size_t i = strnlen(name, MAX_PATH);
        XStoreName(xfc->display, window, name);
        Atom wm_Name = xfc->_NET_WM_NAME;
        Atom utf8Str = xfc->UTF8_STRING;
index ea5a9a3..a9b02eb 100644 (file)
@@ -210,7 +210,7 @@ BOOL freerdp_client_print_buildconfig(void)
 static char* print_token(char* text, int start_offset, int* current, int limit,
                          const char delimiter)
 {
-       int len = (int)strlen(text);
+    size_t len = strlen(text);
 
        if (*current < start_offset)
                *current += printf("%*c", (start_offset - *current), ' ');
index 830866d..5d1eeaf 100644 (file)
@@ -246,7 +246,7 @@ static int freerdp_client_old_command_line_pre_filter(void* context, int index,
                                return -1;
                        }
 
-                       if (_stricmp(&(argv[index])[strlen(argv[index]) - 4], ".rdp") == 0)
+                       if (_stricmp(&(argv[index])[strnlen(argv[index], 9) - 4], ".rdp") == 0)
                        {
                                return -1;
                        }
index 0c3ded4..65cd713 100644 (file)
@@ -241,12 +241,16 @@ PVIRTUALCHANNELENTRY freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName,
        const size_t cchBaseFileName = sizeof(FREERDP_SHARED_LIBRARY_PREFIX) + 32;
        LPCSTR pszExtension;
        LPCSTR pszPrefix = FREERDP_SHARED_LIBRARY_PREFIX;
+       const size_t nameLen = strnlen(pszName, MAX_PATH);
+       const size_t subsystemLen = strnlen(pszSubsystem, MAX_PATH);
+       const size_t typeLen = strlen(pszType);
+       size_t extensionLen;
        pszExtension = PathGetSharedLibraryExtensionA(0);
+       extensionLen = strnlen(pszExtension, MAX_PATH);
 
        if (pszName && pszSubsystem && pszType)
        {
-               const size_t cchFileName = cchBaseFileName + strlen(pszName) + strlen(pszSubsystem) + strlen(
-                                              pszType) + strlen(pszExtension);
+               const size_t cchFileName = cchBaseFileName + nameLen + subsystemLen + typeLen + extensionLen;
                pszFileName = (LPSTR) malloc(cchFileName);
 
                if (!pszFileName)
@@ -257,8 +261,7 @@ PVIRTUALCHANNELENTRY freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName,
        }
        else if (pszName && pszSubsystem)
        {
-               const size_t cchFileName = cchBaseFileName + strlen(pszName) + strlen(pszSubsystem) + strlen(
-                                              pszExtension);
+               const size_t cchFileName = cchBaseFileName + nameLen + subsystemLen + extensionLen;
                pszFileName = (LPSTR) malloc(cchFileName);
 
                if (!pszFileName)
@@ -269,7 +272,7 @@ PVIRTUALCHANNELENTRY freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName,
        }
        else if (pszName)
        {
-               const size_t cchFileName = cchBaseFileName + strlen(pszName) + strlen(pszExtension);
+               const size_t cchFileName = cchBaseFileName + nameLen + extensionLen;
                pszFileName = (LPSTR) malloc(cchFileName);
 
                if (!pszFileName)
@@ -288,7 +291,7 @@ PVIRTUALCHANNELENTRY freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName,
                LPSTR pszEntryName;
                size_t cchEntryName;
                /* subsystem add-in */
-               cchEntryName = 64 + strlen(pszName);
+               cchEntryName = 64 + nameLen;
                pszEntryName = (LPSTR) malloc(cchEntryName + 1);
 
                if (!pszEntryName)
index 0ff6cd1..122ff0d 100644 (file)
@@ -98,7 +98,7 @@ int freerdp_addin_set_argument_value(ADDIN_ARGV* args, char* option, char* value
        int i;
        char* p;
        char* str;
-       int length;
+       size_t length;
        char** new_argv;
        length = strlen(option) + strlen(value) + 1;
        str = (char*) malloc(length + 1);
@@ -142,7 +142,7 @@ int freerdp_addin_replace_argument_value(ADDIN_ARGV* args, char* previous, char*
 {
        int i;
        char* str;
-       int length;
+       size_t length;
        char** new_argv;
        length = strlen(option) + strlen(value) + 1;
        str = (char*) malloc(length + 1);
index 5c8e08a..27bf95f 100644 (file)
@@ -261,12 +261,12 @@ UINT freerdp_channels_attach(freerdp* instance)
        UINT error = CHANNEL_RC_OK;
        int index;
        char* hostname;
-       int hostnameLength;
+       size_t hostnameLength;
        rdpChannels* channels;
        CHANNEL_CLIENT_DATA* pChannelClientData;
        channels = instance->context->channels;
        hostname = instance->settings->ServerHostname;
-       hostnameLength = (int) strlen(hostname);
+       hostnameLength = strlen(hostname);
 
        for (index = 0; index < channels->clientDataCount; index++)
        {
@@ -304,12 +304,12 @@ UINT freerdp_channels_detach(freerdp* instance)
        UINT error = CHANNEL_RC_OK;
        int index;
        char* hostname;
-       int hostnameLength;
+       size_t hostnameLength;
        rdpChannels* channels;
        CHANNEL_CLIENT_DATA* pChannelClientData;
        channels = instance->context->channels;
        hostname = instance->settings->ServerHostname;
-       hostnameLength = (int) strlen(hostname);
+       hostnameLength = strlen(hostname);
 
        for (index = 0; index < channels->clientDataCount; index++)
        {
index a5590f4..8e00bbc 100644 (file)
@@ -91,7 +91,7 @@ static char* string_strnstr(char* str1, const char* str2, size_t slen)
 
        if ((c = *str2++) != '\0')
        {
-               len = strlen(str2);
+               len = strnlen(str2, slen + 1);
 
                do
                {
index c554805..b79810c 100644 (file)
@@ -239,12 +239,12 @@ static BOOL rdg_write_packet(rdpRdg* rdg, wStream* sPacket)
        wStream* sChunk;
        char chunkSize[11];
        sprintf_s(chunkSize, sizeof(chunkSize), "%"PRIXz"\r\n", Stream_Length(sPacket));
-       sChunk = Stream_New(NULL, strlen(chunkSize) + Stream_Length(sPacket) + 2);
+       sChunk = Stream_New(NULL, strnlen(chunkSize, sizeof(chunkSize)) + Stream_Length(sPacket) + 2);
 
        if (!sChunk)
                return FALSE;
 
-       Stream_Write(sChunk, chunkSize, strlen(chunkSize));
+       Stream_Write(sChunk, chunkSize, strnlen(chunkSize, sizeof(chunkSize)));
        Stream_Write(sChunk, Stream_Buffer(sPacket), Stream_Length(sPacket));
        Stream_Write(sChunk, "\r\n", 2);
        Stream_SealLength(sChunk);
@@ -1217,12 +1217,12 @@ static int rdg_write_data_packet(rdpRdg* rdg, const BYTE* buf, int isize)
                return 0;
 
        sprintf_s(chunkSize, sizeof(chunkSize), "%"PRIxz"\r\n", packetSize);
-       sChunk = Stream_New(NULL, strlen(chunkSize) + packetSize + 2);
+       sChunk = Stream_New(NULL, strnlen(chunkSize, sizeof(chunkSize)) + packetSize + 2);
 
        if (!sChunk)
                return -1;
 
-       Stream_Write(sChunk, chunkSize, strlen(chunkSize));
+       Stream_Write(sChunk, chunkSize, strnlen(chunkSize, sizeof(chunkSize)));
        Stream_Write_UINT16(sChunk, PKT_TYPE_DATA);   /* Type */
        Stream_Write_UINT16(sChunk, 0);   /* Reserved */
        Stream_Write_UINT32(sChunk, (UINT32)packetSize);   /* Packet length */
index 6ca3cc7..fc0bcc6 100644 (file)
@@ -32,8 +32,8 @@ typedef struct rdp_mcs rdpMcs;
 
 #include <winpr/stream.h>
 
-#define MCS_BASE_CHANNEL_ID    1001
-#define MCS_GLOBAL_CHANNEL_ID  1003
+#define MCS_BASE_CHANNEL_ID 1001
+#define MCS_GLOBAL_CHANNEL_ID 1003
 
 enum MCS_Result
 {
@@ -149,19 +149,18 @@ struct rdp_mcs
        rdpMcsChannel* channels;
 };
 
-#define MCS_SEND_DATA_HEADER_MAX_LENGTH                8
+#define MCS_SEND_DATA_HEADER_MAX_LENGTH 8
 
-#define MCS_TYPE_CONNECT_INITIAL               0x65
-#define MCS_TYPE_CONNECT_RESPONSE              0x66
+#define MCS_TYPE_CONNECT_INITIAL 0x65
+#define MCS_TYPE_CONNECT_RESPONSE 0x66
 
-FREERDP_LOCAL BOOL mcs_merge_domain_parameters(DomainParameters*
-        targetParameters, DomainParameters* minimumParameters,
-        DomainParameters* maximumParameters, DomainParameters* pOutParameters);
+FREERDP_LOCAL BOOL mcs_merge_domain_parameters(DomainParameters* targetParameters,
+                                               DomainParameters* minimumParameters,
+                                               DomainParameters* maximumParameters,
+                                               DomainParameters* pOutParameters);
 
-FREERDP_LOCAL BOOL mcs_write_connect_initial(wStream* s, rdpMcs* mcs,
-        wStream* userData);
-FREERDP_LOCAL BOOL mcs_write_connect_response(wStream* s, rdpMcs* mcs,
-        wStream* userData);
+FREERDP_LOCAL BOOL mcs_write_connect_initial(wStream* s, rdpMcs* mcs, wStream* userData);
+FREERDP_LOCAL BOOL mcs_write_connect_response(wStream* s, rdpMcs* mcs, wStream* userData);
 
 FREERDP_LOCAL BOOL mcs_recv_connect_initial(rdpMcs* mcs, wStream* s);
 FREERDP_LOCAL BOOL mcs_send_connect_initial(rdpMcs* mcs);
@@ -173,19 +172,16 @@ FREERDP_LOCAL BOOL mcs_recv_attach_user_request(rdpMcs* mcs, wStream* s);
 FREERDP_LOCAL BOOL mcs_send_attach_user_request(rdpMcs* mcs);
 FREERDP_LOCAL BOOL mcs_recv_attach_user_confirm(rdpMcs* mcs, wStream* s);
 FREERDP_LOCAL BOOL mcs_send_attach_user_confirm(rdpMcs* mcs);
-FREERDP_LOCAL BOOL mcs_recv_channel_join_request(rdpMcs* mcs, wStream* s,
-        UINT16* channelId);
+FREERDP_LOCAL BOOL mcs_recv_channel_join_request(rdpMcs* mcs, wStream* s, UINT16* channelId);
 FREERDP_LOCAL BOOL mcs_send_channel_join_request(rdpMcs* mcs, UINT16 channelId);
-FREERDP_LOCAL BOOL mcs_recv_channel_join_confirm(rdpMcs* mcs, wStream* s,
-        UINT16* channelId);
+FREERDP_LOCAL BOOL mcs_recv_channel_join_confirm(rdpMcs* mcs, wStream* s, UINT16* channelId);
 FREERDP_LOCAL BOOL mcs_send_channel_join_confirm(rdpMcs* mcs, UINT16 channelId);
-FREERDP_LOCAL BOOL mcs_recv_disconnect_provider_ultimatum(rdpMcs* mcs,
-        wStream* s, int* reason);
+FREERDP_LOCAL BOOL mcs_recv_disconnect_provider_ultimatum(rdpMcs* mcs, wStream* s, int* reason);
 FREERDP_LOCAL BOOL mcs_send_disconnect_provider_ultimatum(rdpMcs* mcs);
-FREERDP_LOCAL BOOL mcs_read_domain_mcspdu_header(wStream* s,
-        enum DomainMCSPDU* domainMCSPDU, UINT16* length);
-FREERDP_LOCAL void mcs_write_domain_mcspdu_header(wStream* s,
-        enum DomainMCSPDU domainMCSPDU, UINT16 length, BYTE options);
+FREERDP_LOCAL BOOL mcs_read_domain_mcspdu_header(wStream* s, enum DomainMCSPDU* domainMCSPDU,
+                                                 UINT16* length);
+FREERDP_LOCAL void mcs_write_domain_mcspdu_header(wStream* s, enum DomainMCSPDU domainMCSPDU,
+                                                  UINT16 length, BYTE options);
 
 FREERDP_LOCAL BOOL mcs_client_begin(rdpMcs* mcs);
 
index a0b615c..77e8c42 100644 (file)
@@ -711,11 +711,12 @@ static BOOL nego_read_request_token_or_cookie(rdpNego* nego, wStream* s)
        size_t pos, len;
        BOOL result = FALSE;
        BOOL isToken = FALSE;
+       size_t remain = Stream_GetRemainingLength(s);
        str = Stream_Pointer(s);
        pos = Stream_GetPosition(s);
 
        /* minimum length for token is 15 */
-       if (Stream_GetRemainingLength(s) < 15)
+       if (remain < 15)
                return TRUE;
 
        if (memcmp(Stream_Pointer(s), "Cookie: mstshash=", 17) != 0)
@@ -725,13 +726,13 @@ static BOOL nego_read_request_token_or_cookie(rdpNego* nego, wStream* s)
        else
        {
                /* not a token, minimum length for cookie is 19 */
-               if (Stream_GetRemainingLength(s) < 19)
+               if (remain < 19)
                        return TRUE;
 
                Stream_Seek(s, 17);
        }
 
-       while (Stream_GetRemainingLength(s) >= 2)
+       while ((remain = Stream_GetRemainingLength(s)) >= 2)
        {
                Stream_Read_UINT16(s, crlf);
 
@@ -745,9 +746,10 @@ static BOOL nego_read_request_token_or_cookie(rdpNego* nego, wStream* s)
        {
                Stream_Rewind(s, 2);
                len = Stream_GetPosition(s) - pos;
+               remain = Stream_GetRemainingLength(s);
                Stream_Write_UINT16(s, 0);
 
-               if (strlen((char*)str) == len)
+               if (strnlen((char*)str, remain) == len)
                {
                        if (isToken)
                                result = nego_set_routing_token(nego, str, len);
index 9721d11..b22fb4f 100644 (file)
@@ -48,7 +48,7 @@ static HANDLE freerdp_peer_virtual_channel_open(freerdp_peer* client, const char
        if (flags & WTS_CHANNEL_OPTION_DYNAMIC)
                return NULL; /* not yet supported */
 
-       length = strlen(name);
+       length = strnlen(name, 9);
 
        if (length > 8)
                return NULL; /* SVC maximum name length is 8 */
index 029b794..4ef8d85 100644 (file)
@@ -406,7 +406,7 @@ static BOOL http_proxy_connect(BIO* bufferedBio, const char* hostname, UINT16 po
        Stream_Write(s, " HTTP/1.1" CRLF "Host: ", 17);
        Stream_Write(s, hostname, strlen(hostname));
        Stream_Write_UINT8(s, ':');
-       Stream_Write(s, port_str, strlen(port_str));
+       Stream_Write(s, port_str, strnlen(port_str, sizeof (port_str)));
        Stream_Write(s, CRLF CRLF, 4);
        status = BIO_write(bufferedBio, Stream_Buffer(s), Stream_GetPosition(s));
 
@@ -468,7 +468,7 @@ static BOOL http_proxy_connect(BIO* bufferedBio, const char* hostname, UINT16 po
        *eol = '\0';
        WLog_INFO(TAG, "HTTP Proxy: %s", recv_buf);
 
-       if (strlen(recv_buf) < 12)
+       if (strnlen(recv_buf, sizeof(recv_buf)) < 12)
        {
                return FALSE;
        }
index 41299ec..de49c42 100644 (file)
@@ -352,11 +352,11 @@ static void wts_write_drdynvc_header(wStream* s, BYTE Cmd, UINT32 ChannelId)
 static BOOL wts_write_drdynvc_create_request(wStream* s, UINT32 ChannelId,
         const char* ChannelName)
 {
-       UINT32 len;
+       size_t len;
        wts_write_drdynvc_header(s, CREATE_REQUEST_PDU, ChannelId);
-       len = strlen(ChannelName) + 1;
+       len = strnlen(ChannelName, CHANNEL_NAME_LEN) + 1;
 
-       if (!Stream_EnsureRemainingCapacity(s, (int) len))
+       if (!Stream_EnsureRemainingCapacity(s, len))
                return FALSE;
 
        Stream_Write(s, ChannelName, len);
@@ -524,7 +524,7 @@ static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs,
 {
        UINT32 index;
 
-       if (!mcs || !channel_name || !strlen(channel_name))
+       if (!mcs || !channel_name || !strnlen(channel_name, CHANNEL_NAME_LEN))
                return NULL;
 
        for (index = 0; index < mcs->channelCount; index++)
@@ -532,7 +532,7 @@ static rdpMcsChannel* wts_get_joined_channel_by_name(rdpMcs* mcs,
                if (mcs->channels[index].joined)
                {
                        if (_strnicmp(mcs->channels[index].Name, channel_name,
-                                     strlen(channel_name)) == 0)
+                                                 strnlen(channel_name, CHANNEL_NAME_LEN)) == 0)
                                return &mcs->channels[index];
                }
        }
@@ -990,7 +990,7 @@ BOOL WINAPI FreeRDP_WTSWaitSystemEvent(HANDLE hServer, DWORD EventMask,
 HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId,
         LPSTR pVirtualName)
 {
-       int length;
+       size_t length;
        UINT32 index;
        rdpMcs* mcs;
        BOOL joined = FALSE;
index a3bdd27..890888e 100644 (file)
@@ -184,7 +184,7 @@ static int testSuccess(int port)
                NULL
        };
        char* commandLine = NULL;
-       int commandLineLen;
+       size_t commandLineLen;
        int argc = 4;
        char* path = NULL;
        char* wpath = NULL;
index a222754..32cdefc 100644 (file)
@@ -705,14 +705,14 @@ rdpCertificateData* certificate_data_new(const char* hostname, UINT16 port, cons
        certdata->hostname = _strdup(hostname);
 
        if (subject)
-               certdata->subject = crypto_base64_encode((BYTE*)subject, strlen(subject));
+               certdata->subject = crypto_base64_encode((const BYTE*)subject, strlen(subject));
        else
-               certdata->subject = crypto_base64_encode((BYTE*)"", 0);
+               certdata->subject = crypto_base64_encode((const BYTE*)"", 0);
 
        if (issuer)
-               certdata->issuer = crypto_base64_encode((BYTE*)issuer, strlen(issuer));
+               certdata->issuer = crypto_base64_encode((const BYTE*)issuer, strlen(issuer));
        else
-               certdata->issuer = crypto_base64_encode((BYTE*)"", 0);
+               certdata->issuer = crypto_base64_encode((const BYTE*)"", 0);
 
        certdata->fingerprint = _strdup(fingerprint);
 
index 0b2133e..6dc51cf 100644 (file)
@@ -741,7 +741,7 @@ char** crypto_cert_get_dns_names(X509* x509, int* count, int** lengths)
        for (i = 0; i < list.count; i ++)
        {
                result[i] = list.strings[i];
-               (*lengths)[i] = strlen(result[i]);
+               (*lengths)[i] = strnlen(result[i], sizeof(*result));
        }
 
        string_list_free(&list);
index 7752bc9..2f14c73 100644 (file)
@@ -26,7 +26,7 @@ struct Encode64test
        const char* output;
 };
 
-struct Encode64test encodeTests[] =
+static const struct Encode64test encodeTests[] =
 {
        {"\x00",                        1, "AA=="},
        {"\x00\x00",            2, "AAA="},
@@ -39,7 +39,6 @@ struct Encode64test encodeTests[] =
        {NULL, -1, NULL},  /*  /!\ last one  /!\ */
 };
 
-
 int TestBase64(int argc, char* argv[])
 {
        int i, testNb = 0;
index 44ba580..94fab11 100644 (file)
 
 static int prepare(const char* currentFileV2, const char* legacyFileV2, const char* legacyFile)
 {
-       char* legacy[] =
+       const char* legacy[] =
        {
                "someurl ff:11:22:dd\r\n",
                "otherurl aa:bb:cc:dd\r",
                "legacyurl aa:bb:cc:dd\n"
        };
-       char* hosts[] =
+       const char* hosts[] =
        {
                "#somecomment\r\n"
                "someurl 3389 ff:11:22:dd subject issuer\r\n"
@@ -51,7 +51,7 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
        if (!fl)
                goto finish;
 
-       for (i = 0; i < sizeof(hosts) / sizeof(hosts[0]); i++)
+       for (i = 0; i < ARRAYSIZE(hosts); i++)
        {
                if (fwrite(hosts[i], strlen(hosts[i]), 1, fl) != 1 ||
                    fwrite(hosts[i], strlen(hosts[i]), 1, fc) != 1)
@@ -67,7 +67,7 @@ static int prepare(const char* currentFileV2, const char* legacyFileV2, const ch
        if (!fl)
                goto finish;
 
-       for (i = 0; i < sizeof(legacy) / sizeof(legacy[0]); i++)
+       for (i = 0; i < ARRAYSIZE(legacy); i++)
        {
                if (fwrite(legacy[i], strlen(legacy[i]), 1, fl) != 1)
                        goto finish;
index f02e4d4..6a2d2c2 100644 (file)
@@ -31,13 +31,13 @@ char* certificate_path()
 #else
        static const char dirsep = '/';
 #endif
-       static const char* filename = "Test_x509_cert_info.pem";
+       static const char filename[] = "Test_x509_cert_info.pem";
        const char* file = __FILE__;
        const char* last_dirsep = strrchr(file, dirsep);
 
        if (last_dirsep)
        {
-               char* result = malloc(last_dirsep - file + 1 + strlen(filename) + 1);
+               char* result = malloc(last_dirsep - file + 1 + strnlen(filename, sizeof(filename)) + 1);
                strncpy(result, file, (last_dirsep - file + 1));
                strcpy(result + (last_dirsep - file + 1), filename);
                return result;
index 09e6ef1..7f4ac8e 100644 (file)
@@ -213,7 +213,7 @@ static int bio_rdp_tls_read(BIO* bio, char* buf, int size)
 
 static int bio_rdp_tls_puts(BIO* bio, const char* str)
 {
-       int size;
+       size_t size;
        int status;
 
        if (!str)
index bd4486e..c5691d5 100644 (file)
 
 static char* gdi_convert_postfix_to_infix(const char* postfix)
 {
-       int i;
-       int length;
+       size_t i;
+       size_t length;
        BOOL unary;
        wStack* stack;
-       int al, bl, cl, dl;
+       size_t al, bl, cl, dl;
        char* a, *b, *c, *d;
        bl = cl = dl = 0;
        stack = Stack_New(FALSE);
index 660fd0b..fd84e7f 100644 (file)
@@ -435,7 +435,7 @@ int freerdp_keyboard_load_map_from_xkbfile(void* display, DWORD x11_keycode_to_r
                                found = FALSE;
                                CopyMemory(xkb_keyname, xkb->names->keys[i].name, 4);
 
-                               if (strlen(xkb_keyname) < 1)
+                               if (strnlen(xkb_keyname, sizeof(xkb_keyname)) < 1)
                                        continue;
 
                                for (j = 0; j < ARRAYSIZE(XKB_KEY_NAME_SCANCODE_TABLE); j++)
index 884c482..4564d46 100644 (file)
@@ -107,8 +107,8 @@ static int rdtk_font_draw_glyph(rdtkSurface* surface, int nXDst, int nYDst, rdtk
 int rdtk_font_draw_text(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* font,
                         const char* text)
 {
-       int index;
-       int length;
+       size_t index;
+       size_t length;
        rdtkGlyph* glyph;
        font = surface->engine->font;
        length = strlen(text);
@@ -125,8 +125,8 @@ int rdtk_font_draw_text(rdtkSurface* surface, int nXDst, int nYDst, rdtkFont* fo
 
 int rdtk_font_text_draw_size(rdtkFont* font, int* width, int* height, const char* text)
 {
-       int index;
-       int length;
+       size_t index;
+       size_t length;
        int glyphIndex;
        rdtkGlyph* glyph;
        *width = 0;
@@ -201,7 +201,7 @@ static char* rdtk_font_load_descriptor_file(const char* filename, int* pSize)
 
 static int rdtk_font_convert_descriptor_code_to_utf8(const char* str, BYTE* utf8)
 {
-       int len = strlen(str);
+       size_t len = strlen(str);
        *((UINT32*) utf8) = 0;
 
        if (len < 1)
@@ -587,7 +587,7 @@ static int rdtk_font_load_descriptor(rdtkFont* font, const char* filename)
 rdtkFont* rdtk_font_new(rdtkEngine* engine, const char* path, const char* file)
 {
        int status;
-       int length;
+       size_t length;
        rdtkFont* font = NULL;
        char* fontBaseFile = NULL;
        char* fontImageFile = NULL;
index 86fc21c..b71e93a 100644 (file)
@@ -61,7 +61,7 @@ static BOOL pf_server_parse_target_from_routing_token(rdpContext* context,
 #define ROUTING_TOKEN_PREFIX "Cookie: msts="
        char* colon;
        size_t len;
-       const size_t prefix_len  = strlen(ROUTING_TOKEN_PREFIX);
+       const size_t prefix_len  = strnlen(ROUTING_TOKEN_PREFIX, sizeof(ROUTING_TOKEN_PREFIX));
        DWORD routing_token_length;
        const char* routing_token = freerdp_nego_get_routing_token(context, &routing_token_length);
 
index 3d1566a..9e6ac2c 100644 (file)
@@ -69,7 +69,7 @@ static COMMAND_LINE_ARGUMENT_A shadow_args[] =
 static int shadow_server_print_command_line_help(int argc, char** argv)
 {
        char* str;
-       int length;
+       size_t length;
        COMMAND_LINE_ARGUMENT_A* arg;
 
        if (argc < 1)
@@ -98,7 +98,7 @@ static int shadow_server_print_command_line_help(int argc, char** argv)
 
                        if (arg->Format)
                        {
-                               length = (int)(strlen(arg->Name) + strlen(arg->Format) + 2);
+                               length = (strlen(arg->Name) + strlen(arg->Format) + 2);
                                str = (char*) malloc(length + 1);
 
                                if (!str)
@@ -117,7 +117,7 @@ static int shadow_server_print_command_line_help(int argc, char** argv)
                }
                else if (arg->Flags & COMMAND_LINE_VALUE_BOOL)
                {
-                       length = (int) strlen(arg->Name) + 32;
+                       length = strlen(arg->Name) + 32;
                        str = (char*) malloc(length + 1);
 
                        if (!str)
index 52606b1..a1d97f0 100644 (file)
@@ -382,7 +382,7 @@ static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 form
                        body = strstr(pSrcData, "<BODY");
 
                /* StartHTML */
-               sprintf_s(num, sizeof(num), "%010"PRIuz"", strlen(pDstData));
+               sprintf_s(num, sizeof(num), "%010"PRIuz"", strnlen(pDstData, SrcSize + 200));
                CopyMemory(&pDstData[23], num, 10);
 
                if (!body)
@@ -390,11 +390,11 @@ static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 form
 
                strcat(pDstData, "<!--StartFragment-->");
                /* StartFragment */
-               sprintf_s(num, sizeof(num), "%010"PRIuz"", strlen(pDstData));
+               sprintf_s(num, sizeof(num), "%010"PRIuz"", strnlen(pDstData, SrcSize + 200));
                CopyMemory(&pDstData[69], num, 10);
                strcat(pDstData, pSrcData);
                /* EndFragment */
-               sprintf_s(num, sizeof(num), "%010"PRIuz"", strlen(pDstData));
+               sprintf_s(num, sizeof(num), "%010"PRIuz"", strnlen(pDstData, SrcSize + 200));
                CopyMemory(&pDstData[93], num, 10);
                strcat(pDstData, "<!--EndFragment-->");
 
@@ -402,7 +402,7 @@ static void* clipboard_synthesize_html_format(wClipboard* clipboard, UINT32 form
                        strcat(pDstData, "</BODY></HTML>");
 
                /* EndHTML */
-               sprintf_s(num, sizeof(num), "%010"PRIuz"", strlen(pDstData));
+               sprintf_s(num, sizeof(num), "%010"PRIuz"", strnlen(pDstData, SrcSize + 200));
                CopyMemory(&pDstData[43], num, 10);
                *pSize = (UINT32) strlen(pDstData) + 1;
                free(pSrcData);
index 8fb3252..2ecfe68 100644 (file)
@@ -47,7 +47,7 @@ int TestClipboardFormats(int argc, char* argv[])
                        return -1;
                }
 
-               SrcSize = (UINT32)(strlen(pSrcData) + 1);
+               SrcSize = (UINT32)(strnlen(pSrcData, UINT32_MAX - 1) + 1);
                bSuccess = ClipboardSetData(clipboard, utf8StringFormatId, pSrcData,
                                            SrcSize);
                fprintf(stderr, "ClipboardSetData: %"PRId32"\n", bSuccess);
index d30ac11..dda8417 100644 (file)
@@ -217,13 +217,13 @@ WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context)
 
 LPSTR CharUpperA(LPSTR lpsz)
 {
-       int i;
-       int length;
+       size_t i;
+       size_t length;
 
        if (!lpsz)
                return NULL;
 
-       length = (int) strlen(lpsz);
+       length = strlen(lpsz);
 
        if (length < 1)
                return (LPSTR) NULL;
@@ -287,13 +287,13 @@ DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
 
 LPSTR CharLowerA(LPSTR lpsz)
 {
-       int i;
-       int length;
+       size_t i;
+       size_t length;
 
        if (!lpsz)
                return (LPSTR) NULL;
 
-       length = (int) strlen(lpsz);
+       length = strlen(lpsz);
 
        if (length < 1)
                return (LPSTR) NULL;
index e2c29f7..6f91999 100644 (file)
@@ -86,14 +86,14 @@ static BYTE ru_Administrator_lower[] =
 static BYTE ru_Administrator_upper[] =
                "\xd0\x90\xd0\x94\xd0\x9c\xd0\x98\xd0\x9d\xd0\x98\xd0\xa1\xd0\xa2\xd0\xa0\xd0\x90\xd0\xa2\xd0\x9e\xd0\xa0\x00";
 
-void string_hexdump(BYTE* data, int length)
+static void string_hexdump(const BYTE* data, size_t length)
 {
-       BYTE* p = data;
-       int i, line, offset = 0;
+       const BYTE* p = data;
+       size_t i, line, offset = 0;
 
        while (offset < length)
        {
-               printf("%04x ", offset);
+               printf("%04"PRIxz" ", offset);
 
                line = length - offset;
 
@@ -116,11 +116,11 @@ void string_hexdump(BYTE* data, int length)
        }
 }
 
-int convert_utf8_to_utf16(BYTE* lpMultiByteStr, BYTE* expected_lpWideCharStr, int expected_cchWideChar)
+static int convert_utf8_to_utf16(BYTE* lpMultiByteStr, BYTE* expected_lpWideCharStr, int expected_cchWideChar)
 {
        int rc = -1;
        int length;
-       int cbMultiByte;
+       size_t cbMultiByte;
        int cchWideChar;
        LPWSTR lpWideCharStr = NULL;
 
@@ -139,7 +139,7 @@ int convert_utf8_to_utf16(BYTE* lpMultiByteStr, BYTE* expected_lpWideCharStr, in
                goto fail;
        }
 
-       lpWideCharStr = (LPWSTR) calloc(cchWideChar, sizeof(WCHAR));
+       lpWideCharStr = (LPWSTR) calloc((size_t)cchWideChar, sizeof(WCHAR));
        if (!lpWideCharStr)
        {
                printf("MultiByteToWideChar: unable to allocate memory for test\n");
@@ -191,7 +191,7 @@ fail:
        return rc;
 }
 
-int convert_utf16_to_utf8(BYTE* lpWideCharStr, BYTE* expected_lpMultiByteStr, int expected_cbMultiByte)
+static int convert_utf16_to_utf8(BYTE* lpWideCharStr, BYTE* expected_lpMultiByteStr, int expected_cbMultiByte)
 {
        int rc = -1;
        int length;
@@ -266,7 +266,7 @@ fail:
        return rc;
 }
 
-BOOL test_unicode_uppercasing(BYTE* lower, BYTE* upper)
+static BOOL test_unicode_uppercasing(BYTE* lower, BYTE* upper)
 {
        WCHAR* lowerW = NULL;
        int lowerLength;
@@ -296,12 +296,12 @@ BOOL test_unicode_uppercasing(BYTE* lower, BYTE* upper)
        return TRUE;
 }
 
-BOOL test_ConvertFromUnicode_wrapper()
+static BOOL test_ConvertFromUnicode_wrapper(void)
 {
-       BYTE src1[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x40\x00\x40\x00\x40\x00";
-       BYTE src2[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x00\x00";
+       const BYTE src1[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x40\x00\x40\x00\x40\x00";
+       const BYTE src2[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x00\x00";
        /*               00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18 */
-       CHAR  cmp0[] = { 'R','I','C','H',' ','T','E','X','T',' ','F','O','R','M','A','T', 0 };
+       const CHAR  cmp0[] = { 'R','I','C','H',' ','T','E','X','T',' ','F','O','R','M','A','T', 0 };
        CHAR* dst = NULL;
        int i;
 
@@ -380,28 +380,30 @@ fail:
        return FALSE;
 }
 
-BOOL test_ConvertToUnicode_wrapper()
+static BOOL test_ConvertToUnicode_wrapper(void)
 {
        /*               00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18 */
-       CHAR  src1[] = { 'R','I','C','H',' ','T','E','X','T',' ','F','O','R','M','A','T','@','@','@' };
-       CHAR  src2[] = { 'R','I','C','H',' ','T','E','X','T',' ','F','O','R','M','A','T', 0 };
-       BYTE cmp0[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x00\x00";
+       const CHAR  src1[] = { 'R','I','C','H',' ','T','E','X','T',' ','F','O','R','M','A','T','@','@','@' };
+       const CHAR  src2[] = { 'R','I','C','H',' ','T','E','X','T',' ','F','O','R','M','A','T', 0 };
+       const BYTE cmp0[] = "\x52\x00\x49\x00\x43\x00\x48\x00\x20\x00\x54\x00\x45\x00\x58\x00\x54\x00\x20\x00\x46\x00\x4f\x00\x52\x00\x4d\x00\x41\x00\x54\x00\x00\x00";
        WCHAR* dst = NULL;
-       int i;
+       int ii;
+       size_t i;
 
        /* Test unterminated unicode string:
         * ConvertToUnicode must always null-terminate, even if the src string isn't
         */
 
        printf("Input UTF8 String:\n");
-       string_hexdump((BYTE*) src1, 19);
+       string_hexdump((const BYTE*) src1, 19);
 
-       i = ConvertToUnicode(CP_UTF8, 0, src1, 16, &dst, 0);
-       if (i != 16)
+       ii = ConvertToUnicode(CP_UTF8, 0, src1, 16, &dst, 0);
+       if (ii != 16)
        {
-               fprintf(stderr, "ConvertToUnicode failure A1: unexpectedly returned %d instead of 16\n", i);
+               fprintf(stderr, "ConvertToUnicode failure A1: unexpectedly returned %d instead of 16\n", ii);
                goto fail;
        }
+       i = (size_t)ii;
        if (dst == NULL)
        {
                fprintf(stderr, "ConvertToUnicode failure A2: destination ist NULL\n");
@@ -412,13 +414,13 @@ BOOL test_ConvertToUnicode_wrapper()
                fprintf(stderr, "ConvertToUnicode failure A3: dst length is %d instead of 16\n", i);
                goto fail;
        }
-       if (_wcscmp(dst, (WCHAR*)cmp0))
+       if (_wcscmp(dst, (const WCHAR*)cmp0))
        {
                fprintf(stderr, "ConvertToUnicode failure A4: data mismatch\n");
                goto fail;
        }
        printf("Output UTF16 String:\n");
-       string_hexdump((BYTE*) dst, (i + 1) * sizeof(WCHAR));
+       string_hexdump((const BYTE*) dst, (i + 1) * sizeof(WCHAR));
 
        free(dst);
        dst = NULL;
@@ -426,7 +428,7 @@ BOOL test_ConvertToUnicode_wrapper()
        /* Test null-terminated string */
 
        printf("Input UTF8 String:\n");
-       string_hexdump((BYTE*) src2, strlen(src2) + 1);
+       string_hexdump((const BYTE*) src2, strlen(src2) + 1);
 
        i = ConvertToUnicode(CP_UTF8, 0, src2, -1, &dst, 0);
        if (i != 17)
index 2768832..8ba80e3 100644 (file)
@@ -168,7 +168,7 @@ int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
 
        if (cbMultiByte == -1)
        {
-               size_t len = strlen((const char*) lpMultiByteStr);
+               size_t len = strnlen((const char*) lpMultiByteStr, INT32_MAX);
                if (len >= INT32_MAX)
                        return 0;
                cbMultiByte = (int)len + 1;
@@ -389,7 +389,7 @@ int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
 
        if (cbMultiByte == -1)
        {
-               size_t len = strlen(lpMultiByteStr);
+               size_t len = strnlen(lpMultiByteStr, INT_MAX);
                if (len >= INT_MAX)
                        return 0;
                cbMultiByte = (int)(len + 1);
index 35d75d9..7715586 100644 (file)
@@ -30,7 +30,7 @@ static BOOL test_crypto_cipher_aes_128_cbc()
        memset(ibuf, 0, sizeof(ibuf));
        memset(obuf, 0, sizeof(obuf));
 
-       ilen = strlen(plaintext) + 1;
+       ilen = strnlen(plaintext, sizeof(plaintext)) + 1;
        memcpy(ibuf, plaintext, ilen);
 
        ilen = ((ilen + 15) / 16) * 16;
@@ -121,7 +121,7 @@ static BOOL test_crypto_cipher_rc4()
        BYTE* text = NULL;
        WINPR_RC4_CTX* ctx;
 
-       len = strlen(TEST_RC4_PLAINTEXT);
+       len = strnlen(TEST_RC4_PLAINTEXT, sizeof(TEST_RC4_PLAINTEXT));
 
        if (!(text = (BYTE*) calloc(1, len)))
        {
@@ -129,12 +129,12 @@ static BOOL test_crypto_cipher_rc4()
                goto out;
        }
 
-       if ((ctx = winpr_RC4_New(TEST_RC4_KEY, strlen((char*) TEST_RC4_KEY))) == NULL)
+       if ((ctx = winpr_RC4_New(TEST_RC4_KEY, strnlen((const char*)TEST_RC4_KEY, sizeof(TEST_RC4_KEY)))) == NULL)
        {
                fprintf(stderr, "%s: winpr_RC4_New failed\n", __FUNCTION__);
                goto out;
        }
-       rc = winpr_RC4_Update(ctx, len, (BYTE*) TEST_RC4_PLAINTEXT, text);
+       rc = winpr_RC4_Update(ctx, len, (const BYTE*) TEST_RC4_PLAINTEXT, text);
        winpr_RC4_Free(ctx);
        if (!rc)
        {
index be944cc..a3290b8 100644 (file)
@@ -23,7 +23,7 @@ static BOOL test_crypto_hash_md5(void)
                fprintf(stderr, "%s: winpr_Digest_Init failed\n", __FUNCTION__);
                goto out;
        }
-       if (!winpr_Digest_Update(ctx, (BYTE*) TEST_MD5_DATA, strlen(TEST_MD5_DATA)))
+       if (!winpr_Digest_Update(ctx, (const BYTE*) TEST_MD5_DATA, strnlen(TEST_MD5_DATA, sizeof(TEST_MD5_DATA))))
        {
                fprintf(stderr, "%s: winpr_Digest_Update failed\n", __FUNCTION__);
                goto out;
@@ -74,7 +74,7 @@ static BOOL test_crypto_hash_md4(void)
                fprintf(stderr, "%s: winpr_Digest_Init failed\n", __FUNCTION__);
                goto out;
        }
-       if (!winpr_Digest_Update(ctx, (BYTE*) TEST_MD4_DATA, strlen(TEST_MD4_DATA)))
+       if (!winpr_Digest_Update(ctx, (const BYTE*) TEST_MD4_DATA, strnlen(TEST_MD4_DATA, sizeof(TEST_MD4_DATA))))
        {
                fprintf(stderr, "%s: winpr_Digest_Update failed\n", __FUNCTION__);
                goto out;
@@ -125,7 +125,7 @@ static BOOL test_crypto_hash_sha1(void)
                fprintf(stderr, "%s: winpr_Digest_Init failed\n", __FUNCTION__);
                goto out;
        }
-       if (!winpr_Digest_Update(ctx, (BYTE*) TEST_SHA1_DATA, strlen(TEST_SHA1_DATA)))
+       if (!winpr_Digest_Update(ctx, (const BYTE*) TEST_SHA1_DATA, strnlen(TEST_SHA1_DATA, sizeof(TEST_SHA1_DATA))))
        {
                fprintf(stderr, "%s: winpr_Digest_Update failed\n", __FUNCTION__);
                goto out;
@@ -179,7 +179,7 @@ static BOOL test_crypto_hash_hmac_md5(void)
                fprintf(stderr, "%s: winpr_HMAC_Init failed\n", __FUNCTION__);
                goto out;
        }
-       if (!winpr_HMAC_Update(ctx, (BYTE*) TEST_HMAC_MD5_DATA, strlen(TEST_HMAC_MD5_DATA)))
+       if (!winpr_HMAC_Update(ctx, (const BYTE*) TEST_HMAC_MD5_DATA, strnlen(TEST_HMAC_MD5_DATA, sizeof(TEST_HMAC_MD5_DATA))))
        {
                fprintf(stderr, "%s: winpr_HMAC_Update failed\n", __FUNCTION__);
                goto out;
@@ -233,7 +233,7 @@ static BOOL test_crypto_hash_hmac_sha1(void)
                fprintf(stderr, "%s: winpr_HMAC_Init failed\n", __FUNCTION__);
                goto out;
        }
-       if (!winpr_HMAC_Update(ctx, (BYTE*) TEST_HMAC_SHA1_DATA, strlen(TEST_HMAC_SHA1_DATA)))
+       if (!winpr_HMAC_Update(ctx, (const      BYTE*) TEST_HMAC_SHA1_DATA, strnlen(TEST_HMAC_SHA1_DATA, sizeof(TEST_HMAC_SHA1_DATA))))
        {
                fprintf(stderr, "%s: winpr_HMAC_Update failed\n", __FUNCTION__);
                goto out;
index 3e5690e..6f7d1e1 100644 (file)
@@ -11,9 +11,9 @@ int TestCryptoProtectMemory(int argc, char* argv[])
 {
        int cbPlainText;
        int cbCipherText;
-       char* pPlainText;
+       const char* pPlainText;
        BYTE* pCipherText;
-       pPlainText = (char*) SECRET_PASSWORD_TEST;
+       pPlainText = SECRET_PASSWORD_TEST;
        cbPlainText = strlen(pPlainText) + 1;
        cbCipherText = cbPlainText + (CRYPTPROTECTMEMORY_BLOCK_SIZE - (cbPlainText % CRYPTPROTECTMEMORY_BLOCK_SIZE));
        printf("cbPlainText: %d cbCipherText: %d\n", cbPlainText, cbCipherText);
index d271ce4..81a2daf 100644 (file)
@@ -222,8 +222,8 @@ LPCH GetEnvironmentStringsA(VOID)
 {
 #if !defined(_UWP)
        char* p;
-       int offset;
-       int length;
+       size_t offset;
+       size_t length;
        char** envp;
        DWORD cchEnvironmentBlock;
        LPCH lpszEnvironmentBlock;
@@ -238,9 +238,9 @@ LPCH GetEnvironmentStringsA(VOID)
 
        while (*envp)
        {
-               length = (int) strlen(*envp);
+               length = strlen(*envp);
 
-               while ((offset + length + 8) > (int) cchEnvironmentBlock)
+               while ((offset + length + 8) > cchEnvironmentBlock)
                {
                        DWORD new_size;
                        LPCH new_blk;
@@ -317,17 +317,17 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
 {
        const char* cp;
        char* p;
-       int offset;
-       int length;
+       size_t offset;
+       size_t length;
        const char* envp;
        DWORD cchEnvironmentBlock;
        LPCH lpszEnvironmentBlock;
        const char** mergeStrings;
-       int mergeStringLength;
-       int mergeArraySize = 128;
-       int run;
-       int mergeLength;
-       int foundMerge;
+       size_t mergeStringLength;
+       size_t mergeArraySize = 128;
+       size_t run;
+       size_t mergeLength;
+       size_t foundMerge;
        char* foundEquals;
 
        mergeStrings = (LPCSTR*) calloc(mergeArraySize, sizeof(char*));
@@ -341,7 +341,7 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
 
        while (*cp && *(cp + 1))
        {
-               length = (int) strlen(cp);
+               length = strlen(cp);
 
                if (mergeStringLength == mergeArraySize)
                {
@@ -378,10 +378,10 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
 
        while ((original != NULL) && (*envp && *(envp+1)))
        {
-               ULONG old_offset = offset;
-               length = (int) strlen(envp);
+               size_t old_offset = offset;
+               length = strlen(envp);
 
-               while ((offset + length + 8) > (int) cchEnvironmentBlock)
+               while ((offset + length + 8) > cchEnvironmentBlock)
                {
                        LPCH tmp;
                        cchEnvironmentBlock *= 2;
@@ -405,7 +405,7 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
                        if (!mergeStrings[run])
                                continue;
 
-                       mergeLength = (int) strlen(mergeStrings[run]);
+                       mergeLength = strlen(mergeStrings[run]);
                        foundEquals = strstr(mergeStrings[run], "=");
 
                        if (!foundEquals)
@@ -421,7 +421,7 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
                                }
                                else
                                {
-                                       while ((offset + mergeLength + 8) > (int) cchEnvironmentBlock)
+                                       while ((offset + mergeLength + 8) > cchEnvironmentBlock)
                                        {
                                                LPCH tmp;
                                                cchEnvironmentBlock *= 2;
@@ -462,9 +462,9 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
                if (!mergeStrings[run])
                        continue;
 
-               mergeLength = (int) strlen(mergeStrings[run]);
+               mergeLength = strlen(mergeStrings[run]);
 
-               while ((offset + mergeLength + 8) > (int) cchEnvironmentBlock)
+               while ((offset + mergeLength + 8) > cchEnvironmentBlock)
                {
                        LPCH tmp;
                        cchEnvironmentBlock *= 2;
@@ -497,23 +497,23 @@ LPCH MergeEnvironmentStrings(PCSTR original, PCSTR merge)
 
 DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
 {
-       int vLength = 0;
+       size_t vLength = 0;
        char* env = NULL;
        char* foundEquals;
        const char* penvb = envBlock;
-       int nLength, fLength, lpNameLength;
+       size_t nLength, fLength, lpNameLength;
 
        if (!lpName || NULL == envBlock)
                return 0;
 
-       lpNameLength = (int) strlen(lpName);
+       lpNameLength = strlen(lpName);
 
        if (lpNameLength < 1)
                return 0;
 
        while (*penvb && *(penvb + 1))
        {
-               fLength = (int) strlen(penvb);
+               fLength = strlen(penvb);
                foundEquals = strstr(penvb,"=");
 
                if (!foundEquals)
@@ -522,7 +522,7 @@ DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer,
                        return 0;
                }
 
-               nLength = (int) (foundEquals - penvb);
+               nLength = (foundEquals - penvb);
 
                if (nLength != lpNameLength)
                {
@@ -542,9 +542,9 @@ DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer,
        if (!env)
                return 0;
 
-       vLength = (int) strlen(env);
+       vLength = strlen(env);
 
-       if ((vLength + 1 > (int) nSize) || (!lpBuffer))
+       if ((vLength + 1 > nSize) || (!lpBuffer))
                return vLength + 1;
 
        CopyMemory(lpBuffer, env, vLength + 1);
@@ -597,9 +597,9 @@ BOOL SetEnvironmentVariableEBA(LPSTR* envBlock, LPCSTR lpName, LPCSTR lpValue)
 char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock)
 {
        char* p;
-       int index;
-       int count;
-       int length;
+       SSIZE_T index;
+       size_t count;
+       size_t length;
        char** envp = NULL;
 
        count = 0;
@@ -610,7 +610,7 @@ char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock)
 
        while (p[0] && p[1])
        {
-               length = (int) strlen(p);
+               length = strlen(p);
                p += (length + 1);
                count++;
        }
@@ -625,7 +625,7 @@ char** EnvironmentBlockToEnvpA(LPCH lpszEnvironmentBlock)
 
        while (p[0] && p[1])
        {
-               length = (int) strlen(p);
+               length = strlen(p);
                envp[index] = _strdup(p);
                if (!envp[index])
                {
index 6a69e5b..ee61203 100644 (file)
@@ -8,7 +8,7 @@ int TestEnvironmentMergeEnvironmentStrings(int argc, char* argv[])
 {
 #ifndef _WIN32
        TCHAR* p;
-       int length;
+       size_t length;
        LPTCH lpszEnvironmentBlock;
        LPTCH lpsz2Merge = "SHELL=123\0test=1\0test1=2\0DISPLAY=:77\0\0";
        LPTCH lpszMergedEnvironmentBlock;
index 04452e2..776a51f 100644 (file)
@@ -17,7 +17,7 @@ int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
        nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0);
 
        /* check if value returned is len + 1 ) */
-       if (nSize != strlen(TEST_VALUE) + 1)
+       if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)) + 1)
        {
                printf("GetEnvironmentVariableA not found error\n");
                return -1;
@@ -30,7 +30,7 @@ int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
 
        nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize);
 
-       if (nSize != strlen(TEST_VALUE))
+       if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)))
        {
                printf("GetEnvironmentVariableA wrong size returned\n");
                goto fail;
index 651958c..0ff881c 100644 (file)
@@ -266,7 +266,7 @@ char* GetNamedPipeNameWithoutPrefixA(LPCSTR lpName)
        if (!IsNamedPipeFileNameA(lpName))
                return NULL;
 
-       lpFileName = _strdup(&lpName[strlen(NAMED_PIPE_PREFIX_PATH)]);
+       lpFileName = _strdup(&lpName[strnlen(NAMED_PIPE_PREFIX_PATH, sizeof(NAMED_PIPE_PREFIX_PATH))]);
        return lpFileName;
 }
 
index e9da561..0b402b6 100644 (file)
@@ -27,7 +27,7 @@
 int TestFileGetStdHandle(int argc, char* argv[])
 {
        HANDLE so;
-       char *buf = "happy happy";
+       const char buf[] = "happy happy";
        DWORD bytesWritten;
 
        so = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -36,8 +36,8 @@ int TestFileGetStdHandle(int argc, char* argv[])
                fprintf(stderr, "GetStdHandle failed ;(\n");
                return -1;
        }
-       WriteFile(so, buf, strlen(buf), &bytesWritten, FALSE);
-       if (bytesWritten != strlen(buf))
+       WriteFile(so, buf, strnlen(buf, sizeof(buf)), &bytesWritten, FALSE);
+       if (bytesWritten != strnlen(buf, sizeof (buf)))
        {
                fprintf(stderr, "write failed\n");
                return -1;
index c1ce691..5177d51 100644 (file)
@@ -74,7 +74,7 @@ char* GetDeviceFileNameWithoutPrefixA(LPCSTR lpName)
        if (strncmp(lpName, DEVICE_FILE_PREFIX_PATH, sizeof(DEVICE_FILE_PREFIX_PATH) - 1) != 0)
                return NULL;
 
-       lpFileName = _strdup(&lpName[strlen(DEVICE_FILE_PREFIX_PATH)]);
+       lpFileName = _strdup(&lpName[strnlen(DEVICE_FILE_PREFIX_PATH, sizeof (DEVICE_FILE_PREFIX_PATH))]);
        return lpFileName;
 }
 
index 032bdf6..10cc3ae 100644 (file)
@@ -283,7 +283,7 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
                }
 
                buffer[status] = '\0';
-               length = strlen(buffer);
+               length = strnlen(buffer, sizeof(buffer));
 
                if (length < nSize)
                {
@@ -321,7 +321,7 @@ DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
                 * so use realpath to find the absolute, canonical path.
                 */
                realpath(path, buffer);
-               length = strlen(buffer);
+               length = strnlen(buffer, sizeof(buffer));
 
                if (length < nSize)
                {
index 32d5f8f..2583b0c 100644 (file)
@@ -291,10 +291,10 @@ LONG RegQueryValueExA(HKEY hKey, LPCSTR lpValueName,
                        }
                        else if (pValue->type == REG_SZ)
                        {
-                               int length;
+                               size_t length;
                                char* pData = (char*) lpData;
 
-                               length = (int) strlen(pValue->data.string);
+                               length = strnlen(pValue->data.string, UINT32_MAX);
 
                                if (pData != NULL)
                                {
@@ -302,7 +302,7 @@ LONG RegQueryValueExA(HKEY hKey, LPCSTR lpValueName,
                                        pData[length] = '\0';
                                }
 
-                               *lpcbData = length;
+                               *lpcbData = (UINT32)length;
 
                                return ERROR_SUCCESS;
                        }
index 5b91277..a255bfa 100644 (file)
 LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
 {
        char* p;
-       int length;
+       size_t length;
        char* pBeg;
        char* pEnd;
        char* buffer;
        char* pOutput;
        int numArgs = 0;
        LPSTR* pArgs;
-       int maxNumArgs;
-       int maxBufferSize;
-       int cmdLineLength;
+       size_t maxNumArgs;
+       size_t maxBufferSize;
+       size_t cmdLineLength;
        BOOL* lpEscapedChars;
        LPSTR lpEscapedCmdLine;
 
@@ -112,7 +112,7 @@ LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
 
        pArgs = NULL;
        lpEscapedCmdLine = NULL;
-       cmdLineLength = (int) strlen(lpCmdLine);
+       cmdLineLength = strlen(lpCmdLine);
        lpEscapedChars = (BOOL*) calloc(cmdLineLength + 1, sizeof(BOOL));
 
        if (!lpEscapedChars)
@@ -120,7 +120,8 @@ LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
 
        if (strstr(lpCmdLine, "\\\""))
        {
-               int i, n;
+               size_t i;
+               size_t n;
                char* pLastEnd = NULL;
                lpEscapedCmdLine = (char*) calloc(cmdLineLength + 1, sizeof(char));
 
@@ -140,7 +141,7 @@ LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
 
                        if (!pBeg)
                        {
-                               length = (int) strlen(p);
+                               length = strlen(p);
                                CopyMemory(pOutput, p, length);
                                pOutput += length;
                                break;
@@ -159,8 +160,8 @@ LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
                                pBeg--;
                        }
 
-                       n = (int)((pEnd - pBeg) - 1);
-                       length = (int)(pBeg - pLastEnd);
+                       n = ((pEnd - pBeg) - 1);
+                       length = (pBeg - pLastEnd);
                        CopyMemory(pOutput, p, length);
                        pOutput += length;
                        p += length;
@@ -179,7 +180,7 @@ LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
 
                *pOutput++ = '\0';
                lpCmdLine = (LPCSTR) lpEscapedCmdLine;
-               cmdLineLength = (int) strlen(lpCmdLine);
+               cmdLineLength = strlen(lpCmdLine);
        }
 
        maxNumArgs = 2;
@@ -223,7 +224,7 @@ LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs)
                if (*p != '"')
                {
                        /* no whitespace escaped with double quotes */
-                       length = (int)(p - pBeg);
+                       length = (p - pBeg);
                        CopyMemory(pOutput, pBeg, length);
                        pOutput[length] = '\0';
                        pArgs[numArgs++] = pOutput;
index 8908e46..17562bb 100644 (file)
@@ -107,10 +107,10 @@ static BOOL WLog_BinaryAppender_WriteMessage(wLog* log, wLogAppender* appender,
 {
        FILE* fp;
        wStream* s;
-       int MessageLength;
-       int FileNameLength;
-       int FunctionNameLength;
-       int TextStringLength;
+       size_t MessageLength;
+       size_t FileNameLength;
+       size_t FunctionNameLength;
+       size_t TextStringLength;
        BOOL ret = TRUE;
        wLogBinaryAppender* binaryAppender;
 
@@ -124,33 +124,36 @@ static BOOL WLog_BinaryAppender_WriteMessage(wLog* log, wLogAppender* appender,
        if (!fp)
                return FALSE;
 
-       FileNameLength = (int) strlen(message->FileName);
-       FunctionNameLength = (int) strlen(message->FunctionName);
-       TextStringLength = (int) strlen(message->TextString);
+       FileNameLength = strlen(message->FileName);
+       FunctionNameLength = strlen(message->FunctionName);
+       TextStringLength = strlen(message->TextString);
 
        MessageLength = 16 +
                        (4 + FileNameLength + 1) +
                        (4 + FunctionNameLength + 1) +
                        (4 + TextStringLength + 1);
 
+       if ((MessageLength > UINT32_MAX) || (FileNameLength > UINT32_MAX) || (FunctionNameLength > UINT32_MAX) || (TextStringLength > UINT32_MAX))
+               return FALSE;
+
        s = Stream_New(NULL, MessageLength);
        if (!s)
                return FALSE;
 
-       Stream_Write_UINT32(s, MessageLength);
+       Stream_Write_UINT32(s, (UINT32)MessageLength);
 
        Stream_Write_UINT32(s, message->Type);
        Stream_Write_UINT32(s, message->Level);
 
        Stream_Write_UINT32(s, message->LineNumber);
 
-       Stream_Write_UINT32(s, FileNameLength);
+       Stream_Write_UINT32(s, (UINT32)FileNameLength);
        Stream_Write(s, message->FileName, FileNameLength + 1);
 
-       Stream_Write_UINT32(s, FunctionNameLength);
+       Stream_Write_UINT32(s, (UINT32)FunctionNameLength);
        Stream_Write(s, message->FunctionName, FunctionNameLength + 1);
 
-       Stream_Write_UINT32(s, TextStringLength);
+       Stream_Write_UINT32(s, (UINT32)TextStringLength);
        Stream_Write(s, message->TextString, TextStringLength + 1);
 
        Stream_SealLength(s);