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: ensure the value in range #828

Merged
merged 14 commits into from
Dec 15, 2023
Merged
51 changes: 26 additions & 25 deletions src/Neo.Compiler.CSharp/MethodConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2849,7 +2849,7 @@ private void ConvertElementAccessPostIncrementOrDecrementExpression(SemanticMode
AddInstruction(OpCode.DUP);
AddInstruction(OpCode.REVERSE4);
AddInstruction(OpCode.REVERSE3);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, property.Type);
Call(model, property.SetMethod!, CallingConvention.StdCall);
}
else
Expand All @@ -2862,7 +2862,7 @@ private void ConvertElementAccessPostIncrementOrDecrementExpression(SemanticMode
AddInstruction(OpCode.DUP);
AddInstruction(OpCode.REVERSE4);
AddInstruction(OpCode.REVERSE3);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, model.GetTypeInfo(operand).Type);
AddInstruction(OpCode.SETITEM);
}
}
Expand Down Expand Up @@ -2896,7 +2896,7 @@ private void ConvertFieldIdentifierNamePostIncrementOrDecrementExpression(Syntax
byte index = context.AddStaticField(symbol);
AccessSlot(OpCode.LDSFLD, index);
AddInstruction(OpCode.DUP);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AccessSlot(OpCode.STSFLD, index);
}
else
Expand All @@ -2907,7 +2907,7 @@ private void ConvertFieldIdentifierNamePostIncrementOrDecrementExpression(Syntax
Push(index);
AddInstruction(OpCode.PICKITEM);
AddInstruction(OpCode.TUCK);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
Push(index);
AddInstruction(OpCode.SWAP);
AddInstruction(OpCode.SETITEM);
Expand All @@ -2919,7 +2919,7 @@ private void ConvertLocalIdentifierNamePostIncrementOrDecrementExpression(Syntax
byte index = _localVariables[symbol];
AccessSlot(OpCode.LDLOC, index);
AddInstruction(OpCode.DUP);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AccessSlot(OpCode.STLOC, index);
}

Expand All @@ -2928,7 +2928,7 @@ private void ConvertParameterIdentifierNamePostIncrementOrDecrementExpression(Sy
byte index = _parameters[symbol];
AccessSlot(OpCode.LDARG, index);
AddInstruction(OpCode.DUP);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AccessSlot(OpCode.STARG, index);
}

Expand All @@ -2938,7 +2938,7 @@ private void ConvertPropertyIdentifierNamePostIncrementOrDecrementExpression(Sem
{
Call(model, symbol.GetMethod!);
AddInstruction(OpCode.DUP);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
Call(model, symbol.SetMethod!);
}
else
Expand All @@ -2947,7 +2947,7 @@ private void ConvertPropertyIdentifierNamePostIncrementOrDecrementExpression(Sem
AddInstruction(OpCode.DUP);
Call(model, symbol.GetMethod!);
AddInstruction(OpCode.TUCK);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
Call(model, symbol.SetMethod!, CallingConvention.StdCall);
}
}
Expand Down Expand Up @@ -2975,7 +2975,7 @@ private void ConvertFieldMemberAccessPostIncrementOrDecrementExpression(Semantic
byte index = context.AddStaticField(symbol);
AccessSlot(OpCode.LDSFLD, index);
AddInstruction(OpCode.DUP);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AccessSlot(OpCode.STSFLD, index);
}
else
Expand All @@ -2986,7 +2986,7 @@ private void ConvertFieldMemberAccessPostIncrementOrDecrementExpression(Semantic
Push(index);
AddInstruction(OpCode.PICKITEM);
AddInstruction(OpCode.TUCK);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
Push(index);
AddInstruction(OpCode.SWAP);
AddInstruction(OpCode.SETITEM);
Expand All @@ -2999,7 +2999,7 @@ private void ConvertPropertyMemberAccessPostIncrementOrDecrementExpression(Seman
{
Call(model, symbol.GetMethod!);
AddInstruction(OpCode.DUP);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
Call(model, symbol.SetMethod!);
}
else
Expand All @@ -3008,7 +3008,7 @@ private void ConvertPropertyMemberAccessPostIncrementOrDecrementExpression(Seman
AddInstruction(OpCode.DUP);
Call(model, symbol.GetMethod!);
AddInstruction(OpCode.TUCK);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
Call(model, symbol.SetMethod!, CallingConvention.StdCall);
}
}
Expand Down Expand Up @@ -3076,7 +3076,7 @@ private void ConvertElementAccessPreIncrementOrDecrementExpression(SemanticModel
AddInstruction(OpCode.OVER);
AddInstruction(OpCode.OVER);
Call(model, property.GetMethod!, CallingConvention.StdCall);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, property.Type);
AddInstruction(OpCode.DUP);
AddInstruction(OpCode.REVERSE4);
Call(model, property.SetMethod!, CallingConvention.Cdecl);
Expand All @@ -3088,7 +3088,7 @@ private void ConvertElementAccessPreIncrementOrDecrementExpression(SemanticModel
AddInstruction(OpCode.OVER);
AddInstruction(OpCode.OVER);
AddInstruction(OpCode.PICKITEM);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, model.GetTypeInfo(operand).Type);
AddInstruction(OpCode.DUP);
AddInstruction(OpCode.REVERSE4);
AddInstruction(OpCode.REVERSE3);
Expand Down Expand Up @@ -3124,7 +3124,7 @@ private void ConvertFieldIdentifierNamePreIncrementOrDecrementExpression(SyntaxT
{
byte index = context.AddStaticField(symbol);
AccessSlot(OpCode.LDSFLD, index);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.DUP);
AccessSlot(OpCode.STSFLD, index);
}
Expand All @@ -3135,7 +3135,7 @@ private void ConvertFieldIdentifierNamePreIncrementOrDecrementExpression(SyntaxT
AddInstruction(OpCode.DUP);
Push(index);
AddInstruction(OpCode.PICKITEM);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.TUCK);
Push(index);
AddInstruction(OpCode.SWAP);
Expand All @@ -3147,7 +3147,7 @@ private void ConvertLocalIdentifierNamePreIncrementOrDecrementExpression(SyntaxT
{
byte index = _localVariables[symbol];
AccessSlot(OpCode.LDLOC, index);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.DUP);
AccessSlot(OpCode.STLOC, index);
}
Expand All @@ -3156,7 +3156,7 @@ private void ConvertParameterIdentifierNamePreIncrementOrDecrementExpression(Syn
{
byte index = _parameters[symbol];
AccessSlot(OpCode.LDARG, index);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.DUP);
AccessSlot(OpCode.STARG, index);
}
Expand All @@ -3166,7 +3166,7 @@ private void ConvertPropertyIdentifierNamePreIncrementOrDecrementExpression(Sema
if (symbol.IsStatic)
{
Call(model, symbol.GetMethod!);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.DUP);
Call(model, symbol.SetMethod!);
}
Expand All @@ -3175,7 +3175,7 @@ private void ConvertPropertyIdentifierNamePreIncrementOrDecrementExpression(Sema
AddInstruction(OpCode.LDARG0);
AddInstruction(OpCode.DUP);
Call(model, symbol.GetMethod!);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.TUCK);
Call(model, symbol.SetMethod!, CallingConvention.StdCall);
}
Expand Down Expand Up @@ -3203,7 +3203,7 @@ private void ConvertFieldMemberAccessPreIncrementOrDecrementExpression(SemanticM
{
byte index = context.AddStaticField(symbol);
AccessSlot(OpCode.LDSFLD, index);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.DUP);
AccessSlot(OpCode.STSFLD, index);
}
Expand All @@ -3214,7 +3214,7 @@ private void ConvertFieldMemberAccessPreIncrementOrDecrementExpression(SemanticM
AddInstruction(OpCode.DUP);
Push(index);
AddInstruction(OpCode.PICKITEM);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.TUCK);
Push(index);
AddInstruction(OpCode.SWAP);
Expand All @@ -3227,7 +3227,7 @@ private void ConvertPropertyMemberAccessPreIncrementOrDecrementExpression(Semant
if (symbol.IsStatic)
{
Call(model, symbol.GetMethod!);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.DUP);
Call(model, symbol.SetMethod!);
}
Expand All @@ -3236,20 +3236,21 @@ private void ConvertPropertyMemberAccessPreIncrementOrDecrementExpression(Semant
ConvertExpression(model, operand.Expression);
AddInstruction(OpCode.DUP);
Call(model, symbol.GetMethod!);
EmitIncrementOrDecrement(operatorToken);
EmitIncrementOrDecrement(operatorToken, symbol.Type);
AddInstruction(OpCode.TUCK);
Call(model, symbol.SetMethod!, CallingConvention.StdCall);
}
}

private void EmitIncrementOrDecrement(SyntaxToken operatorToken)
private void EmitIncrementOrDecrement(SyntaxToken operatorToken, ITypeSymbol? typeSymbol)
{
AddInstruction(operatorToken.ValueText switch
{
"++" => OpCode.INC,
"--" => OpCode.DEC,
_ => throw new CompilationException(operatorToken, DiagnosticId.SyntaxNotSupported, $"Unsupported operator: {operatorToken}")
});
if (typeSymbol != null) EnsureIntegerInRange(typeSymbol);
}

private void ConvertSwitchExpression(SemanticModel model, SwitchExpressionSyntax expression)
Expand Down
Loading