From 501943c8f7507275f32dfa069e69231abe16e2b4 Mon Sep 17 00:00:00 2001 From: "Brandon McGinty (chatgpt)" Date: Sun, 9 Aug 2026 14:22:17 -0400 Subject: [PATCH] Handle malformed and IPv6 config addresses --- config/user_config.go | 16 +++++++++++----- config/user_config_test.go | 13 +++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/config/user_config.go b/config/user_config.go index 5451565..79d2f3d 100644 --- a/config/user_config.go +++ b/config/user_config.go @@ -6,6 +6,7 @@ import ( "git.stormux.org/storm/barnard/uiterm" "github.com/pelletier/go-toml/v2" "io/ioutil" + "net" "os" "strconv" "strings" @@ -367,7 +368,7 @@ func readFile(path string) []byte { func fileExists(path string) bool { info, err := os.Stat(path) - if os.IsNotExist(err) { + if err != nil { return false } return !info.IsDir() @@ -389,11 +390,16 @@ func resolvePath(path string) string { } func makeHostPort(addr string) (string, int) { - parts := strings.Split(addr, ":") - host := parts[0] - port, err := strconv.Atoi(parts[1]) + // SplitHostPort correctly handles bracketed IPv6. Invalid or portless + // addresses stay usable as a host with Mumble's default port instead of + // crashing configuration operations. + host, portText, err := net.SplitHostPort(addr) if err != nil { - panic(err) + return strings.Trim(addr, "[]"), 64738 + } + port, err := strconv.Atoi(portText) + if err != nil || port < 1 || port > 65535 { + return host, 64738 } return host, port } diff --git a/config/user_config_test.go b/config/user_config_test.go index a82d078..6f3123b 100644 --- a/config/user_config_test.go +++ b/config/user_config_test.go @@ -37,6 +37,19 @@ func TestConfigBackfillsRecordingDefaults(t *testing.T) { } } +// Regression: malformed and IPv6 addresses were split at every colon and +// could panic while merely reading a saved user preference. +func TestMakeHostPortHandlesIPv6AndMalformedAddress(t *testing.T) { + host, port := makeHostPort("[2001:db8::1]:64739") + if host != "2001:db8::1" || port != 64739 { + t.Fatalf("got %q:%d", host, port) + } + host, port = makeHostPort("not-a-host-port") + if host != "not-a-host-port" || port != 64738 { + t.Fatalf("got %q:%d", host, port) + } +} + func TestConfigUsesHomeEnvironmentForDefaultPath(t *testing.T) { dir := t.TempDir() t.Setenv("HOME", dir)