Alistair Leslie-Hughes : dpnet: Add check for mismatched string lengths.

Alexandre Julliard julliard at wine.codeweavers.com
Fri Mar 6 06:27:28 CST 2015


Module: wine
Branch: master
Commit: a5d3125395bd92b1010b4df585327d4043d86651
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=a5d3125395bd92b1010b4df585327d4043d86651

Author: Alistair Leslie-Hughes <leslie_alistair at hotmail.com>
Date:   Tue Mar  3 08:38:23 2015 +1100

dpnet: Add check for mismatched string lengths.

---

 dlls/dpnet/address.c       | 40 +++++++++++++++++++++++++++++++++-------
 dlls/dpnet/tests/address.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/dlls/dpnet/address.c b/dlls/dpnet/address.c
index 2062bc4..5eea0cb 100644
--- a/dlls/dpnet/address.c
+++ b/dlls/dpnet/address.c
@@ -382,11 +382,43 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_AddComponent(IDirectPlay8Address *
     struct component *entry;
     BOOL found = FALSE;
 
-    TRACE("(%p, %s, %p, %u, %x): stub\n", This, debugstr_w(pwszName), lpvData, dwDataSize, dwDataType);
+    TRACE("(%p, %s, %p, %u, %x)\n", This, debugstr_w(pwszName), lpvData, dwDataSize, dwDataType);
 
     if (NULL == lpvData)
         return DPNERR_INVALIDPOINTER;
 
+    switch (dwDataType)
+    {
+        case DPNA_DATATYPE_DWORD:
+            if (sizeof(DWORD) != dwDataSize)
+            {
+                WARN("Invalid DWORD size, returning DPNERR_INVALIDPARAM\n");
+                return DPNERR_INVALIDPARAM;
+            }
+            break;
+        case DPNA_DATATYPE_GUID:
+            if (sizeof(GUID) != dwDataSize)
+            {
+                WARN("Invalid GUID size, returning DPNERR_INVALIDPARAM\n");
+                return DPNERR_INVALIDPARAM;
+            }
+            break;
+        case DPNA_DATATYPE_STRING:
+            if (((strlenW((WCHAR*)lpvData)+1)*sizeof(WCHAR)) != dwDataSize)
+            {
+                WARN("Invalid STRING size, returning DPNERR_INVALIDPARAM\n");
+                return DPNERR_INVALIDPARAM;
+            }
+            break;
+        case DPNA_DATATYPE_STRING_ANSI:
+            if ((strlen((const CHAR*)lpvData)+1) != dwDataSize)
+            {
+                WARN("Invalid ASCII size, returning DPNERR_INVALIDPARAM\n");
+                return DPNERR_INVALIDPARAM;
+            }
+            break;
+    }
+
     LIST_FOR_EACH_ENTRY(entry, &This->components, struct component, entry)
     {
         if (lstrcmpW(pwszName, entry->name) == 0)
@@ -411,16 +443,10 @@ static HRESULT WINAPI IDirectPlay8AddressImpl_AddComponent(IDirectPlay8Address *
     switch (dwDataType)
     {
         case DPNA_DATATYPE_DWORD:
-            if (sizeof(DWORD) != dwDataSize)
-                return DPNERR_INVALIDPARAM;
-
             entry->data.value = *(DWORD*)lpvData;
             TRACE("(%p, %u): DWORD Type -> %u\n", lpvData, dwDataSize, *(const DWORD*) lpvData);
             break;
         case DPNA_DATATYPE_GUID:
-            if (sizeof(GUID) != dwDataSize)
-                return DPNERR_INVALIDPARAM;
-
             entry->data.guid = *(GUID*)lpvData;
             TRACE("(%p, %u): GUID Type -> %s\n", lpvData, dwDataSize, debugstr_guid(lpvData));
             break;
diff --git a/dlls/dpnet/tests/address.c b/dlls/dpnet/tests/address.c
index bad4fa4..53633c2 100644
--- a/dlls/dpnet/tests/address.c
+++ b/dlls/dpnet/tests/address.c
@@ -71,6 +71,7 @@ static void address_addcomponents(void)
 {
     static const WCHAR UNKNOWN[] = { 'u','n','k','n','o','w','n',0 };
     static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
+    static const char testing[] = "testing";
     HRESULT hr;
     IDirectPlay8Address *localaddr = NULL;
 
@@ -85,6 +86,7 @@ static void address_addcomponents(void)
         DWORD namelen = 0;
         DWORD bufflen = 0;
         DWORD port = 8888;
+        WCHAR buffer[256];
 
         /* We can add any Component to the Address interface not just the predefined ones. */
         hr = IDirectPlay8Address_AddComponent(localaddr, UNKNOWN, &IID_Random, sizeof(GUID), DPNA_DATATYPE_GUID);
@@ -93,9 +95,39 @@ static void address_addcomponents(void)
         hr = IDirectPlay8Address_AddComponent(localaddr, UNKNOWN, &IID_Random, sizeof(GUID)+1, DPNA_DATATYPE_GUID);
         ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
 
+        hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_HOSTNAME, &localhost, sizeof(localhost)+2, DPNA_DATATYPE_STRING);
+        ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
+
+        hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_HOSTNAME, &localhost, sizeof(localhost)/2, DPNA_DATATYPE_STRING);
+        ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
+
+        hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_HOSTNAME, testing, sizeof(testing)+2, DPNA_DATATYPE_STRING_ANSI);
+        ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
+
+        /* Show that on error, nothing is added. */
+        size = sizeof(buffer);
+        hr = IDirectPlay8Address_GetComponentByName(localaddr, DPNA_KEY_HOSTNAME, buffer, &size, &type);
+        ok(hr == DPNERR_DOESNOTEXIST, "got 0x%08x\n", hr);
+
+        hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_HOSTNAME, testing, sizeof(testing), DPNA_DATATYPE_STRING_ANSI);
+        ok(hr == S_OK, "got 0x%08x\n", hr);
+
+        hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_PORT, &port, sizeof(DWORD)+2, DPNA_DATATYPE_DWORD);
+        ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
+
         hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_HOSTNAME, &localhost, sizeof(localhost), DPNA_DATATYPE_STRING);
         ok(hr == S_OK, "got 0x%08x\n", hr);
 
+        /* The information doesn't get removed when invalid parameters are used.*/
+        hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_HOSTNAME, &localhost, sizeof(localhost)+2, DPNA_DATATYPE_STRING);
+        ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
+
+        size = sizeof(buffer);
+        hr = IDirectPlay8Address_GetComponentByName(localaddr, DPNA_KEY_HOSTNAME, buffer, &size, &type);
+        ok(hr == S_OK, "got 0x%08x\n", hr);
+        todo_wine ok(type == DPNA_DATATYPE_STRING, "incorrect type %d\n", type);
+        todo_wine ok(!lstrcmpW(buffer, localhost), "Invalid string: %s\n", wine_dbgstr_w(buffer));
+
         hr = IDirectPlay8Address_AddComponent(localaddr, DPNA_KEY_PORT, &port, sizeof(DWORD)+2, DPNA_DATATYPE_DWORD);
         ok(hr == DPNERR_INVALIDPARAM, "got 0x%08x\n", hr);
 




More information about the wine-cvs mailing list