From 1a9f9038d4b7f9d964d6e495309479f3b25c6001 Mon Sep 17 00:00:00 2001 From: "devsy-app[bot]" <277138668+devsy-app[bot]@users.noreply.github.com> Date: Sun, 9 Aug 2026 21:54:33 +0000 Subject: [PATCH] fix(netstat): correct dead paren check in getProcName getProcName's second guard checked the already-validated open-paren index instead of the close-paren index. The close-paren-not-found case was only caught indirectly by the later i > j fallback, leaving misleading dead code. Use j < 0 to make the intent explicit, and add table-driven tests covering the previously-untested helper including the no-close-paren edge case. --- pkg/netstat/netstat_util.go | 2 +- pkg/netstat/netstat_util_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/netstat/netstat_util.go b/pkg/netstat/netstat_util.go index a224db4d1..63ea79457 100644 --- a/pkg/netstat/netstat_util.go +++ b/pkg/netstat/netstat_util.go @@ -186,7 +186,7 @@ func getProcName(s []byte) string { return "" } j := bytes.LastIndex(s, []byte(")")) - if i < 0 { + if j < 0 { return "" } if i > j { diff --git a/pkg/netstat/netstat_util_test.go b/pkg/netstat/netstat_util_test.go index fddba09b1..a68e9f6ea 100644 --- a/pkg/netstat/netstat_util_test.go +++ b/pkg/netstat/netstat_util_test.go @@ -92,3 +92,24 @@ func (s *NetstatUtilTestSuite) TestDoNetstat_PropagatesParseError() { assert.Error(s.T(), err, "parser errors must propagate") } + +func (s *NetstatUtilTestSuite) TestGetProcName() { + tests := []struct { + name string + input string + want string + }{ + {"name in parens", "1234 (comm-name) stuff", "comm-name"}, + {"no opening paren", "comm-name", ""}, + {"no closing paren", "1234 (comm-name", ""}, + {"reversed parens", ")comm-name(", ""}, + {"empty name", "1234 ()", ""}, + {"name with parens inside", "1234 (a (b) c)", "a (b) c"}, + } + + for _, tt := range tests { + s.Run(tt.name, func() { + assert.Equal(s.T(), tt.want, getProcName([]byte(tt.input))) + }) + } +}