Skip to content

Commit 5935114

Browse files
committed
remove mitigations for receiving under-length digests from the server (now fixed at server)
1 parent 3e3386c commit 5935114

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/StackExchange.Redis/ResultProcessor.Digest.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,28 @@ private sealed class DigestProcessor : ResultProcessor<ValueCondition?>
1313
{
1414
protected override bool SetResultCore(PhysicalConnection connection, Message message, in RawResult result)
1515
{
16-
if (result.IsNull)
16+
if (result.IsNull) // for example, key doesn't exist
1717
{
1818
SetResult(message, null);
1919
return true;
2020
}
2121

22-
switch (result.Resp2TypeBulkString)
22+
if (result.Resp2TypeBulkString == ResultType.BulkString
23+
&& result.Payload is { Length: 2 * ValueCondition.DigestBytes } payload)
2324
{
24-
case ResultType.BulkString:
25-
var payload = result.Payload;
26-
var len = checked((int)payload.Length);
27-
if (len == 2 * ValueCondition.DigestBytes & payload.IsSingleSegment)
28-
{
29-
// full-size hash in a single chunk - fast path
30-
SetResult(message, ValueCondition.ParseDigest(payload.First.Span));
31-
return true;
32-
}
33-
34-
if (len >= 1 & len <= ValueCondition.DigestBytes * 2)
35-
{
36-
// Either multi-segment, or isn't long enough (missing leading zeros,
37-
// see https://github.com/redis/redis/issues/14496).
38-
Span<byte> buffer = new byte[2 * ValueCondition.DigestBytes];
39-
int start = (2 * ValueCondition.DigestBytes) - len;
40-
if (start != 0) buffer.Slice(0, start).Fill((byte)'0'); // pad
41-
payload.CopyTo(buffer.Slice(start)); // linearize
42-
SetResult(message, ValueCondition.ParseDigest(buffer));
43-
return true;
44-
}
45-
break;
25+
ValueCondition digest;
26+
if (payload.IsSingleSegment) // single chunk - fast path
27+
{
28+
digest = ValueCondition.ParseDigest(payload.First.Span);
29+
}
30+
else // linearize
31+
{
32+
Span<byte> buffer = stackalloc byte[2 * ValueCondition.DigestBytes];
33+
payload.CopyTo(buffer);
34+
digest = ValueCondition.ParseDigest(buffer);
35+
}
36+
SetResult(message, digest);
37+
return true;
4638
}
4739
return false;
4840
}

0 commit comments

Comments
 (0)