From f408d0f37b425271be02049495ca47a54028b35d Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Wed, 23 May 2018 15:39:30 -0400 Subject: [PATCH] review fix: add nil receivers to other read-only ModeSet methods --- irc/modes/modes.go | 23 ++++++++++++++++------- irc/modes/modes_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/irc/modes/modes.go b/irc/modes/modes.go index 19328457..310e46f4 100644 --- a/irc/modes/modes.go +++ b/irc/modes/modes.go @@ -366,6 +366,10 @@ func (set *ModeSet) SetMode(mode Mode, on bool) (applied bool) { // return the modes in the set as a slice func (set *ModeSet) AllModes() (result []Mode) { + if set == nil { + return + } + set.RLock() defer set.RUnlock() @@ -376,22 +380,27 @@ func (set *ModeSet) AllModes() (result []Mode) { } // String returns the modes in this set. -func (set *ModeSet) String() string { +func (set *ModeSet) String() (result string) { + if set == nil { + return + } + set.RLock() defer set.RUnlock() - if len(set.modes) == 0 { - return "" - } - var result []byte + var buf strings.Builder for mode := range set.modes { - result = append(result, mode.String()...) + buf.WriteRune(rune(mode)) } - return string(result) + return buf.String() } // Prefixes returns a list of prefixes for the given set of channel modes. func (set *ModeSet) Prefixes(isMultiPrefix bool) (prefixes string) { + if set == nil { + return + } + set.RLock() defer set.RUnlock() diff --git a/irc/modes/modes_test.go b/irc/modes/modes_test.go index 8f20bdfc..216add3c 100644 --- a/irc/modes/modes_test.go +++ b/irc/modes/modes_test.go @@ -35,3 +35,16 @@ func TestSetMode(t *testing.T) { t.Errorf("unexpected modestring: %s", modeString) } } + +func TestNilReceivers(t *testing.T) { + var set ModeSet + + if set.HasMode(Invisible) { + t.Errorf("nil ModeSet should not have any modes") + } + + str := set.String() + if str != "" { + t.Errorf("nil Modeset should have empty String(), got %v instead", str) + } +}