Skip to content

Conversation

@Jim8y
Copy link
Contributor

@Jim8y Jim8y commented Dec 7, 2025

Summary

  • add parse helper and runtime parsing for UInt160/UInt256/ECPoint with validation
  • update compiler/analyzer/tests to use Parse for constants and runtime parsing fallbacks
  • refresh security docs with Parse-based examples and new parse unit tests

Testing

  • dotnet test tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj -c Release
  • dotnet test tests/Neo.SmartContract.Analyzer.UnitTests/Neo.SmartContract.Analyzer.UnitTests.csproj -c Release
  • dotnet test neo-devpack-dotnet.sln -c Release (started; main suites pass; compiler unit tests may need rerun if desired)


if (list.Count == 0)
EnsureCoverageInternal(Assembly.GetExecutingAssembly(), CachedContracts.Select(u => (u.Key, u.Value.DbgInfo)));
EnsureCoverageInternal(Assembly.GetExecutingAssembly(), CachedContracts.Select(u => (u.Key, u.Value.DbgInfo)), 0.89M);
Copy link
Member

Choose a reason for hiding this comment

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

Is not full covered now?

return valueBytes[0] == (byte)'0' && (valueBytes[1] == (byte)'x' || valueBytes[1] == (byte)'X') ? 2 : 0;
}

internal static byte[] ParseHex(byte[] valueBytes, int offset, int expectedLength)
Copy link
Member

Choose a reason for hiding this comment

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

I think we only use full read, or I miss something? It will be clear a regular ParseHex(byte[] data)

if (value >= (byte)'0' && value <= (byte)'9') return (byte)(value - (byte)'0');
if (value >= (byte)'a' && value <= (byte)'f') return (byte)(value - (byte)'a' + 10);
if (value >= (byte)'A' && value <= (byte)'F') return (byte)(value - (byte)'A' + 10);
throw new FormatException("Invalid hex character.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
throw new FormatException("Invalid hex character.");
throw new FormatException("Invalid hex character: ${value}.");

Comment on lines +25 to +40
internal static byte[] ParseHex(byte[] valueBytes, int offset, int expectedLength)
{
int hexLength = valueBytes.Length - offset;
if (hexLength != expectedLength * 2)
throw new FormatException("Invalid hex length.");

byte[] result = new byte[expectedLength];
for (int i = 0; i < expectedLength; i++)
{
byte high = FromHexByte(valueBytes[offset + i * 2]);
byte low = FromHexByte(valueBytes[offset + i * 2 + 1]);
result[i] = (byte)((high << 4) | low);
}

return result;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
internal static byte[] ParseHex(byte[] valueBytes, int offset, int expectedLength)
{
int hexLength = valueBytes.Length - offset;
if (hexLength != expectedLength * 2)
throw new FormatException("Invalid hex length.");
byte[] result = new byte[expectedLength];
for (int i = 0; i < expectedLength; i++)
{
byte high = FromHexByte(valueBytes[offset + i * 2]);
byte low = FromHexByte(valueBytes[offset + i * 2 + 1]);
result[i] = (byte)((high << 4) | low);
}
return result;
}
internal static byte[] ParseHex(byte[] valueBytes, int offset, int expectedLength)
{
int hexLength = valueBytes.Length - offset;
if (hexLength != expectedLength * 2)
throw new FormatException("Invalid hex length.");
return Convert.FromHexString(valueBytes.AsSpan()[offset..]);
}

This is better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UInt160 type method in smart contract can return String, can be compiled, but can't be executed.

4 participants