Guard against accessing slices that may have contents changed

This commit is contained in:
Armon Dadgar 2013-12-18 16:40:32 -08:00
parent 4abc881adc
commit 108df68d3d
2 changed files with 15 additions and 7 deletions

View File

@ -252,7 +252,10 @@ func (s *Server) Shutdown() error {
if s.raft != nil {
s.raftTransport.Close()
s.raftLayer.Close()
s.raft.Shutdown()
future := s.raft.Shutdown()
if err := future.Error(); err != nil {
s.logger.Printf("[WARN] Error shutting down raft: %s", err)
}
s.raftStore.Close()
}

View File

@ -154,8 +154,7 @@ func (s *StateStore) GetNode(name string) (bool, string) {
} else if err != nil {
panic(fmt.Errorf("Failed to get node: %v", err))
}
return true, string(val)
return true, string(sliceCopy(val))
}
// GetNodes returns all the known nodes, the slice alternates between
@ -180,7 +179,7 @@ func (s *StateStore) Nodes() []string {
} else if err != nil {
panic(fmt.Errorf("Failed to get nodes: %v", err))
}
nodes = append(nodes, string(key), string(val))
nodes = append(nodes, string(sliceCopy(key)), string(sliceCopy(val)))
}
return nodes
}
@ -301,7 +300,7 @@ func parseNodeServices(tx *mdb.Txn, dbi mdb.DBI, prefix []byte) rpc.NodeServices
}
// Split to get service name
parts := bytes.SplitN(key, []byte("||"), 2)
parts := bytes.SplitN(sliceCopy(key), []byte("||"), 2)
service = string(parts[1])
// Setup the entry
@ -417,7 +416,7 @@ func (s *StateStore) Services() map[string][]string {
} else if err != nil {
panic(fmt.Errorf("Failed to get services: %v", err))
}
parts := bytes.SplitN(key, []byte("||"), 3)
parts := bytes.SplitN(sliceCopy(key), []byte("||"), 3)
service := string(parts[0])
tag := string(parts[1])
@ -520,7 +519,7 @@ func (s *StateSnapshot) Nodes() []string {
} else if err != nil {
panic(fmt.Errorf("Failed to get nodes: %v", err))
}
nodes = append(nodes, string(key), string(val))
nodes = append(nodes, string(sliceCopy(key)), string(sliceCopy(val)))
}
return nodes
}
@ -529,3 +528,9 @@ func (s *StateSnapshot) Nodes() []string {
func (s *StateSnapshot) NodeServices(name string) rpc.NodeServices {
return filterNodeServices(s.tx, s.dbis[1], name)
}
func sliceCopy(in []byte) []byte {
c := make([]byte, len(in))
copy(c, in)
return c
}