From 2ffea8cf3a9488c52567d19bf00377e8cbe1e736 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 27 Jan 2025 14:29:36 +0800 Subject: Fix config loading Previous abuse of anonymous functions caused it to not report errors correctly, leading to null dereferences everywhere --- config.go | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'config.go') 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 } -- cgit v1.2.3