From 7e983d835e731bdc24c4b940d7140bd3cf9c0ddc Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Sun, 10 Nov 2024 12:21:30 -0500 Subject: [PATCH] fix: guard string splits against nil Signed-off-by: Chris Gianelloni --- main.go | 5 ++++- peers.go | 21 +++++++++++++++++++-- utils.go | 5 ++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index c3dbc8b..ef497d6 100644 --- a/main.go +++ b/main.go @@ -1043,6 +1043,9 @@ func getPeerText(ctx context.Context) string { if strings.Contains(peer.IP, ":") { if len(strings.Split(peer.IP, ":")) > 3 { splitIP := strings.Split(peer.IP, ":") + if splitIP == nil { + continue + } peerIP = fmt.Sprintf("%s...%s:%s", splitIP[0], splitIP[:len(splitIP)-2], @@ -1223,7 +1226,7 @@ func tcpinfoRtt(address string) int { if err != nil { return result } - var q *tcpinfo.Info + q := &tcpinfo.Info{} if err := json.Unmarshal(txt, &q); err != nil { result = int(q.RTT.Seconds() * 1000) } diff --git a/peers.go b/peers.go index 4be34b6..6fbc2af 100644 --- a/peers.go +++ b/peers.go @@ -87,6 +87,9 @@ func filterPeers(ctx context.Context) error { // Process peersIn for _, peer := range peersIn { p := strings.Split(peer, ":") + if p == nil { + continue + } peerIP := p[0] peerPORT := p[1] if strings.HasPrefix(peerIP, "[") { // IPv6 @@ -100,7 +103,11 @@ func filterPeers(ctx context.Context) error { } else { added := false for i, toCheck := range peers { - checkIP := strings.Split(toCheck, ":")[0] + checkIPArr := strings.Split(toCheck, ":") + if checkIPArr == nil { + continue + } + checkIP := checkIPArr[0] if checkIP == peerIP { if p[2] != "i" { // Remove and re-add as duplex (i+o) @@ -120,6 +127,9 @@ func filterPeers(ctx context.Context) error { // Process peersOut for _, peer := range peersOut { p := strings.Split(peer, ":") + if p == nil { + continue + } peerIP := p[0] peerPORT := p[1] if strings.HasPrefix(peerIP, "[") { // IPv6 @@ -133,7 +143,11 @@ func filterPeers(ctx context.Context) error { } else { added := false for i, toCheck := range peers { - checkIP := strings.Split(toCheck, ":")[0] + checkIPArr := strings.Split(toCheck, ":") + if checkIPArr == nil { + continue + } + checkIP := checkIPArr[0] if checkIP == peerIP { if p[2] != "o" { // Remove and re-add as duplex (i+o) @@ -175,6 +189,9 @@ func pingPeers(ctx context.Context) error { go func() { defer wg.Done() peerArr := strings.Split(v, ";") + if peerArr == nil { + return + } peerIP := peerArr[0] peerPORT := peerArr[1] peerDIR := peerArr[2] diff --git a/utils.go b/utils.go index d568699..44ec0fa 100644 --- a/utils.go +++ b/utils.go @@ -1,4 +1,4 @@ -// Copyright 2023 Blink Labs Software +// Copyright 2024 Blink Labs Software // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -36,6 +36,9 @@ func getNodeVersion() (version string, revision string, err error) { return "N/A", "N/A", err } strArray := strings.Split(string(stdout), string(' ')) + if strArray == nil { + return "N/A", "N/A", fmt.Errorf("error") + } version = strArray[1] revision = strArray[7] if len(revision) > 8 {