Skip to content

Commit 8ed7a1c

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. Part of #208
1 parent 5f42a1d commit 8ed7a1c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1818
- Missed Role type constants in the connection_pool subpackage (#208)
1919
- ConnectionPool does not close UnknownRole connections (#208)
2020
- Segmentation faults in ConnectionPool requests after disconnect (#208)
21+
- Addresses in ConnectionPool may be changed from an external code (#208)
2122

2223
## [1.8.0] - 2022-08-17
2324

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
@@ -244,6 +244,25 @@ func TestRequestOnClosed(t *testing.T) {
244244
require.Nilf(t, err, "failed to restart tarantool")
245245
}
246246

247+
func TestGetPoolInfo(t *testing.T) {
248+
server1 := servers[0]
249+
server2 := servers[1]
250+
251+
srvs := []string{server1, server2}
252+
expected := []string{server1, server2}
253+
connPool, err := connection_pool.Connect(srvs, connOpts)
254+
require.Nilf(t, err, "failed to connect")
255+
require.NotNilf(t, connPool, "conn is nil after Connect")
256+
257+
defer connPool.Close()
258+
259+
srvs[0] = "x"
260+
connPool.GetAddrs()[1] = "y"
261+
for i, addr := range connPool.GetAddrs() {
262+
require.Equal(t, expected[i], addr)
263+
}
264+
}
265+
247266
func TestCall17(t *testing.T) {
248267
roles := []bool{false, true, false, false, true}
249268

0 commit comments

Comments
 (0)