Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-30711: getaddrinfo invalid port number #2436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-30711: Fix port number conversion in getaddrinfo emulation
Return an error code if servname is an invalid numeric service.
  • Loading branch information
smejkar committed Jun 27, 2017
commit 7c52c85c4c6801fe43bf2f40ba06922a06068285
23 changes: 8 additions & 15 deletions 23 Modules/getaddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ static int get_name(const char *, struct gai_afd *,
int);
static int get_addr(const char *, int, struct addrinfo **,
struct addrinfo *, int);
static int str_isnumber(const char *);

static const char * const ai_errlist[] = {
"success.",
Expand Down Expand Up @@ -220,18 +219,6 @@ freeaddrinfo(struct addrinfo *ai)
} while ((ai = next) != NULL);
}

static int
str_isnumber(const char *p)
{
unsigned char *q = (unsigned char *)p;
while (*q) {
if (! isdigit(*q))
return NO;
q++;
}
return YES;
}

int
getaddrinfo(const char*hostname, const char*servname,
const struct addrinfo *hints, struct addrinfo **res)
Expand Down Expand Up @@ -333,13 +320,19 @@ getaddrinfo(const char*hostname, const char*servname,
* service port
*/
if (servname) {
if (str_isnumber(servname)) {
long servnum;
char *end;

servnum = strtol(servname, &end, 10);
if (*end == '\0') { /* treat "" as 0 */
if (pai->ai_socktype == GAI_ANY) {
/* caller accept *GAI_ANY* socktype */
pai->ai_socktype = SOCK_DGRAM;
pai->ai_protocol = IPPROTO_UDP;
}
port = htons((u_short)atoi(servname));
if (servnum < 0 || servnum > 0xffff)
ERR(EAI_SERVICE);
port = htons((u_short)servnum);
} else {
struct servent *sp;
char *proto;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.