hw/xwin: Don't allocate one wchar_t too much for unicode text placed on the Windows clipboard
The count of wchar_t returned by MultiByteToWideChar() includes the terminating null character, so don't add one to it. Also, reduce the scope of various length variables Signed-off-by: Colin Harrison <colin.harrison@virgin.net> Reviewed-by: Jon TURNEY <jon.turney@dronecode.org.uk>
This commit is contained in:
		
							parent
							
								
									d172cd630d
								
							
						
					
					
						commit
						5920433c3a
					
				| 
						 | 
					@ -44,6 +44,7 @@
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <limits.h>
 | 
					#include <limits.h>
 | 
				
			||||||
 | 
					#include <wchar.h>
 | 
				
			||||||
#include <X11/Xutil.h>
 | 
					#include <X11/Xutil.h>
 | 
				
			||||||
#include <X11/Xatom.h>
 | 
					#include <X11/Xatom.h>
 | 
				
			||||||
#include <X11/extensions/Xfixes.h>
 | 
					#include <X11/extensions/Xfixes.h>
 | 
				
			||||||
| 
						 | 
					@ -208,14 +209,11 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
        int iReturn;
 | 
					        int iReturn;
 | 
				
			||||||
        HGLOBAL hGlobal = NULL;
 | 
					        HGLOBAL hGlobal = NULL;
 | 
				
			||||||
        XICCEncodingStyle xiccesStyle;
 | 
					        XICCEncodingStyle xiccesStyle;
 | 
				
			||||||
        int iConvertDataLen = 0;
 | 
					 | 
				
			||||||
        char *pszConvertData = NULL;
 | 
					        char *pszConvertData = NULL;
 | 
				
			||||||
        char *pszTextList[2] = { NULL };
 | 
					        char *pszTextList[2] = { NULL };
 | 
				
			||||||
        int iCount;
 | 
					        int iCount;
 | 
				
			||||||
        char **ppszTextList = NULL;
 | 
					        char **ppszTextList = NULL;
 | 
				
			||||||
        wchar_t *pwszUnicodeStr = NULL;
 | 
					        wchar_t *pwszUnicodeStr = NULL;
 | 
				
			||||||
        int iUnicodeLen = 0;
 | 
					 | 
				
			||||||
        int iReturnDataLen = 0;
 | 
					 | 
				
			||||||
        Bool fAbort = FALSE;
 | 
					        Bool fAbort = FALSE;
 | 
				
			||||||
        Bool fCloseClipboard = FALSE;
 | 
					        Bool fCloseClipboard = FALSE;
 | 
				
			||||||
        Bool fSetClipboardData = TRUE;
 | 
					        Bool fSetClipboardData = TRUE;
 | 
				
			||||||
