[git commit] libbb: make bb_lookup_port() abort on bad port names

Denys Vlasenko vda.linux at googlemail.com
Thu Sep 9 20:00:44 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=7ab9cd23988b48956fcfe171d5828d61285baf40
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Also, no need to preserve errno

function                                             old     new   delta
.rodata                                           104247  104241      -6
bb_lookup_port                                        97      83     -14
nc_main                                             1039    1018     -21
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-41)             Total: -41 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/xconnect.c       | 20 ++++++--------------
 networking/ftpgetput.c |  3 +--
 networking/nc.c        |  2 +-
 networking/nc_bloaty.c |  7 ++-----
 4 files changed, 10 insertions(+), 22 deletions(-)

diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 5dd9cfd28..f1983a68b 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -115,27 +115,19 @@ void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
 
 /* Return port number for a service.
  * If "port" is a number use it as the port.
- * If "port" is a name it is looked up in /etc/services,
- * if it isnt found return default_port
+ * If "port" is a name it is looked up in /etc/services.
+ * if NULL, return default_port
  */
-unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
+unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned port_nr)
 {
-	unsigned port_nr = default_port;
 	if (port) {
-		int old_errno;
-
-		/* Since this is a lib function, we're not allowed to reset errno to 0.
-		 * Doing so could break an app that is deferring checking of errno. */
-		old_errno = errno;
 		port_nr = bb_strtou(port, NULL, 10);
 		if (errno || port_nr > 65535) {
 			struct servent *tserv = getservbyname(port, protocol);
-			port_nr = default_port;
-			if (tserv)
-				port_nr = ntohs(tserv->s_port);
-//FIXME: else: port string was garbage, but we don't report that???
+			if (!tserv)
+				bb_error_msg_and_die("bad port '%s'", port);
+			port_nr = ntohs(tserv->s_port);
 		}
-		errno = old_errno;
 	}
 	return (uint16_t)port_nr;
 }
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 30b3dabd1..4c92f34a1 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -290,8 +290,7 @@ static const char ftpgetput_longopts[] ALIGN1 =
 int ftpgetput_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ftpgetput_main(int argc UNUSED_PARAM, char **argv)
 {
-	const char *port = "ftp";
-	/* socket to ftp server */
+	const char *port = NULL;
 
 #if ENABLE_FTPPUT && !ENABLE_FTPGET
 # define ftp_action ftp_send
diff --git a/networking/nc.c b/networking/nc.c
index 705b7356a..d351bf72a 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -216,7 +216,7 @@ int nc_main(int argc, char **argv)
 				close(sfd);
 		} else {
 			cfd = create_and_connect_stream_or_die(argv[0],
-				argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 0);
+				bb_lookup_port(argv[1], "tcp", 0));
 		}
 	}
 
diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c
index 25b95246f..cfa133eae 100644
--- a/networking/nc_bloaty.c
+++ b/networking/nc_bloaty.c
@@ -813,8 +813,6 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
 	//if (option_mask32 & OPT_o) /* hexdump log */
 	if (option_mask32 & OPT_p) { /* local source port */
 		o_lport = bb_lookup_port(str_p, o_udpmode ? "udp" : "tcp", 0);
-		if (!o_lport)
-			bb_error_msg_and_die("bad local port '%s'", str_p);
 	}
 	//if (option_mask32 & OPT_r) /* randomize various things */
 	//if (option_mask32 & OPT_u) /* use UDP */
@@ -827,9 +825,8 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
 
 	if (argv[0]) {
 		themaddr = xhost2sockaddr(argv[0],
-			argv[1]
-			? bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp", 0)
-			: 0);
+			bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp", 0)
+		);
 	}
 
 	/* create & bind network socket */


More information about the busybox-cvs mailing list