Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 8fefef0

Browse files
utils: guard tlsConfigRegister by a lock (go-sql-driver#613)
* utils: guard tlsConfigRegister by a lock Fixes go-sql-driver#610 * utils: move TLSConfig lock and clone logic to helper func
1 parent d2a8175 commit 8fefef0

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

dsn.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,7 @@ func parseDSNParams(cfg *Config, params string) (err error) {
528528
return fmt.Errorf("invalid value for TLS config name: %v", err)
529529
}
530530

531-
if tlsConfig, ok := tlsConfigRegister[name]; ok {
532-
tlsConfig = cloneTLSConfig(tlsConfig)
533-
531+
if tlsConfig := getTLSConfigClone(name); tlsConfig != nil {
534532
if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
535533
host, _, err := net.SplitHostPort(cfg.Addr)
536534
if err == nil {

utils.go

+16
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import (
1616
"fmt"
1717
"io"
1818
"strings"
19+
"sync"
1920
"sync/atomic"
2021
"time"
2122
)
2223

2324
var (
25+
tlsConfigLock sync.RWMutex
2426
tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
2527
)
2628

@@ -54,19 +56,32 @@ func RegisterTLSConfig(key string, config *tls.Config) error {
5456
return fmt.Errorf("key '%s' is reserved", key)
5557
}
5658

59+
tlsConfigLock.Lock()
5760
if tlsConfigRegister == nil {
5861
tlsConfigRegister = make(map[string]*tls.Config)
5962
}
6063

6164
tlsConfigRegister[key] = config
65+
tlsConfigLock.Unlock()
6266
return nil
6367
}
6468

6569
// DeregisterTLSConfig removes the tls.Config associated with key.
6670
func DeregisterTLSConfig(key string) {
71+
tlsConfigLock.Lock()
6772
if tlsConfigRegister != nil {
6873
delete(tlsConfigRegister, key)
6974
}
75+
tlsConfigLock.Unlock()
76+
}
77+
78+
func getTLSConfigClone(key string) (config *tls.Config) {
79+
tlsConfigLock.RLock()
80+
if v, ok := tlsConfigRegister[key]; ok {
81+
config = cloneTLSConfig(v)
82+
}
83+
tlsConfigLock.RUnlock()
84+
return
7085
}
7186

7287
// Returns the bool value of the input.
@@ -745,6 +760,7 @@ func escapeStringQuotes(buf []byte, v string) []byte {
745760
/******************************************************************************
746761
* Sync utils *
747762
******************************************************************************/
763+
748764
// noCopy may be embedded into structs which must not be copied
749765
// after the first use.
750766
//

0 commit comments

Comments
 (0)