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 @@ -260,5 +260,9 @@ launchSettings.json
/coverages
**/.DS_Store

# UT coverage report
/coverages
**/.DS_Store

cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved
# Benchmarks
**/BenchmarkDotNet.Artifacts/
2 changes: 1 addition & 1 deletion src/Neo.VM/JumpTable/JumpTable.Splice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public virtual void SubStr(ExecutionEngine engine, Instruction instruction)
if (index < 0)
throw new InvalidOperationException($"The index can not be negative for {nameof(OpCode.SUBSTR)}, index: {index}.");
var x = engine.Pop().GetSpan();
if (index + count > x.Length)
if (checked(index + count) > x.Length)
shargon marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidOperationException($"The index + count is out of range for {nameof(OpCode.SUBSTR)}, index: {index}, count: {count}, {index + count}/[0, {x.Length}].");
Types.Buffer result = new(count, false);
x.Slice(index, count).CopyTo(result.InnerBuffer.Span);
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