Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b9c306a

Browse files
committed
Add guards around array index
1 parent 94c379e commit b9c306a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/ServiceStack.Redis/RedisManagerPool.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ public IRedisClient GetClient()
141141
try
142142
{
143143
//inactivePoolIndex == -1 || index of reservedSlot || index of invalid client
144-
var existingClient = inactivePoolIndex >= 0 ? clients[inactivePoolIndex] : null;
144+
var existingClient = inactivePoolIndex >= 0 && inactivePoolIndex < clients.Length
145+
? clients[inactivePoolIndex]
146+
: null;
147+
145148
if (existingClient != null && existingClient != reservedSlot && existingClient.HadExceptions)
146149
{
147150
RedisState.DeactivateClient(existingClient);
@@ -155,10 +158,12 @@ public IRedisClient GetClient()
155158
//Create new client outside of pool when max pool size exceeded
156159
//Reverting free-slot not needed when -1 since slwo wasn't reserved or
157160
//when existingClient changed (failover) since no longer reserver
158-
if (inactivePoolIndex == -1 || clients[inactivePoolIndex] != existingClient)
161+
var stillReserved = inactivePoolIndex >= 0 && inactivePoolIndex < clients.Length &&
162+
clients[inactivePoolIndex] == existingClient;
163+
if (inactivePoolIndex == -1 || !stillReserved)
159164
{
160165
if (Log.IsDebugEnabled)
161-
Log.Debug("clients[inactivePoolIndex] != existingClient: {0}".Fmt(clients[inactivePoolIndex]));
166+
Log.Debug("clients[inactivePoolIndex] != existingClient: {0}".Fmt(!stillReserved ? "!stillReserved" : "-1"));
162167

163168
Interlocked.Increment(ref RedisState.TotalClientsCreatedOutsidePool);
164169

@@ -177,7 +182,10 @@ public IRedisClient GetClient()
177182
//Revert free-slot for any I/O exceptions that can throw (before lock)
178183
lock (clients)
179184
{
180-
clients[inactivePoolIndex] = null;
185+
if (inactivePoolIndex >= 0 && inactivePoolIndex < clients.Length)
186+
{
187+
clients[inactivePoolIndex] = null;
188+
}
181189
}
182190
throw;
183191
}

0 commit comments

Comments
 (0)