Skip to content

Commit

Permalink
fix: guard string splits against nil (#238)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 authored Nov 11, 2024
1 parent 68361c3 commit ff8ca15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 19 additions & 2 deletions peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ff8ca15

Please sign in to comment.