Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix]: integer overflow in JumpTable.SubStr #3496

Open
wants to merge 14 commits into
base: HF_Echidna
Choose a base branch
from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,7 @@ paket-files/
PublishProfiles
/.vscode
launchSettings.json

# UT coverage report
/coverages
**/.DS_Store
17 changes: 13 additions & 4 deletions src/Neo.VM/JumpTable/JumpTable.Splice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,22 @@ public virtual void SubStr(ExecutionEngine engine, Instruction instruction)
{
shargon marked this conversation as resolved.
Show resolved Hide resolved
int count = (int)engine.Pop().GetInteger();
if (count < 0)
throw new InvalidOperationException($"The value {count} is out of range.");
throw new InvalidOperationException($"The value of count {count} is out of range.");

int index = (int)engine.Pop().GetInteger();
if (index < 0)
throw new InvalidOperationException($"The value {index} is out of range.");
throw new InvalidOperationException($"The value of index {index} is out of range.");

var x = engine.Pop().GetSpan();
if (index + count > x.Length)
throw new InvalidOperationException($"The value {count} is out of range.");
if (count > x.Length)
throw new InvalidOperationException($"The value of count {count} is out of range.");

if (index >= x.Length)
throw new InvalidOperationException($"The value of index {index} is out of range.");

if (checked(index + count) > x.Length)
shargon marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidOperationException($"The value of index + count {index + count} is out of range.");

Types.Buffer result = new(count, false);
x.Slice(index, count).CopyTo(result.InnerBuffer.Span);
engine.Push(result);
Expand Down
44 changes: 44 additions & 0 deletions tests/Neo.VM.Tests/Tests/OpCodes/Splice/SUBSTR.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,50 @@
}
}
]
},
{
"name": "Count Exceed Range Test",
roman-khimov marked this conversation as resolved.
Show resolved Hide resolved
"script": [
"PUSHDATA1",
"0x0a",
"0x00010203040506070809",
"PUSH2",
"PUSHINT32",
"0x7FFFFFFF",
"SUBSTR"
],
"steps": [
{
"actions": [
"execute"
],
"result": {
"state": "FAULT"
}
}
]
},
{
"name": "Index Exceed Range Test",
roman-khimov marked this conversation as resolved.
Show resolved Hide resolved
"script": [
"PUSHDATA1",
"0x0a",
"0x00010203040506070809",
"PUSHINT32",
"0x7FFFFFFF",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also add some tests for INT64, like:

                byte(opcode.PUSHINT64), 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
                byte(opcode.PUSH2),

It'll fail (in NeoGo it's at instruction 22 (SUBSTR): not an int32), but just to make sure.

"PUSH2",
"SUBSTR"
],
"steps": [
{
"actions": [
"execute"
],
"result": {
"state": "FAULT"
}
}
]
}
]
}
Loading