Skip to content

Commit

Permalink
Fix NPE bugs in sync.Regexp (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterklijn authored Oct 28, 2022
1 parent a863d18 commit d28035f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 6 additions & 6 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ func (r *Regexp) Set(value *regexp.Regexp) {

// MarshalJSON returns the JSON encoding of the value.
func (r *Regexp) MarshalJSON() ([]byte, error) {
r.rw.RLock()
defer r.rw.RUnlock()
return json.Marshal(r.value.String())
return json.Marshal(r.String())
}

// UnmarshalJSON returns the JSON encoding of the value.
Expand All @@ -354,9 +352,11 @@ func (r *Regexp) UnmarshalJSON(d []byte) error {

// String returns a string representation of the value.
func (r *Regexp) String() string {
r.rw.RLock()
defer r.rw.RUnlock()
return r.value.String()
regex := r.Get()
if regex == nil {
return ""
}
return regex.String()
}

//
Expand Down
12 changes: 12 additions & 0 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ func TestRegexp_SetString(t *testing.T) {
}
}

func TestRegexp_String(t *testing.T) {
sr := Regexp{}
assert.Equal(t, "", sr.String())
}

func TestRegexp_MarshalJSON(t *testing.T) {
sr := Regexp{}
json, err := sr.MarshalJSON()
assert.Equal(t, []byte(`""`), json)
assert.NoError(t, err)
}

func TestStringMap(t *testing.T) {
var sm StringMap
ch := make(chan struct{})
Expand Down

0 comments on commit d28035f

Please sign in to comment.