blob: c88ac8885e5c075d882071ae3aa485b599ff5091 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// Compiler options: -unsafe
using System;
class PointerArithmeticTest
{
unsafe public static int Main()
{
try {
return CheckAdd((byte*)(-1), -1);
} catch (System.OverflowException) {}
try {
if (IntPtr.Size <= 4)
return CheckSub((short*)(-1), int.MaxValue);
else
return CheckSub((short*)(-1), long.MaxValue);
} catch (System.OverflowException) {}
CheckSub2((short*)(-1), int.MaxValue);
if ((long)Conversions (long.MaxValue) != (IntPtr.Size <= 4 ? uint.MaxValue : long.MaxValue))
return 5;
Console.WriteLine ("OK");
return 0;
}
unsafe static int* Conversions (long b)
{
return (int*)b;
}
unsafe static int CheckAdd(byte* ptr, int offset)
{
if (checked(ptr + offset < ptr))
return 1;
return 101;
}
unsafe static int CheckSub(short* ptr, int offset)
{
if (checked(ptr - offset < ptr))
return 2;
return 102;
}
unsafe static int CheckSub(short* ptr, long offset)
{
if (checked(ptr - offset < ptr))
return 2;
return 102;
}
unsafe static int CheckSub2(short* ptr, int offset)
{
short* b = ptr + offset;
if (checked(ptr - b < 0))
return 3;
return 0;
}
}
|