Skip to content

Commit

Permalink
bugfix: protect addresses from external changes
Browse files Browse the repository at this point in the history
We need to use copies of slices, not just pointers to them. It helps
to avoid unexpected changes.

Part of #208
  • Loading branch information
oleg-jukovec committed Aug 24, 2022
1 parent 2fc7566 commit b977961
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Missed Role type constants in the connection_pool subpackage (#208)
- ConnectionPool does not close UnknownRole connections (#208)
- Segmentation faults in ConnectionPool requests after disconnect (#208)
- Addresses in ConnectionPool may be changed from an external code (#208)

## [1.8.0] - 2022-08-17

Expand Down
7 changes: 5 additions & 2 deletions connection_pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
anyPool := NewEmptyRoundRobin(size)

connPool = &ConnectionPool{
addrs: addrs,
addrs: make([]string, len(addrs)),
connOpts: connOpts,
opts: opts,
notify: notify,
Expand All @@ -105,6 +105,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
roPool: roPool,
anyPool: anyPool,
}
copy(connPool.addrs, addrs)

somebodyAlive := connPool.fillPools()
if !somebodyAlive {
Expand Down Expand Up @@ -178,7 +179,9 @@ func (connPool *ConnectionPool) Close() []error {

// GetAddrs gets addresses of connections in pool.
func (connPool *ConnectionPool) GetAddrs() []string {
return connPool.addrs
cpy := make([]string, len(connPool.addrs))
copy(cpy, connPool.addrs)
return cpy
}

// GetPoolInfo gets information of connections (connected status, ro/rw role).
Expand Down
19 changes: 19 additions & 0 deletions connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ func TestRequestOnClosed(t *testing.T) {
require.Nilf(t, err, "failed to restart tarantool")
}

func TestGetPoolInfo(t *testing.T) {
server1 := servers[0]
server2 := servers[1]

srvs := []string{server1, server2}
expected := []string{server1, server2}
connPool, err := connection_pool.Connect(srvs, connOpts)
require.Nilf(t, err, "failed to connect")
require.NotNilf(t, connPool, "conn is nil after Connect")

defer connPool.Close()

srvs[0] = "x"
connPool.GetAddrs()[1] = "y"
for i, addr := range connPool.GetAddrs() {
require.Equal(t, expected[i], addr)
}
}

func TestCall17(t *testing.T) {
roles := []bool{false, true, false, false, true}

Expand Down

0 comments on commit b977961

Please sign in to comment.