comctl32: Return the number of characters copied in WM_GETTEXT even if the buffer is too small (try 2)

Juan Lang juan.lang at gmail.com
Wed Aug 26 10:49:28 CDT 2009


Fixes bugs 13411 and 19839.
--Juan
-------------- next part --------------
From ac026222838ede2a752c4a2f917fe1b5c700b9d3 Mon Sep 17 00:00:00 2001
From: Juan Lang <juan.lang at gmail.com>
Date: Wed, 26 Aug 2009 08:50:39 -0700
Subject: [PATCH] Return the number of characters copied in WM_GETTEXT even if the buffer is too small

---
 dlls/comctl32/status.c       |   11 ++++++++---
 dlls/comctl32/tests/status.c |   29 +++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/dlls/comctl32/status.c b/dlls/comctl32/status.c
index 11a977d..d6b627a 100644
--- a/dlls/comctl32/status.c
+++ b/dlls/comctl32/status.c
@@ -1016,12 +1016,17 @@ STATUSBAR_WMGetText (const STATUS_INFO *infoPtr, INT size, LPWSTR buf)
 
     len = strlenW (infoPtr->parts[0].text);
 
-    if (size > len) {
+    if (!size)
+        return len;
+    else if (size > len) {
         strcpyW (buf, infoPtr->parts[0].text);
 	return len;
     }
-
-    return -1;
+    else {
+        memcpy (buf, infoPtr->parts[0].text, (size - 1) * sizeof(WCHAR));
+        buf[size - 1] = 0;
+        return size - 1;
+    }
 }
 
 
diff --git a/dlls/comctl32/tests/status.c b/dlls/comctl32/tests/status.c
index f1cbda4..28df50f 100644
--- a/dlls/comctl32/tests/status.c
+++ b/dlls/comctl32/tests/status.c
@@ -466,6 +466,34 @@ static void test_status_ownerdraw(void)
     SetWindowLongPtr( g_hMainWnd, GWLP_WNDPROC, (LONG_PTR)g_wndproc_saved );
 }
 
+static void test_gettext(void)
+{
+    HWND hwndStatus = CreateWindow(SUBCLASS_NAME, NULL, WS_CHILD|WS_VISIBLE,
+        0, 0, 300, 20, g_hMainWnd, NULL, NULL, NULL);
+    char buf[5];
+    int r;
+
+    r = SendMessage(hwndStatus, SB_SETTEXT, 0, (LPARAM)"Text");
+    expect(TRUE, r);
+    r = SendMessage(hwndStatus, WM_GETTEXTLENGTH, 0, 0);
+    expect(4, r);
+    /* A size of 0 returns the length of the text */
+    r = SendMessage(hwndStatus, WM_GETTEXT, 0, 0);
+    expect(4, r);
+    /* A size of 1 only stores the NULL terminator */
+    buf[0] = 0xa;
+    r = SendMessage(hwndStatus, WM_GETTEXT, 1, (LPARAM)buf);
+    expect(0, r);
+    ok(!buf[0], "expected empty buffer\n");
+    /* A size of 2 returns a length 1 */
+    r = SendMessage(hwndStatus, WM_GETTEXT, 2, (LPARAM)buf);
+    expect(1, r);
+    r = SendMessage(hwndStatus, WM_GETTEXT, sizeof(buf), (LPARAM)buf);
+    expect(4, r);
+    ok(!strcmp(buf, "Text"), "expected Text, got %s\n", buf);
+    DestroyWindow(hwndStatus);
+}
+
 START_TEST(status)
 {
     hinst = GetModuleHandleA(NULL);
@@ -483,4 +511,5 @@ START_TEST(status)
     test_create();
     test_height();
     test_status_ownerdraw();
+    test_gettext();
 }
-- 
1.6.3.2


More information about the wine-patches mailing list