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 5 commits into
base: HF_Echidna
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,10 @@ PublishProfiles
/.vscode
launchSettings.json

# UT coverage report
/coverages
**/.DS_Store

# Benchmarks
**/BenchmarkDotNet.Artifacts/
**/BenchmarkDotNet.Artifacts/

11 changes: 7 additions & 4 deletions src/Neo.VM/JumpTable/JumpTable.Splice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ 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 (checked(index + count) > x.Length)
shargon marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidOperationException($"The value of index {index} + count {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