Skip to content

Commit af2d10f

Browse files
committed
bugfix: protect addresses from external changes
We need to use copies of slices, not just pointers to them. It helps to avoid unexpected changes.
1 parent 7b29b6b commit af2d10f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

connection_pool/connection_pool.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
9696
anyPool := NewEmptyRoundRobin(size)
9797

9898
connPool = &ConnectionPool{
99-
addrs: addrs,
99+
addrs: make([]string, len(addrs)),
100100
connOpts: connOpts,
101101
opts: opts,
102102
notify: notify,
@@ -105,6 +105,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
105105
roPool: roPool,
106106
anyPool: anyPool,
107107
}
108+
copy(connPool.addrs, addrs)
108109

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

179180
// GetAddrs gets addresses of connections in pool.
180181
func (connPool *ConnectionPool) GetAddrs() []string {
181-
return connPool.addrs
182+
cpy := make([]string, len(connPool.addrs))
183+
copy(cpy, connPool.addrs)
184+
return cpy
182185
}
183186

184187
// GetPoolInfo gets information of connections (connected status, ro/rw role).

connection_pool/connection_pool_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ func TestClose(t *testing.T) {
208208
require.Nil(t, err)
209209
}
210210

211+
func TestGetPoolInfo(t *testing.T) {
212+
server1 := servers[0]
213+
server2 := servers[1]
214+
215+
srvs := []string{server1, server2}
216+
expected := []string{server1, server2}
217+
connPool, err := connection_pool.Connect(srvs, connOpts)
218+
require.Nilf(t, err, "failed to connect")
219+
require.NotNilf(t, connPool, "conn is nil after Connect")
220+
221+
defer connPool.Close()
222+
223+
srvs[0] = "x"
224+
connPool.GetAddrs()[1] = "y"
225+
for i, addr := range connPool.GetAddrs() {
226+
require.Equal(t, expected[i], addr)
227+
}
228+
}
229+
211230
func TestCall17(t *testing.T) {
212231
roles := []bool{false, true, false, false, true}
213232

0 commit comments

Comments
 (0)