[git commit] netstat: fix breakage due to last commit

Denys Vlasenko vda.linux at googlemail.com
Tue Feb 24 22:11:21 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=1ecee617873163665e9f644b236ef1bad3e8a775
branch: https://git.busybox.net/busybox/log/?h=master

function                                             old     new   delta
bb_get_servname_by_port                              182     195     +13
ip_port_str                                          121     120      -1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 13/-1)              Total: 12 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/libbb.h                 |  2 +-
 libbb/bb_get_servname_by_port.c | 22 ++++++++++++++++------
 networking/netstat.c            |  4 ++--
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index f02d2c756..e282066b8 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -911,7 +911,7 @@ char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) FAST_FUNC RETU
 /* inet_[ap]ton on steroids */
 char* xmalloc_sockaddr2dotted(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
 char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
-// NB: unlike getservbyport, port paramenter is NOT in network order
+// NB: unlike getservbyport, port parameter is NOT in network order
 #define getservbyport dont_use_getservbyport_uses_global_buffer
 char* bb_get_servname_by_port(char **p_etc_services, int port, const char *type) FAST_FUNC RETURNS_MALLOC;
 // "old" (ipv4 only) API
diff --git a/libbb/bb_get_servname_by_port.c b/libbb/bb_get_servname_by_port.c
index d8ba55226..3846072f9 100644
--- a/libbb/bb_get_servname_by_port.c
+++ b/libbb/bb_get_servname_by_port.c
@@ -9,6 +9,12 @@
 //kbuild:lib-$(CONFIG_PSCAN) += bb_get_servname_by_port.o
 #include "libbb.h"
 
+//Rationale for exising:
+//#define getservbyport dont_use_getservbyport_uses_global_buffer
+//(for example: -280 bytes on musl, x86-32)
+//TODO:
+//avoid getservbyname() as well
+
 char* FAST_FUNC bb_get_servname_by_port(char **p_etc_services, int port, const char *type)
 {
 	char *sp;
@@ -22,23 +28,27 @@ char* FAST_FUNC bb_get_servname_by_port(char **p_etc_services, int port, const c
 		*p_etc_services = sp;
 	}
 	while (*sp) {
-		const char *portstr, *sp_end;
+		const char *pnstr, *sp_end;
 		char *end;
-		unsigned portnum;
+		unsigned n;
 
 		sp = skip_whitespace(sp);
 		if (*sp == '#')
 			goto next;
 		sp_end = skip_non_whitespace(sp);
-		portstr = skip_whitespace(sp_end);
-		portnum = bb_strtou(portstr, &end, 10);
-		if (portnum != port || *end != '/')
+		pnstr = skip_whitespace(sp_end);
+		n = bb_strtou(pnstr, &end, 10);
+		if (n != port || *end != '/')
 			goto next;
 		if (type) {
 			end++;
 			end = is_prefixed_with(end, type);
-			if (!end || !(isspace(*end) || *end == '\0'))
+			if (!end
+			 || !(isspace(*end) || *end == '\0'
+			    || *end == '#') // glibc treats "http 80/tcp#COMMENT" as valid
+			) {
 				goto next;
+			}
 		}
 		return auto_string(xstrndup(sp, sp_end - sp));
  next:
diff --git a/networking/netstat.c b/networking/netstat.c
index 80d94221b..d31928957 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -176,7 +176,7 @@ struct globals {
 	smallint prg_cache_loaded;
 	struct prg_node *prg_hash[PRG_HASH_SIZE];
 #endif
-	char **p_etc_services;
+	char *p_etc_services;
 #if ENABLE_FEATURE_NETSTAT_PRG
 	const char *progname_banner;
 #endif
@@ -382,7 +382,7 @@ static const char *get_sname(int port, const char *proto, int numeric)
 	if (port == 0)
 		return "*";
 	if (!numeric) {
-		const char *se = bb_get_servname_by_port(G.p_etc_services, port, proto);
+		const char *se = bb_get_servname_by_port(&G.p_etc_services, port, proto);
 		if (se)
 			return se;
 	}


More information about the busybox-cvs mailing list