1
0
mirror of https://github.com/openbsd/src.git synced 2026-05-01 01:27:08 +00:00

use strtonum() instead of atoi(), and error out for bad numbers

This generates a host-order number, so the ntohs() for getservbyport()
was wrong, that should always have been htons().  The transform is
the same, but misleading.
ok tb
This commit is contained in:
deraadt
2025-10-11 15:46:06 +00:00
parent 0f168beaa7
commit 9cc33de2e7

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: netcat.c,v 1.234 2025/06/24 13:37:11 tb Exp $ */
/* $OpenBSD: netcat.c,v 1.235 2025/10/11 15:46:06 deraadt Exp $ */
/*
* Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
* Copyright (c) 2015 Bob Beck. All rights reserved.
@@ -1542,7 +1542,12 @@ connection_info(const char *host, const char *port, const char *proto,
/* Look up service name unless -n. */
if (!nflag) {
sv = getservbyport(ntohs(atoi(port)), proto);
const char *errstr;
int p = strtonum(port, 1, PORT_MAX, &errstr);
if (errstr)
errx(1, "port number %s: %s", errstr, port);
sv = getservbyport(htons(p), proto);
if (sv != NULL)
service = sv->s_name;
}