Skip to content

Commit

Permalink
Prevent panic when send dests is nil
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Apr 29, 2020
1 parent 1af3944 commit 6a43891
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ func (c *Client) Send(dests *StringArray, data interface{}, config *MessageConfi
return nil, err
}

if err := c.send(dests.Elems, payload, !config.Unencrypted, config.MaxHoldingSeconds); err != nil {
if err := c.send(dests.Elems(), payload, !config.Unencrypted, config.MaxHoldingSeconds); err != nil {
return nil, err
}

Expand Down
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,28 +222,28 @@ func GetDefaultTransactionConfig() *TransactionConfig {
// GetRandomSeedRPCServerAddr returns a random seed rpc server address from the
// client config.
func (config *ClientConfig) GetRandomSeedRPCServerAddr() string {
if len(config.SeedRPCServerAddr.Elems) == 0 {
if config.SeedRPCServerAddr.Len() == 0 {
return ""
}
return config.SeedRPCServerAddr.Elems[rand.Intn(len(config.SeedRPCServerAddr.Elems))]
return config.SeedRPCServerAddr.Elems()[rand.Intn(config.SeedRPCServerAddr.Len())]
}

// GetRandomSeedRPCServerAddr returns a random seed rpc server address from the
// wallet config.
func (config *WalletConfig) GetRandomSeedRPCServerAddr() string {
if len(config.SeedRPCServerAddr.Elems) == 0 {
if config.SeedRPCServerAddr.Len() == 0 {
return ""
}
return config.SeedRPCServerAddr.Elems[rand.Intn(len(config.SeedRPCServerAddr.Elems))]
return config.SeedRPCServerAddr.Elems()[rand.Intn(config.SeedRPCServerAddr.Len())]
}

// GetRandomSeedRPCServerAddr returns a random seed rpc server address from the
// rpc config.
func (config *RPCConfig) GetRandomSeedRPCServerAddr() string {
if len(config.SeedRPCServerAddr.Elems) == 0 {
if config.SeedRPCServerAddr.Len() == 0 {
return ""
}
return config.SeedRPCServerAddr.Elems[rand.Intn(len(config.SeedRPCServerAddr.Elems))]
return config.SeedRPCServerAddr.Elems()[rand.Intn(config.SeedRPCServerAddr.Len())]
}

// MergeClientConfig merges a given client config with the default client config
Expand Down
6 changes: 3 additions & 3 deletions multiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (m *MultiClient) SendWithClient(clientID int, dests *StringArray, data inte
return nil, err
}

if err := m.sendWithClient(clientID, dests.Elems, payload, !config.Unencrypted, config.MaxHoldingSeconds); err != nil {
if err := m.sendWithClient(clientID, dests.Elems(), payload, !config.Unencrypted, config.MaxHoldingSeconds); err != nil {
return nil, err
}

Expand Down Expand Up @@ -374,7 +374,7 @@ func (m *MultiClient) Send(dests *StringArray, data interface{}, config *Message
go func() {
sent := 0
for clientID := range clients {
err := m.sendWithClient(clientID, dests.Elems, payload, !config.Unencrypted, config.MaxHoldingSeconds)
err := m.sendWithClient(clientID, dests.Elems(), payload, !config.Unencrypted, config.MaxHoldingSeconds)
if err == nil {
select {
case success <- struct{}{}:
Expand Down Expand Up @@ -557,7 +557,7 @@ func (m *MultiClient) Listen(addrsRe *StringArray) error {
if addrsRe == nil {
addrs = []string{DefaultSessionAllowAddr}
} else {
addrs = addrsRe.Elems
addrs = addrsRe.Elems()
}

var err error
Expand Down
14 changes: 11 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (amount *Amount) ToFixed64() common.Fixed64 {

// StringArray is a wrapper type for gomobile compatibility. StringArray is not
// protected by lock and should not be read and write at the same time.
type StringArray struct{ Elems []string }
type StringArray struct{ elems []string }

// NewStringArray creates a StringArray from a list of string elements.
func NewStringArray(elems ...string) *StringArray {
Expand All @@ -110,14 +110,22 @@ func NewStringArrayFromString(s string) *StringArray {
return &StringArray{strings.Fields(s)}
}

// Elems returns the string array elements.
func (sa *StringArray) Elems() []string {
if sa == nil {
return nil
}
return sa.elems
}

// Len returns the string array length.
func (sa *StringArray) Len() int {
return len(sa.Elems)
return len(sa.Elems())
}

// Append adds an element to the string array.
func (sa *StringArray) Append(s string) {
sa.Elems = append(sa.Elems, s)
sa.elems = append(sa.elems, s)
}

// StringMapFunc is a wrapper type for gomobile compatibility.
Expand Down

0 comments on commit 6a43891

Please sign in to comment.