aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-27 14:29:36 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-27 14:29:36 +0800
commit2ffea8cf3a9488c52567d19bf00377e8cbe1e736 (patch)
tree8a98f34352baa937dc51202a5e2850ca8738d2db /config.go
parentmain: Use select{} for the deadlock (diff)
downloadmaild-2ffea8cf3a9488c52567d19bf00377e8cbe1e736.tar.gz
maild-2ffea8cf3a9488c52567d19bf00377e8cbe1e736.tar.zst
maild-2ffea8cf3a9488c52567d19bf00377e8cbe1e736.zip
Fix config loadingHEADmaster
Previous abuse of anonymous functions caused it to not report errors correctly, leading to null dereferences everywhere
Diffstat (limited to 'config.go')
-rw-r--r--config.go53
1 files changed, 24 insertions, 29 deletions
diff --git a/config.go b/config.go
index e7d6aa4..693b746 100644
--- a/config.go
+++ b/config.go
@@ -42,39 +42,34 @@ func load_config(path string) error {
return err
}
decoder := scfg.NewDecoder(bufio.NewReader(config_file))
- if func() error {
- config_mutex.Lock()
- defer config_mutex.Unlock()
- err = decoder.Decode(&config)
- if err != nil {
- return err
- }
- config_context, config_context_cancel = context.WithCancel(context.Background())
-
- // TLS key loading and TLS config creation
- cer, err := tls.LoadX509KeyPair(config.TLS.Cert, config.TLS.Key)
- if err != nil {
- return err
- }
- config._tls_config = &tls.Config{
- Certificates: []tls.Certificate{cer},
- MinVersion: tls.VersionTLS13,
- }
+ config_mutex.Lock()
+ defer config_mutex.Unlock()
+ err = decoder.Decode(&config)
+ if err != nil {
+ return err
+ }
+ config_context, config_context_cancel = context.WithCancel(context.Background())
- // Database setup
- if config.DB.Type != "postgres" {
- return err_unsupported_database_type
- }
- global_db, err = pgxpool.New(config_context, config.DB.Conn)
- // BUG: Context-related leak: cancel context when the config is invalidated
- if err != nil {
- return err
- }
+ // TLS key loading and TLS config creation
+ cer, err := tls.LoadX509KeyPair(config.TLS.Cert, config.TLS.Key)
+ if err != nil {
+ return err
+ }
+ config._tls_config = &tls.Config{
+ Certificates: []tls.Certificate{cer},
+ MinVersion: tls.VersionTLS13,
+ }
- return nil
- }() != nil {
+ // Database setup
+ if config.DB.Type != "postgres" {
+ return err_unsupported_database_type
+ }
+ global_db, err = pgxpool.New(config_context, config.DB.Conn)
+ // BUG: Context-related leak: cancel context when the config is invalidated
+ if err != nil {
return err
}
+
return nil
}