From: Alistair Leslie-Hughes Subject: [1/3] dpnet: Add check for mismatched string lengths (try 3) Message-Id: <54F678D8.3010309@hotmail.com> Date: Wed, 04 Mar 2015 14:15:36 +1100 Hi, Added test for string/2 Use sizeof. Changelog: dpnet: Add check for mismatched string lengths Best Regards Alistair Leslie-Hughes From 39a3415229f6da4ea1b6e86a7b3d360716456b18 Mon Sep 17 00:00:00 2001 From: Alistair Leslie-Hughes Date: Tue, 3 Mar 2015 08:38:23 +1100 Subject: [PATCH] Add check for mismatched string lengths To: wine-patches --- dlls/dpnet/address.c | 40 +++++++++++++++++++++++++++++++++------- dlls/dpnet/tests/address.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 64 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..b358002 100644 --- a/dlls/dpnet/tests/address.c +++ b/dlls/dpnet/tests/address.c @@ -85,6 +85,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 +94,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 = 8; + 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", 8, 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(localhost); + 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); -- 2.1.0