| 
						 | 
					@ -381,7 +379,7 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* Convert the Unicode string to UTF8 (MBCS) */
 | 
					            /* Convert the Unicode string to UTF8 (MBCS) */
 | 
				
			||||||
            if (data->fUseUnicode) {
 | 
					            if (data->fUseUnicode) {
 | 
				
			||||||
                iConvertDataLen = WideCharToMultiByte(CP_UTF8,
 | 
					                int iConvertDataLen = WideCharToMultiByte(CP_UTF8,
 | 
				
			||||||
                                                      0,
 | 
					                                                      0,
 | 
				
			||||||
                                                      (LPCWSTR) pszGlobalData,
 | 
					                                                      (LPCWSTR) pszGlobalData,
 | 
				
			||||||
                                                      -1, NULL, 0, NULL, NULL);
 | 
					                                                      -1, NULL, 0, NULL, NULL);
 | 
				
			||||||
| 
						 | 
					@ -396,7 +394,6 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else {
 | 
					            else {
 | 
				
			||||||
                pszConvertData = strdup(pszGlobalData);
 | 
					                pszConvertData = strdup(pszGlobalData);
 | 
				
			||||||
                iConvertDataLen = strlen(pszConvertData) + 1;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* Convert DOS string to UNIX string */
 | 
					            /* Convert DOS string to UNIX string */
 | 
				
			||||||
| 
						 | 
					@ -541,7 +538,6 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
             */
 | 
					             */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        case SelectionNotify:
 | 
					        case SelectionNotify:
 | 
				
			||||||
 | 
					 | 
				
			||||||
            winDebug("winClipboardFlushXEvents - SelectionNotify\n");
 | 
					            winDebug("winClipboardFlushXEvents - SelectionNotify\n");
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                char *pszAtomName;
 | 
					                char *pszAtomName;
 | 
				
			||||||
| 
						 | 
					@ -620,8 +616,7 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
                /* Conversion succeeded or some unconvertible characters */
 | 
					                /* Conversion succeeded or some unconvertible characters */
 | 
				
			||||||
                if (ppszTextList != NULL) {
 | 
					                if (ppszTextList != NULL) {
 | 
				
			||||||
                    int i;
 | 
					                    int i;
 | 
				
			||||||
 | 
					                    int iReturnDataLen = 0;
 | 
				
			||||||
                    iReturnDataLen = 0;
 | 
					 | 
				
			||||||
                    for (i = 0; i < iCount; i++) {
 | 
					                    for (i = 0; i < iCount; i++) {
 | 
				
			||||||
                        iReturnDataLen += strlen(ppszTextList[i]);
 | 
					                        iReturnDataLen += strlen(ppszTextList[i]);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
| 
						 | 
					@ -672,12 +667,12 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (data->fUseUnicode) {
 | 
					            if (data->fUseUnicode) {
 | 
				
			||||||
                /* Find out how much space needed to convert MBCS to Unicode */
 | 
					                /* Find out how much space needed to convert MBCS to Unicode */
 | 
				
			||||||
                iUnicodeLen = MultiByteToWideChar(CP_UTF8,
 | 
					                int iUnicodeLen = MultiByteToWideChar(CP_UTF8,
 | 
				
			||||||
                                                  0,
 | 
					                                                  0,
 | 
				
			||||||
                                                  pszReturnData, -1, NULL, 0);
 | 
					                                                  pszReturnData, -1, NULL, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                /* Allocate memory for the Unicode string */
 | 
					                /* NOTE: iUnicodeLen includes space for null terminator */
 | 
				
			||||||
                pwszUnicodeStr = malloc(sizeof(wchar_t) * (iUnicodeLen + 1));
 | 
					                pwszUnicodeStr = malloc(sizeof(wchar_t) * iUnicodeLen);
 | 
				
			||||||
                if (!pwszUnicodeStr) {
 | 
					                if (!pwszUnicodeStr) {
 | 
				
			||||||
                    ErrorF("winClipboardFlushXEvents - SelectionNotify "
 | 
					                    ErrorF("winClipboardFlushXEvents - SelectionNotify "
 | 
				
			||||||
                           "malloc failed for pwszUnicodeStr, aborting.\n");
 | 
					                           "malloc failed for pwszUnicodeStr, aborting.\n");
 | 
				
			||||||
| 
						 | 
					@ -695,9 +690,10 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                /* Allocate global memory for the X clipboard data */
 | 
					                /* Allocate global memory for the X clipboard data */
 | 
				
			||||||
                hGlobal = GlobalAlloc(GMEM_MOVEABLE,
 | 
					                hGlobal = GlobalAlloc(GMEM_MOVEABLE,
 | 
				
			||||||
                                      sizeof(wchar_t) * (iUnicodeLen + 1));
 | 
					                                      sizeof(wchar_t) * iUnicodeLen);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else {
 | 
					            else {
 | 
				
			||||||
 | 
					                int iConvertDataLen = 0;
 | 
				
			||||||
                pszConvertData = strdup(pszReturnData);
 | 
					                pszConvertData = strdup(pszReturnData);
 | 
				
			||||||
                iConvertDataLen = strlen(pszConvertData) + 1;
 | 
					                iConvertDataLen = strlen(pszConvertData) + 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -730,8 +726,7 @@ winClipboardFlushXEvents(HWND hwnd,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* Copy the returned string into the global memory */
 | 
					            /* Copy the returned string into the global memory */
 | 
				
			||||||
            if (data->fUseUnicode) {
 | 
					            if (data->fUseUnicode) {
 | 
				
			||||||
                memcpy(pszGlobalData,
 | 
					                wcscpy((wchar_t *)pszGlobalData, pwszUnicodeStr);
 | 
				
			||||||
                       pwszUnicodeStr, sizeof(wchar_t) * (iUnicodeLen + 1));
 | 
					 | 
				
			||||||
                free(pwszUnicodeStr);
 | 
					                free(pwszUnicodeStr);
 | 
				
			||||||
                pwszUnicodeStr = NULL;
 | 
					                pwszUnicodeStr = NULL;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue