diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-09 22:00:44 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-09 22:00:44 +0200 |
commit | 7ab9cd23988b48956fcfe171d5828d61285baf40 (patch) | |
tree | 10b3d522ff45b06ac9dd9f362ddc336c5b058ee0 /libbb | |
parent | 82c5eb8e4681ba345500e5c368fb54741bb0c450 (diff) | |
download | busybox-w32-7ab9cd23988b48956fcfe171d5828d61285baf40.tar.gz busybox-w32-7ab9cd23988b48956fcfe171d5828d61285baf40.tar.bz2 busybox-w32-7ab9cd23988b48956fcfe171d5828d61285baf40.zip |
libbb: make bb_lookup_port() abort on bad port names
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@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xconnect.c | 20 |
1 files changed, 6 insertions, 14 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) | |||
115 | 115 | ||
116 | /* Return port number for a service. | 116 | /* Return port number for a service. |
117 | * If "port" is a number use it as the port. | 117 | * If "port" is a number use it as the port. |
118 | * If "port" is a name it is looked up in /etc/services, | 118 | * If "port" is a name it is looked up in /etc/services. |
119 | * if it isnt found return default_port | 119 | * if NULL, return default_port |
120 | */ | 120 | */ |
121 | unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port) | 121 | unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned port_nr) |
122 | { | 122 | { |
123 | unsigned port_nr = default_port; | ||
124 | if (port) { | 123 | if (port) { |
125 | int old_errno; | ||
126 | |||
127 | /* Since this is a lib function, we're not allowed to reset errno to 0. | ||
128 | * Doing so could break an app that is deferring checking of errno. */ | ||
129 | old_errno = errno; | ||
130 | port_nr = bb_strtou(port, NULL, 10); | 124 | port_nr = bb_strtou(port, NULL, 10); |
131 | if (errno || port_nr > 65535) { | 125 | if (errno || port_nr > 65535) { |
132 | struct servent *tserv = getservbyname(port, protocol); | 126 | struct servent *tserv = getservbyname(port, protocol); |
133 | port_nr = default_port; | 127 | if (!tserv) |
134 | if (tserv) | 128 | bb_error_msg_and_die("bad port '%s'", port); |
135 | port_nr = ntohs(tserv->s_port); | 129 | port_nr = ntohs(tserv->s_port); |
136 | //FIXME: else: port string was garbage, but we don't report that??? | ||
137 | } | 130 | } |
138 | errno = old_errno; | ||
139 | } | 131 | } |
140 | return (uint16_t)port_nr; | 132 | return (uint16_t)port_nr; |
141 | } | 133 | } |