Skip to content

Commit 4200fac

Browse files
authored
Consider type boundaries (#10000)
1 parent 33397b2 commit 4200fac

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/Nethermind/Nethermind.Core/Extensions/ByteArrayExtensions.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
1+
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using System;
@@ -45,6 +45,7 @@ public static byte[] Slice(this byte[] bytes, int startIndex, int length)
4545
public static byte[] SliceWithZeroPaddingEmptyOnError(this byte[] bytes, int startIndex, int length)
4646
{
4747
int copiedFragmentLength = Math.Min(bytes.Length - startIndex, length);
48+
4849
if (copiedFragmentLength <= 0)
4950
{
5051
return [];
@@ -59,21 +60,44 @@ public static byte[] SliceWithZeroPaddingEmptyOnError(this byte[] bytes, int sta
5960
public static ReadOnlySpan<byte> SliceWithZeroPaddingEmptyOnError(this ReadOnlySpan<byte> bytes, int startIndex, int length)
6061
{
6162
int copiedFragmentLength = Math.Min(bytes.Length - startIndex, length);
63+
6264
if (copiedFragmentLength <= 0)
6365
{
6466
return default;
6567
}
6668

69+
return SafeSliceWithZeroPadding(bytes, startIndex, length, copiedFragmentLength);
70+
}
71+
72+
public static ReadOnlySpan<byte> SliceWithZeroPaddingEmptyOnError(this ReadOnlySpan<byte> bytes, uint startIndex, uint length)
73+
{
74+
if (bytes.Length < startIndex)
75+
{
76+
return default;
77+
}
78+
79+
long copiedFragmentLength = Math.Min((uint)bytes.Length - startIndex, length);
80+
81+
if (bytes.Length < startIndex + copiedFragmentLength)
82+
{
83+
return default;
84+
}
85+
86+
return SafeSliceWithZeroPadding(bytes, (int)startIndex, (int)length, (int)copiedFragmentLength);
87+
}
88+
89+
private static ReadOnlySpan<byte> SafeSliceWithZeroPadding(ReadOnlySpan<byte> bytes, int startIndex, int length, int copiedFragmentLength)
90+
{
6791
ReadOnlySpan<byte> sliced = bytes.Slice(startIndex, copiedFragmentLength);
92+
6893
if (copiedFragmentLength < length)
6994
{
7095
byte[] extended = new byte[length];
7196
sliced.CopyTo(extended);
72-
sliced = extended;
97+
return extended;
7398
}
7499

75100
return sliced;
76101
}
77-
78102
}
79103
}

src/Nethermind/Nethermind.Evm.Precompiles/ModExpPrecompile.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,16 @@ private static long DataGasCostInternal(ReadOnlySpan<byte> inputData, IReleaseSp
9393
ulong complexity = MultComplexity(baseLength, modulusLength, releaseSpec.IsEip7883Enabled);
9494

9595
uint expLengthUpTo32 = Math.Min(LengthSize, expLength);
96-
uint startIndex = LengthsLengths + baseLength; //+ expLength - expLengthUpTo32; // Geth takes head here, why?
97-
UInt256 exp = new(inputData.SliceWithZeroPaddingEmptyOnError((int)startIndex, (int)expLengthUpTo32), isBigEndian: true);
96+
97+
ReadOnlySpan<byte> data = [];
98+
99+
if (baseLength < uint.MaxValue - LengthsLengths)
100+
{
101+
uint startIndex = LengthsLengths + baseLength; //+ expLength - expLengthUpTo32; // Geth takes head here, why?
102+
data = inputData.SliceWithZeroPaddingEmptyOnError(startIndex, expLengthUpTo32);
103+
}
104+
105+
UInt256 exp = new(data, isBigEndian: true);
98106
UInt256 iterationCount = CalculateIterationCount(expLength, exp, releaseSpec.IsEip7883Enabled);
99107

100108
bool overflow = UInt256.MultiplyOverflow(complexity, iterationCount, out UInt256 result);
@@ -113,7 +121,7 @@ private static bool ExceedsMaxInputSize(IReleaseSpec releaseSpec, uint baseLengt
113121
{
114122
return releaseSpec.IsEip7823Enabled
115123
? (baseLength > ModExpMaxInputSizeEip7823 | expLength > ModExpMaxInputSizeEip7823 | modulusLength > ModExpMaxInputSizeEip7823)
116-
: (baseLength | modulusLength) > int.MaxValue;
124+
: (baseLength | modulusLength) >= uint.MaxValue;
117125
}
118126

119127
[MethodImpl(MethodImplOptions.NoInlining)]

0 commit comments

Comments
 (0)