From 31517c08d2fac9f10ae875a12b2b455ae2c152f3 Mon Sep 17 00:00:00 2001 From: William Martin Date: Wed, 2 Jul 2025 13:14:45 +0200 Subject: [PATCH] Fix ListTunnelPorts in Go In a for loop, a new address is created for the loopvar. Appending the pointer of the loopvar to a slice means that every element in the slice will point to the last element ranged over. --- go/tunnels/manager.go | 4 ++-- go/tunnels/manager_test.go | 9 +++++++++ go/tunnels/tunnels.go | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/go/tunnels/manager.go b/go/tunnels/manager.go index 7c3c648d..9227fb76 100644 --- a/go/tunnels/manager.go +++ b/go/tunnels/manager.go @@ -410,8 +410,8 @@ func (m *Manager) ListTunnelPorts( return nil, fmt.Errorf("error parsing response json to tunnel ports: %w", err) } - for _, port := range tunnelPortsResponse.Value { - tp = append(tp, &port) + for i := range tunnelPortsResponse.Value { + tp = append(tp, &tunnelPortsResponse.Value[i]) } return tp, nil diff --git a/go/tunnels/manager_test.go b/go/tunnels/manager_test.go index f4dab118..7b96101b 100644 --- a/go/tunnels/manager_test.go +++ b/go/tunnels/manager_test.go @@ -653,6 +653,15 @@ func TestTunnelListPorts(t *testing.T) { if len(ports) != 2 { t.Errorf("ports not successfully listed") } + + if ports[0].PortNumber != 3000 { + t.Errorf("port 3000 not successfully listed") + } + + if ports[1].PortNumber != 3001 { + t.Errorf("port 3001 not successfully listed") + } + for _, port := range ports { logger.Printf("Port: %d", port.PortNumber) port.Table().Print() diff --git a/go/tunnels/tunnels.go b/go/tunnels/tunnels.go index 953e288e..5e7e2c48 100644 --- a/go/tunnels/tunnels.go +++ b/go/tunnels/tunnels.go @@ -10,7 +10,7 @@ import ( "github.com/rodaine/table" ) -const PackageVersion = "0.1.18" +const PackageVersion = "0.1.19" func (tunnel *Tunnel) requestObject() (*Tunnel, error) { convertedTunnel := &Tunnel{