From e75e9a0e7e53851e344633abe402979cdbece15b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 6 Jan 2024 17:30:03 +0800 Subject: [PATCH] Fix panic when parsing empty pgsql host (#28708) Regression of #27723 Fix #28705 --- 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 b68f250f7..015c918d9 100644 --- a/modules/setting/database.go +++ b/modules/setting/database.go @@ -168,7 +168,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 1d5b41650..14e0a6ac0 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",