Skip to content

Commit ed58611

Browse files
Race condition fix: Ensure initialization occurs before value assignments in Scratch methods.
1 parent 16cf7ad commit ed58611

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

template/scratch.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ func (s *Scratch) Get(k string) interface{} {
3434
// Set stores the value v at the key k. It will overwrite an existing value
3535
// if present.
3636
func (s *Scratch) Set(k string, v interface{}) string {
37-
s.init()
38-
3937
s.Lock()
4038
defer s.Unlock()
39+
40+
s.init()
4141
s.values[k] = v
4242
return ""
4343
}
4444

4545
// SetX behaves the same as Set, except it will not overwrite existing keys if
4646
// already present.
4747
func (s *Scratch) SetX(k string, v interface{}) string {
48-
s.init()
49-
5048
s.Lock()
5149
defer s.Unlock()
50+
51+
s.init()
5252
if _, ok := s.values[k]; !ok {
5353
s.values[k] = v
5454
}
@@ -57,20 +57,20 @@ func (s *Scratch) SetX(k string, v interface{}) string {
5757

5858
// MapSet stores the value v into a key mk in the map named k.
5959
func (s *Scratch) MapSet(k, mk string, v interface{}) (string, error) {
60-
s.init()
61-
6260
s.Lock()
6361
defer s.Unlock()
62+
63+
s.init()
6464
return s.mapSet(k, mk, v, true)
6565
}
6666

6767
// MapSetX behaves the same as MapSet, except it will not overwrite the map
6868
// key if it already exists.
6969
func (s *Scratch) MapSetX(k, mk string, v interface{}) (string, error) {
70-
s.init()
71-
7270
s.Lock()
7371
defer s.Unlock()
72+
s.init()
73+
7474
return s.mapSet(k, mk, v, false)
7575
}
7676

@@ -96,10 +96,10 @@ func (s *Scratch) mapSet(k, mk string, v interface{}, o bool) (string, error) {
9696

9797
// MapValues returns the list of values in the map sorted by key.
9898
func (s *Scratch) MapValues(k string) ([]interface{}, error) {
99-
s.init()
100-
10199
s.Lock()
102100
defer s.Unlock()
101+
s.init()
102+
103103
if s.values == nil {
104104
return nil, nil
105105
}

0 commit comments

Comments
 (0)