Skip to content

Commit cd8db18

Browse files
Fix NullReferenceException in HashObject.GetExpiration (#955)
fix #954 Co-authored-by: Tal Zaccai <[email protected]>
1 parent 0cd87b0 commit cd8db18

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

libs/server/Objects/Hash/HashObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ private long GetExpiration(byte[] key)
554554
return -2;
555555
}
556556

557-
if (expirationTimes.TryGetValue(key, out var expiration))
557+
if (expirationTimes is not null && expirationTimes.TryGetValue(key, out var expiration))
558558
{
559559
return expiration;
560560
}

test/Garnet.test/RespHashTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public async Task CanSetWithExpireAndRemoveExpireByCallingSetAgain()
8585
ClassicAssert.AreEqual("Tsavorite", r);
8686
}
8787

88+
// Covers the fix of #954.
89+
[Test]
90+
public void CanFieldPersistAndGetTimeToLive()
91+
{
92+
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
93+
var db = redis.GetDatabase(0);
94+
var key = "user:user1";
95+
var field = "field1";
96+
97+
db.HashSet(key, [new HashEntry(field, "v1")]);
98+
db.HashFieldExpire(key, [field], TimeSpan.FromHours(1));
99+
db.HashFieldPersist(key, [field]);
100+
var ttl = db.HashFieldGetTimeToLive(key, [field]);
101+
ClassicAssert.AreEqual(-1, ttl[0]);
102+
}
103+
88104
[Test]
89105
public void CanSetAndGetOnePairLarge()
90106
{

0 commit comments

Comments
 (0)