From: Bruno Jesus <00cpxxx@gmail.com> Subject: ws2_32: Cope with invalid protocols in WSAEnumProtocols Message-Id: Date: Tue, 3 Sep 2013 00:31:17 -0300 Fix bug http://bugs.winehq.org/show_bug.cgi?id=34412 diff --git a/dlls/ws2_32/protocol.c b/dlls/ws2_32/protocol.c index c039e7c..4f076f2 100644 --- a/dlls/ws2_32/protocol.c +++ b/dlls/ws2_32/protocol.c @@ -67,6 +67,10 @@ static const GUID ProviderIdIPX = { 0x11058240, 0xbe47, 0x11cf, static const GUID ProviderIdSPX = { 0x11058241, 0xbe47, 0x11cf, { 0x95, 0xc8, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92 } }; + +/* List of valid protocols returned by WSAEnumProtocols */ +static INT local[] = { WS_IPPROTO_TCP, WS_IPPROTO_UDP, NSPROTO_IPX, NSPROTO_SPX, NSPROTO_SPXII, 0 }; + /***************************************************************************** * WINSOCK_EnterSingleProtocolW [internal] * @@ -204,11 +208,19 @@ static INT WINSOCK_EnterSingleProtocolA( INT protocol, WSAPROTOCOL_INFOA* info ) return ret; } +static BOOL WINSOCK_ValidProtocol( INT protocol ) +{ + INT i; + for (i = 0; local[i]; i++) + if (local[i] == protocol) + return TRUE; + return FALSE; +} + static INT WINSOCK_EnumProtocols( BOOL unicode, LPINT protocols, LPWSAPROTOCOL_INFOW buffer, LPDWORD len ) { - INT i = 0; + INT items = 0, i = 0; DWORD size = 0; - INT local[] = { WS_IPPROTO_TCP, WS_IPPROTO_UDP, NSPROTO_IPX, NSPROTO_SPX, NSPROTO_SPXII, 0 }; union _info { LPWSAPROTOCOL_INFOA a; @@ -218,9 +230,10 @@ static INT WINSOCK_EnumProtocols( BOOL unicode, LPINT protocols, LPWSAPROTOCOL_I if (!protocols) protocols = local; - while (protocols[i]) i++; + while(protocols[i]) + if(WINSOCK_ValidProtocol(protocols[i++])) items++; - size = i * (unicode ? sizeof(WSAPROTOCOL_INFOW) : sizeof(WSAPROTOCOL_INFOA)); + size = items * (unicode ? sizeof(WSAPROTOCOL_INFOW) : sizeof(WSAPROTOCOL_INFOA)); if (*len < size || !buffer) { @@ -229,20 +242,20 @@ static INT WINSOCK_EnumProtocols( BOOL unicode, LPINT protocols, LPWSAPROTOCOL_I return SOCKET_ERROR; } - for (i = 0; protocols[i]; i++) + for (i = items = 0; protocols[i]; i++) { if (unicode) { - if (WINSOCK_EnterSingleProtocolW( protocols[i], &info.w[i] ) == SOCKET_ERROR) - break; + if (WINSOCK_EnterSingleProtocolW( protocols[i], &info.w[items] ) != SOCKET_ERROR) + items++; } else { - if (WINSOCK_EnterSingleProtocolA( protocols[i], &info.a[i] ) == SOCKET_ERROR) - break; + if (WINSOCK_EnterSingleProtocolA( protocols[i], &info.a[items] ) != SOCKET_ERROR) + items++; } } - return i; + return items; } /***************************************************************************** diff --git a/dlls/ws2_32/tests/protocol.c b/dlls/ws2_32/tests/protocol.c index 2b95381..1b9a429 100644 --- a/dlls/ws2_32/tests/protocol.c +++ b/dlls/ws2_32/tests/protocol.c @@ -60,9 +60,10 @@ static void test_service_flags(int family, int version, int socktype, int protoc static void test_WSAEnumProtocolsA(void) { - INT ret; + INT ret, i, j, found; DWORD len = 0, error; WSAPROTOCOL_INFOA info, *buffer; + INT ptest[] = {0xdead, IPPROTO_TCP, 0xcafe, IPPROTO_UDP, 0xbeef, 0}; ret = WSAEnumProtocolsA( NULL, NULL, &len ); ok( ret == SOCKET_ERROR, "WSAEnumProtocolsA() succeeded unexpectedly\n"); @@ -80,8 +81,6 @@ static void test_WSAEnumProtocolsA(void) if (buffer) { - INT i; - ret = WSAEnumProtocolsA( NULL, buffer, &len ); ok( ret != SOCKET_ERROR, "WSAEnumProtocolsA() failed unexpectedly: %d\n", WSAGetLastError() ); @@ -96,13 +95,41 @@ static void test_WSAEnumProtocolsA(void) HeapFree( GetProcessHeap(), 0, buffer ); } + + /* Test invalid protocols in the list */ + ret = WSAEnumProtocolsA( ptest, NULL, &len ); + ok( ret == SOCKET_ERROR, "WSAEnumProtocolsA() succeeded unexpectedly\n"); + error = WSAGetLastError(); + ok( error == WSAENOBUFS, "Expected 10055, received %d\n", error); + + buffer = HeapAlloc( GetProcessHeap(), 0, len ); + + if (buffer) + { + ret = WSAEnumProtocolsA( ptest, buffer, &len ); + ok( ret != SOCKET_ERROR, "WSAEnumProtocolsA() failed unexpectedly: %d\n", + WSAGetLastError() ); + ok( ret >= 2, "Expected at least 2 items, received %d\n", ret); + + for (i = found = 0; i < ret; i++) + for (j = 0; j < sizeof(ptest) / sizeof(ptest[0]); j++) + if (buffer[i].iProtocol == ptest[j]) + { + found |= 1 << j; + break; + } + ok(found == 0x0A, "Expected 2 bits represented as 0xA, received 0x%x\n", found); + + HeapFree( GetProcessHeap(), 0, buffer ); + } } static void test_WSAEnumProtocolsW(void) { - INT ret; + INT ret, i, j, found; DWORD len = 0, error; WSAPROTOCOL_INFOW info, *buffer; + INT ptest[] = {0xdead, IPPROTO_TCP, 0xcafe, IPPROTO_UDP, 0xbeef, 0}; ret = WSAEnumProtocolsW( NULL, NULL, &len ); ok( ret == SOCKET_ERROR, "WSAEnumProtocolsW() succeeded unexpectedly\n"); @@ -120,8 +147,6 @@ static void test_WSAEnumProtocolsW(void) if (buffer) { - INT i; - ret = WSAEnumProtocolsW( NULL, buffer, &len ); ok( ret != SOCKET_ERROR, "WSAEnumProtocolsW() failed unexpectedly: %d\n", WSAGetLastError() ); @@ -136,6 +161,33 @@ static void test_WSAEnumProtocolsW(void) HeapFree( GetProcessHeap(), 0, buffer ); } + + /* Test invalid protocols in the list */ + ret = WSAEnumProtocolsW( ptest, NULL, &len ); + ok( ret == SOCKET_ERROR, "WSAEnumProtocolsW() succeeded unexpectedly\n"); + error = WSAGetLastError(); + ok( error == WSAENOBUFS, "Expected 10055, received %d\n", error); + + buffer = HeapAlloc( GetProcessHeap(), 0, len ); + + if (buffer) + { + ret = WSAEnumProtocolsW( ptest, buffer, &len ); + ok( ret != SOCKET_ERROR, "WSAEnumProtocolsW() failed unexpectedly: %d\n", + WSAGetLastError() ); + ok( ret >= 2, "Expected at least 2 items, received %d\n", ret); + + for (i = found = 0; i < ret; i++) + for (j = 0; j < sizeof(ptest) / sizeof(ptest[0]); j++) + if (buffer[i].iProtocol == ptest[j]) + { + found |= 1 << j; + break; + } + ok(found == 0x0A, "Expected 2 bits represented as 0xA, received 0x%x\n", found); + + HeapFree( GetProcessHeap(), 0, buffer ); + } } START_TEST( protocol )