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: specify log exception. #3089

Merged
merged 9 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions src/Neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ public virtual VerifyResult VerifyStateIndependent(ProtocolSettings settings)

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
if (_signers == null || _signers.Length == 0) throw new ArgumentException("Sender is not specified in the transaction.");
Copy link
Member

Choose a reason for hiding this comment

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

Will this change fork something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it will still fault. Originally it fault, now it still fault, but fault with different exception, a more reasonable exception message.

return new Array(referenceCounter, new StackItem[]
{
// Computed properties
Expand Down
13 changes: 10 additions & 3 deletions src/Neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,16 @@ protected internal BigInteger GetRandom()
/// <param name="state">The message of the log.</param>
protected internal void RuntimeLog(byte[] state)
{
if (state.Length > MaxNotificationSize) throw new ArgumentException(null, nameof(state));
string message = Utility.StrictUTF8.GetString(state);
Log?.Invoke(this, new LogEventArgs(ScriptContainer, CurrentScriptHash, message));
if (state.Length > MaxNotificationSize) throw new ArgumentException("Message is too long.", nameof(state));
try
{
string message = Utility.StrictUTF8.GetString(state);
Log?.Invoke(this, new LogEventArgs(ScriptContainer, CurrentScriptHash, message));
}
catch
{
throw new ArgumentException("Failed to convert byte array to string: Invalid or non-printable UTF-8 sequence detected.", nameof(state));
Copy link
Member

Choose a reason for hiding this comment

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

Add the failed data in hex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure, cause user passes in bytes, if you show them hex, does that help at all?

Copy link
Member

Choose a reason for hiding this comment

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

It's the message, both options are good for me

}
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,17 @@ public void TestGetRandomDifferentBlock()
rand_4.Should().NotBe(rand_9);
rand_5.Should().NotBe(rand_10);
}

[TestMethod]
public void TestInvalidUtf8LogMessage()
{
var tx_1 = TestUtils.GetTransaction(UInt160.Zero);
using var engine = ApplicationEngine.Create(TriggerType.Application, tx_1, null, TestBlockchain.TheNeoSystem.GenesisBlock, settings: TestBlockchain.TheNeoSystem.Settings, gas: 1100_00000000);
var msg = new byte[]
{
68, 216, 160, 6, 89, 102, 86, 72, 37, 15, 132, 45, 76, 221, 170, 21, 128, 51, 34, 168, 205, 56, 10, 228, 51, 114, 4, 218, 245, 155, 172, 132
};
Assert.ThrowsException<ArgumentException>(() => engine.RuntimeLog(msg));
}
}
}