diff options
author | Runxi Yu <me@runxiyu.org> | 2025-01-27 14:29:36 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-01-27 14:29:36 +0800 |
commit | 2ffea8cf3a9488c52567d19bf00377e8cbe1e736 (patch) | |
tree | 8a98f34352baa937dc51202a5e2850ca8738d2db | |
parent | main: Use select{} for the deadlock (diff) | |
download | maild-2ffea8cf3a9488c52567d19bf00377e8cbe1e736.tar.gz maild-2ffea8cf3a9488c52567d19bf00377e8cbe1e736.tar.zst maild-2ffea8cf3a9488c52567d19bf00377e8cbe1e736.zip |
Previous abuse of anonymous functions caused it to not report errors
correctly, leading to null dereferences everywhere
-rw-r--r-- | config.go | 53 |
1 files changed, 24 insertions, 29 deletions
@@ -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 } |