Commit 516b21f4 authored by Nikolay Sivov's avatar Nikolay Sivov Committed by Alexandre Julliard

user32: Don't send WM_GETTEXT from GetWindowText() with invalid buffer length.

parent ee72b0fd
...@@ -722,7 +722,6 @@ static struct wm_gettext_override_data ...@@ -722,7 +722,6 @@ static struct wm_gettext_override_data
BOOL enabled; /* when 1 bypasses default procedure */ BOOL enabled; /* when 1 bypasses default procedure */
char *buff; /* expected text buffer pointer */ char *buff; /* expected text buffer pointer */
WCHAR *buffW; /* same, for W test */ WCHAR *buffW; /* same, for W test */
int len;
} g_wm_gettext_override; } g_wm_gettext_override;
static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
...@@ -810,8 +809,7 @@ static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPAR ...@@ -810,8 +809,7 @@ static LRESULT WINAPI main_window_procA(HWND hwnd, UINT msg, WPARAM wparam, LPAR
{ {
char *text = (char*)lparam; char *text = (char*)lparam;
ok(g_wm_gettext_override.buff == text, "expected buffer %p, got %p\n", g_wm_gettext_override.buff, text); ok(g_wm_gettext_override.buff == text, "expected buffer %p, got %p\n", g_wm_gettext_override.buff, text);
if (g_wm_gettext_override.len) ok(*text == 0, "expected empty string buffer %x\n", *text);
ok(*text == 0, "expected empty string buffer %x\n", *text);
return 0; return 0;
} }
break; break;
...@@ -833,8 +831,7 @@ static LRESULT WINAPI main_window_procW(HWND hwnd, UINT msg, WPARAM wparam, LPAR ...@@ -833,8 +831,7 @@ static LRESULT WINAPI main_window_procW(HWND hwnd, UINT msg, WPARAM wparam, LPAR
{ {
WCHAR *text = (WCHAR*)lparam; WCHAR *text = (WCHAR*)lparam;
ok(g_wm_gettext_override.buffW == text, "expected buffer %p, got %p\n", g_wm_gettext_override.buffW, text); ok(g_wm_gettext_override.buffW == text, "expected buffer %p, got %p\n", g_wm_gettext_override.buffW, text);
if (g_wm_gettext_override.len) ok(*text == 0, "expected empty string buffer %x\n", *text);
ok(*text == 0, "expected empty string buffer %x\n", *text);
return 0; return 0;
} }
break; break;
...@@ -5819,7 +5816,6 @@ static void test_gettext(void) ...@@ -5819,7 +5816,6 @@ static void test_gettext(void)
num_gettext_msgs = 0; num_gettext_msgs = 0;
memset( buf, 0xcc, sizeof(buf) ); memset( buf, 0xcc, sizeof(buf) );
g_wm_gettext_override.buff = buf; g_wm_gettext_override.buff = buf;
g_wm_gettext_override.len = sizeof(buf);
buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) ); buf_len = GetWindowTextA( hwnd, buf, sizeof(buf) );
ok( buf_len == 0, "got %d\n", buf_len ); ok( buf_len == 0, "got %d\n", buf_len );
ok( *buf == 0, "got %x\n", *buf ); ok( *buf == 0, "got %x\n", *buf );
...@@ -5828,11 +5824,9 @@ static void test_gettext(void) ...@@ -5828,11 +5824,9 @@ static void test_gettext(void)
num_gettext_msgs = 0; num_gettext_msgs = 0;
strcpy( buf, "blah" ); strcpy( buf, "blah" );
g_wm_gettext_override.buff = buf; g_wm_gettext_override.buff = buf;
g_wm_gettext_override.len = 0;
buf_len = GetWindowTextA( hwnd, buf, 0 ); buf_len = GetWindowTextA( hwnd, buf, 0 );
ok( buf_len == 0, "got %d\n", buf_len ); ok( buf_len == 0, "got %d\n", buf_len );
ok( !strcmp(buf, "blah"), "got %s\n", buf ); ok( !strcmp(buf, "blah"), "got %s\n", buf );
todo_wine
ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs ); ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
g_wm_gettext_override.enabled = FALSE; g_wm_gettext_override.enabled = FALSE;
...@@ -5846,7 +5840,6 @@ todo_wine ...@@ -5846,7 +5840,6 @@ todo_wine
num_gettext_msgs = 0; num_gettext_msgs = 0;
memset( bufW, 0xcc, sizeof(bufW) ); memset( bufW, 0xcc, sizeof(bufW) );
g_wm_gettext_override.buffW = bufW; g_wm_gettext_override.buffW = bufW;
g_wm_gettext_override.len = sizeof(bufW)/sizeof(WCHAR);
buf_len = GetWindowTextW( hwnd2, bufW, sizeof(bufW)/sizeof(WCHAR) ); buf_len = GetWindowTextW( hwnd2, bufW, sizeof(bufW)/sizeof(WCHAR) );
ok( buf_len == 0, "got %d\n", buf_len ); ok( buf_len == 0, "got %d\n", buf_len );
ok( *bufW == 0, "got %x\n", *bufW ); ok( *bufW == 0, "got %x\n", *bufW );
...@@ -5855,11 +5848,9 @@ todo_wine ...@@ -5855,11 +5848,9 @@ todo_wine
num_gettext_msgs = 0; num_gettext_msgs = 0;
memset( bufW, 0xcc, sizeof(bufW) ); memset( bufW, 0xcc, sizeof(bufW) );
g_wm_gettext_override.buffW = bufW; g_wm_gettext_override.buffW = bufW;
g_wm_gettext_override.len = 0;
buf_len = GetWindowTextW( hwnd2, bufW, 0 ); buf_len = GetWindowTextW( hwnd2, bufW, 0 );
ok( buf_len == 0, "got %d\n", buf_len ); ok( buf_len == 0, "got %d\n", buf_len );
ok( *bufW == 0xcccc, "got %x\n", *bufW ); ok( *bufW == 0xcccc, "got %x\n", *bufW );
todo_wine
ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs ); ok( num_gettext_msgs == 0, "got %u WM_GETTEXT messages\n", num_gettext_msgs );
g_wm_gettext_override.enabled = FALSE; g_wm_gettext_override.enabled = FALSE;
......
...@@ -2676,17 +2676,15 @@ INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount ) ...@@ -2676,17 +2676,15 @@ INT WINAPI GetWindowTextA( HWND hwnd, LPSTR lpString, INT nMaxCount )
{ {
WCHAR *buffer; WCHAR *buffer;
if (!lpString) return 0; if (!lpString || nMaxCount <= 0) return 0;
if (WIN_IsCurrentProcess( hwnd )) if (WIN_IsCurrentProcess( hwnd ))
{ {
if (nMaxCount > 0) lpString[0] = 0;
lpString[0] = 0;
return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString ); return (INT)SendMessageA( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
} }
/* when window belongs to other process, don't send a message */ /* when window belongs to other process, don't send a message */
if (nMaxCount <= 0) return 0;
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0; if (!(buffer = HeapAlloc( GetProcessHeap(), 0, nMaxCount * sizeof(WCHAR) ))) return 0;
get_server_window_text( hwnd, buffer, nMaxCount ); get_server_window_text( hwnd, buffer, nMaxCount );
if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL )) if (!WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpString, nMaxCount, NULL, NULL ))
...@@ -2725,17 +2723,15 @@ INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount ) ...@@ -2725,17 +2723,15 @@ INT WINAPI InternalGetWindowText(HWND hwnd,LPWSTR lpString,INT nMaxCount )
*/ */
INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount ) INT WINAPI GetWindowTextW( HWND hwnd, LPWSTR lpString, INT nMaxCount )
{ {
if (!lpString) return 0; if (!lpString || nMaxCount <= 0) return 0;
if (WIN_IsCurrentProcess( hwnd )) if (WIN_IsCurrentProcess( hwnd ))
{ {
if (nMaxCount > 0) lpString[0] = 0;
lpString[0] = 0;
return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString ); return (INT)SendMessageW( hwnd, WM_GETTEXT, nMaxCount, (LPARAM)lpString );
} }
/* when window belongs to other process, don't send a message */ /* when window belongs to other process, don't send a message */
if (nMaxCount <= 0) return 0;
get_server_window_text( hwnd, lpString, nMaxCount ); get_server_window_text( hwnd, lpString, nMaxCount );
return strlenW(lpString); return strlenW(lpString);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment