Skip to content

Commit

Permalink
fix memory corruption in clipboard copying
Browse files Browse the repository at this point in the history
was copying WCHAR size instead of char (wtf)
  • Loading branch information
Gargaj committed Mar 25, 2019
1 parent 7269a05 commit 6986b8e
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/platform_w32_common/Clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ namespace Clipboard
void Copy( const char * data, int len )
{
HWND hWnd = GetForegroundWindow();
if (!::OpenClipboard( hWnd ))
if ( !::OpenClipboard( hWnd ) )
return;

EmptyClipboard();

HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE,(len + 1) * sizeof(WCHAR));
WCHAR * pMem = (WCHAR*)GlobalLock(h);
HGLOBAL h = GlobalAlloc( GMEM_MOVEABLE, ( len + 1 ) * sizeof( char ) );
WCHAR * pMem = (WCHAR*) GlobalLock( h );

ZeroMemory( pMem, (len + 1) * sizeof(WCHAR) );
CopyMemory( pMem, data, (len + 1) * sizeof(WCHAR) );
ZeroMemory( pMem, ( len + 1 ) * sizeof( char ) );
CopyMemory( pMem, data, ( len + 1 ) * sizeof( char ) );

GlobalUnlock(h);
SetClipboardData(CF_TEXT,h);
GlobalUnlock( h );
SetClipboardData( CF_TEXT, h );

CloseClipboard();
}

int GetContentsLength()
{
HWND hWnd = GetForegroundWindow();
if (!::OpenClipboard( hWnd ))
if ( !::OpenClipboard( hWnd ) )
return 0;

HANDLE hData = GetClipboardData(CF_TEXT);
const char * buffer = (const char*)GlobalLock( hData );
int n = strlen(buffer);
HANDLE hData = GetClipboardData( CF_TEXT );
const char * buffer = (const char*) GlobalLock( hData );
int n = strlen( buffer );

CloseClipboard();

Expand All @@ -40,11 +40,11 @@ namespace Clipboard
void GetContents( char * data, int len )
{
HWND hWnd = GetForegroundWindow();
if (!::OpenClipboard( hWnd ))
if ( !::OpenClipboard( hWnd ) )
return;

HANDLE hData = GetClipboardData(CF_TEXT);
const char * buffer = (const char*)GlobalLock( hData );
HANDLE hData = GetClipboardData( CF_TEXT );
const char * buffer = (const char*) GlobalLock( hData );
strncpy( data, buffer, len );

CloseClipboard();
Expand Down

0 comments on commit 6986b8e

Please sign in to comment.