Skip to content

Commit

Permalink
[mcs] Check type of index used in pointer array aritmetic. Fixes #17145
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Jan 10, 2014
1 parent 427d242 commit c92e96e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
12 changes: 12 additions & 0 deletions mcs/errors/cs0029-35.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// CS0029: Cannot implicitly convert type `string' to `int'
// Line: 10
// Compiler options: -unsafe

class TestClass
{
public unsafe static void Main ()
{
int* arr = null;
var i = arr["c"];
}
}
12 changes: 12 additions & 0 deletions mcs/errors/cs0220-4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// CS0220: The operation overflows at compile time in checked mode
// Line: 10
// Compiler options: -unsafe

class TestClass
{
public unsafe static void Main ()
{
int* arr = null;
var i = arr[long.MaxValue];
}
}
5 changes: 4 additions & 1 deletion mcs/mcs/ecore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ public static void UnsafeError (Report Report, Location loc)
//
// Converts `source' to an int, uint, long or ulong.
//
protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expression source)
protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expression source, bool pointerArray = false)
{
var btypes = ec.BuiltinTypes;

Expand All @@ -1085,6 +1085,9 @@ protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expressio
}
}

if (pointerArray)
return converted;

//
// Only positive constants are allowed at compile time
//
Expand Down
17 changes: 12 additions & 5 deletions mcs/mcs/expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9306,17 +9306,24 @@ public override Expression CreateExpressionTree (ResolveContext ec)
return CreateExpressionFactoryCall (ec, "ArrayIndex", args);
}

Expression MakePointerAccess (ResolveContext ec, TypeSpec type)
Expression MakePointerAccess (ResolveContext rc, TypeSpec type)
{
if (Arguments.Count != 1){
ec.Report.Error (196, loc, "A pointer must be indexed by only one value");
rc.Report.Error (196, loc, "A pointer must be indexed by only one value");
return null;
}

if (Arguments [0] is NamedArgument)
Error_NamedArgument ((NamedArgument) Arguments[0], ec.Report);
var arg = Arguments[0];
if (arg is NamedArgument)
Error_NamedArgument ((NamedArgument) arg, rc.Report);

Expression p = new PointerArithmetic (Binary.Operator.Addition, Expr, Arguments [0].Expr.Resolve (ec), type, loc);
var index = arg.Expr.Resolve (rc);
if (index == null)
return null;

index = ConvertExpressionToArrayIndex (rc, index, true);

Expression p = new PointerArithmetic (Binary.Operator.Addition, Expr, index, type, loc);
return new Indirection (p, loc);
}

Expand Down

0 comments on commit c92e96e

Please sign in to comment.