Skip to content

Commit 3c42ec4

Browse files
author
ZhangTao1596
committed
StateService: add key length check
1 parent 334da52 commit 3c42ec4

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

src/StateService/MPT/MPTTrie.Delete.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ partial class MPTTrie<TKey, TValue>
1010
public bool Delete(TKey key)
1111
{
1212
var path = ToNibbles(key.ToArray());
13-
if (path.Length == 0) throw new ArgumentException("could not be empty", nameof(key));
13+
if (path.Length == 0)
14+
throw new ArgumentException("could not be empty", nameof(key));
15+
if (path.Length > MPTNode.MaxKeyLength)
16+
throw new ArgumentException("exceeds limit", nameof(key));
1417
return TryDelete(ref root, path);
1518
}
1619

src/StateService/MPT/MPTTrie.Find.cs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ private ReadOnlySpan<byte> Seek(ref MPTNode node, ReadOnlySpan<byte> path, out M
7474
offset = prefix.Length * 2;
7575
from = ToNibbles(from.AsSpan());
7676
}
77+
if (path.Length > MPTNode.MaxKeyLength || from.Length > MPTNode.MaxKeyLength)
78+
throw new ArgumentException("exceeds limit");
7779
path = Seek(ref root, path, out MPTNode start).ToArray();
7880
return Travers(start, path, from, offset)
7981
.Select(p => (FromNibbles(p.Key).AsSerializable<TKey>(), p.Value.AsSerializable<TValue>()));

src/StateService/MPT/MPTTrie.Get.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public TValue this[TKey key]
1111
get
1212
{
1313
var path = ToNibbles(key.ToArray());
14-
if (path.Length == 0) throw new ArgumentException("could not be empty", nameof(key));
14+
if (path.Length == 0)
15+
throw new ArgumentException("could not be empty", nameof(key));
16+
if (path.Length > MPTNode.MaxKeyLength)
17+
throw new ArgumentException("exceeds limit", nameof(key));
1518
var result = TryGet(ref root, path, out var value);
1619
return result ? value.ToArray().AsSerializable<TValue>() : throw new KeyNotFoundException();
1720
}
@@ -21,7 +24,10 @@ public bool TryGetValue(TKey key, out TValue value)
2124
{
2225
value = default;
2326
var path = ToNibbles(key.ToArray());
24-
if (path.Length == 0) throw new ArgumentException("could not be empty", nameof(key));
27+
if (path.Length == 0)
28+
throw new ArgumentException("could not be empty", nameof(key));
29+
if (path.Length > MPTNode.MaxKeyLength)
30+
throw new ArgumentException("exceeds limit", nameof(key));
2531
var result = TryGet(ref root, path, out var val);
2632
if (result)
2733
val.ToArray().AsSerializable<TValue>();

src/StateService/MPT/MPTTrie.Proof.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ partial class MPTTrie<TKey, TValue>
1313
public bool TryGetProof(TKey key, out HashSet<byte[]> proof)
1414
{
1515
var path = ToNibbles(key.ToArray());
16-
if (path.Length == 0) throw new ArgumentException("could not be empty", nameof(key));
16+
if (path.Length == 0)
17+
throw new ArgumentException("could not be empty", nameof(key));
18+
if (path.Length > MPTNode.MaxKeyLength)
19+
throw new ArgumentException("exceeds limit", nameof(key));
1720
proof = new HashSet<byte[]>(ByteArrayEqualityComparer.Default);
1821
return GetProof(ref root, path, proof);
1922
}

0 commit comments

Comments
 (0)