From ad027c28181004cbf891be14bb383ed906b13521 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Sat, 6 Jan 2024 20:05:49 +0800 Subject: [PATCH] Fix panic when parsing empty pgsql host (#28708) (#28709) Backport #28708 by wxiaoguang Regression of #27723 Fix #28705 Co-authored-by: wxiaoguang (cherry picked from commit 7f833d8f71b71b0e983ed979c5ad8088aec7fb7d) --- modules/setting/database.go | 2 +- modules/setting/database_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/setting/database.go b/modules/setting/database.go index 063c854c7e..64c2820de2 100644 --- a/modules/setting/database.go +++ b/modules/setting/database.go @@ -170,7 +170,7 @@ func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbsslMode s RawQuery: dbParam, } query := connURL.Query() - if dbHost[0] == '/' { // looks like a unix socket + if strings.HasPrefix(dbHost, "/") { // looks like a unix socket query.Add("host", dbHost) connURL.Host = ":" + port } diff --git a/modules/setting/database_test.go b/modules/setting/database_test.go index 1d5b416504..14e0a6ac02 100644 --- a/modules/setting/database_test.go +++ b/modules/setting/database_test.go @@ -65,6 +65,10 @@ func Test_getPostgreSQLConnectionString(t *testing.T) { SSLMode string Output string }{ + { + Host: "", // empty means default + Output: "postgres://:@127.0.0.1:5432?sslmode=", + }, { Host: "/tmp/pg.sock", User: "testuser",