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>
This commit is contained in:
		@@ -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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user