diff options
author | Marek Safar <marek.safar@gmail.com> | 2014-01-10 18:56:13 +0400 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2014-01-10 18:57:27 +0400 |
commit | c92e96e8502a59f7539824d832d981ed715a7e34 (patch) | |
tree | bbbb08247ca8f8d580dc1b97e3e603cbad85589f /mcs | |
parent | 427d2420090a1311982ed14e610bac961899e160 (diff) |
[mcs] Check type of index used in pointer array aritmetic. Fixes #17145
Diffstat (limited to 'mcs')
-rw-r--r-- | mcs/errors/cs0029-35.cs | 12 | ||||
-rw-r--r-- | mcs/errors/cs0220-4.cs | 12 | ||||
-rw-r--r-- | mcs/mcs/ecore.cs | 5 | ||||
-rw-r--r-- | mcs/mcs/expression.cs | 17 |
4 files changed, 40 insertions, 6 deletions
diff --git a/mcs/errors/cs0029-35.cs b/mcs/errors/cs0029-35.cs new file mode 100644 index 00000000000..c2330990f12 --- /dev/null +++ b/mcs/errors/cs0029-35.cs @@ -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"]; + } +}
\ No newline at end of file diff --git a/mcs/errors/cs0220-4.cs b/mcs/errors/cs0220-4.cs new file mode 100644 index 00000000000..c52aa545220 --- /dev/null +++ b/mcs/errors/cs0220-4.cs @@ -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]; + } +}
\ No newline at end of file diff --git a/mcs/mcs/ecore.cs b/mcs/mcs/ecore.cs index ec0d7a2c606..6f858c47944 100644 --- a/mcs/mcs/ecore.cs +++ b/mcs/mcs/ecore.cs @@ -1058,7 +1058,7 @@ namespace Mono.CSharp { // // 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; @@ -1085,6 +1085,9 @@ namespace Mono.CSharp { } } + if (pointerArray) + return converted; + // // Only positive constants are allowed at compile time // diff --git a/mcs/mcs/expression.cs b/mcs/mcs/expression.cs index dfec547a0ba..c53baf91f8a 100644 --- a/mcs/mcs/expression.cs +++ b/mcs/mcs/expression.cs @@ -9306,17 +9306,24 @@ namespace Mono.CSharp 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); } |