Commit 17be4a36 authored by sbc@chromium.org's avatar sbc@chromium.org

[NaCl SDK] nacl_io: Fix bug in getaddrinfo() where service is "0"

Add a test for this case.

TEST=unit tests
BUG=386220
R=binji@chromium.org, bradnelson@google.com

Review URL: https://codereview.chromium.org/344833005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278543 0039d316-1c4b-4281-b951-d872f2087c98
parent a20aa5a9
...@@ -2,9 +2,14 @@ ...@@ -2,9 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#include "nacl_io/host_resolver.h" #include "nacl_io/host_resolver.h"
#include <assert.h> #include <assert.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -231,7 +236,7 @@ int HostResolver::getaddrinfo(const char* node, ...@@ -231,7 +236,7 @@ int HostResolver::getaddrinfo(const char* node,
if (service != NULL) { if (service != NULL) {
char* cp; char* cp;
port = strtol(service, &cp, 10); port = strtol(service, &cp, 10);
if (port > 0 && port <= 65535 && *cp == '\0') { if (port >= 0 && port <= UINT16_MAX && *cp == '\0') {
port = htons(port); port = htons(port);
} else { } else {
return EAI_SERVICE; return EAI_SERVICE;
......
...@@ -89,7 +89,7 @@ TEST_F(HostResolverTest, Getaddrinfo_Numeric) { ...@@ -89,7 +89,7 @@ TEST_F(HostResolverTest, Getaddrinfo_Numeric) {
struct sockaddr_in* in; struct sockaddr_in* in;
struct addrinfo hints; struct addrinfo hints;
// Numberic only // Numeric only
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
...@@ -107,6 +107,34 @@ TEST_F(HostResolverTest, Getaddrinfo_Numeric) { ...@@ -107,6 +107,34 @@ TEST_F(HostResolverTest, Getaddrinfo_Numeric) {
ki_freeaddrinfo(ai); ki_freeaddrinfo(ai);
} }
TEST_F(HostResolverTest, Getaddrinfo_NumericService) {
struct addrinfo* ai = NULL;
struct sockaddr_in* in;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
ASSERT_EQ(0, ki_getaddrinfo("1.2.3.4", "0", &hints, &ai));
ASSERT_NE(NULL_INFO, ai);
ASSERT_NE(NULL_ADDR, ai->ai_addr);
in = (struct sockaddr_in*)ai->ai_addr;
uint16_t expected_port = htons(0);
ASSERT_EQ(expected_port, in->sin_port);
ASSERT_EQ(NULL_INFO, ai->ai_next);
ki_freeaddrinfo(ai);
ASSERT_EQ(0, ki_getaddrinfo("1.2.3.4", "65000", &hints, &ai));
ASSERT_NE(NULL_INFO, ai);
ASSERT_NE(NULL_ADDR, ai->ai_addr);
in = (struct sockaddr_in*)ai->ai_addr;
expected_port = htons(65000);
ASSERT_EQ(expected_port, in->sin_port);
ASSERT_EQ(NULL_INFO, ai->ai_next);
ki_freeaddrinfo(ai);
}
TEST_F(HostResolverTest, Getaddrinfo_MissingPPAPI) { TEST_F(HostResolverTest, Getaddrinfo_MissingPPAPI) {
// Verify that full lookups fail due to lack of PPAPI interfaces // Verify that full lookups fail due to lack of PPAPI interfaces
struct addrinfo* ai = NULL; struct addrinfo* ai = NULL;
......
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