Skip to content

Commit 361bb17

Browse files
committed
Add configuration option to check for overflow and underflow
1 parent 7fc9025 commit 361bb17

File tree

9 files changed

+103
-12
lines changed

9 files changed

+103
-12
lines changed

ICSharpCode.Decompiler.Tests/Helpers/Tester.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public enum CompilerOptions
6969
UseTestRunner = 0x4000,
7070
NullableEnable = 0x8000,
7171
ReferenceUnsafe = 0x10000,
72+
CheckForOverflowUnderflow = 0x20000,
7273
UseMcsMask = UseMcs2_6_4 | UseMcs5_23,
7374
UseRoslynMask = UseRoslyn1_3_2 | UseRoslyn2_10_0 | UseRoslyn3_11_0 | UseRoslynLatest
7475
}
@@ -543,6 +544,16 @@ public static async Task<CompilerResults> CompileCSharp(string sourceFileName, C
543544
{
544545
otherOptions += "-platform:anycpu ";
545546
}
547+
548+
if (flags.HasFlag(CompilerOptions.CheckForOverflowUnderflow))
549+
{
550+
otherOptions += "-checked+ ";
551+
}
552+
else
553+
{
554+
otherOptions += "-checked- ";
555+
}
556+
546557
if (preprocessorSymbols.Count > 0)
547558
{
548559
otherOptions += " \"-d:" + string.Join(";", preprocessorSymbols) + "\" ";

ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<Compile Include="TestCases\Pretty\Comparisons.cs" />
142142
<Compile Include="TestCases\Pretty\GloballyQualifiedTypeInStringInterpolation.cs" />
143143
<Compile Include="TestCases\Pretty\Issue3406.cs" />
144+
<Compile Include="TestCases\Pretty\Issue3483.cs" />
144145
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
145146
<Compile Include="TestCases\Pretty\Issue3439.cs" />
146147
<Compile Include="TestCases\Pretty\Issue3442.cs" />

ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ public async Task Issue3442([ValueSource(nameof(defaultOptions))] CompilerOption
635635
await RunForLibrary(cscOptions: cscOptions);
636636
}
637637

638+
[Test]
639+
public async Task Issue3483([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
640+
{
641+
await RunForLibrary(cscOptions: cscOptions | CompilerOptions.CheckForOverflowUnderflow, configureDecompiler: settings => settings.CheckForOverflowUnderflow = true);
642+
}
643+
638644
[Test]
639645
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
640646
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
2+
{
3+
internal static class Issue3483
4+
{
5+
public static int Add_Checked(int x, int y)
6+
{
7+
return x + y;
8+
}
9+
public static int Add_Unchecked(int x, int y)
10+
{
11+
return unchecked(x + y);
12+
}
13+
public static int Add_CheckedAndUnchecked_1(int x, int y, int z)
14+
{
15+
return x + unchecked(y + z);
16+
}
17+
public static int Add_CheckedAndUnchecked_2(int x, int y, int z)
18+
{
19+
unchecked
20+
{
21+
return x + checked(y + z);
22+
}
23+
}
24+
public static uint Cast_Checked(int x)
25+
{
26+
return (uint)x;
27+
}
28+
public static uint Cast_Unchecked(int x)
29+
{
30+
return unchecked((uint)x);
31+
}
32+
public static int Cast_CheckedAndUnchecked_1(int x)
33+
{
34+
return (int)unchecked((uint)x);
35+
}
36+
public static int Cast_CheckedAndUnchecked_2(int x)
37+
{
38+
unchecked
39+
{
40+
return (int)checked((uint)x);
41+
}
42+
}
43+
}
44+
}

ICSharpCode.Decompiler/CSharp/Transforms/AddCheckedBlocks.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,14 @@ public void Run(AstNode node, TransformContext context)
252252
else
253253
{
254254
Result r = GetResultFromBlock(block);
255-
if (r.NodesToInsertInUncheckedContext != null)
256-
r.NodesToInsertInUncheckedContext.Insert();
255+
if (context.DecompileRun.Settings.CheckForOverflowUnderflow)
256+
{
257+
r.NodesToInsertInCheckedContext?.Insert();
258+
}
259+
else
260+
{
261+
r.NodesToInsertInUncheckedContext?.Insert();
262+
}
257263
}
258264
}
259265

ICSharpCode.Decompiler/DecompilerSettings.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,24 @@ public bool SortCustomAttributes {
21852185
}
21862186
}
21872187

2188+
bool checkForOverflowUnderflow = false;
2189+
2190+
/// <summary>
2191+
/// Check for overflow and underflow in operators.
2192+
/// </summary>
2193+
[Category("DecompilerSettings.Other")]
2194+
[Description("DecompilerSettings.CheckForOverflowUnderflow")]
2195+
public bool CheckForOverflowUnderflow {
2196+
get { return checkForOverflowUnderflow; }
2197+
set {
2198+
if (checkForOverflowUnderflow != value)
2199+
{
2200+
checkForOverflowUnderflow = value;
2201+
OnPropertyChanged();
2202+
}
2203+
}
2204+
}
2205+
21882206
CSharpFormattingOptions csharpFormattingOptions;
21892207

21902208
[Browsable(false)]

ILSpy/Properties/Resources.Designer.cs

Lines changed: 9 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ILSpy/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ Are you sure you want to continue?</value>
288288
<data name="DecompilerSettings.AutoLoadAssemblyReferences" xml:space="preserve">
289289
<value>Automatically load assembly references</value>
290290
</data>
291+
<data name="DecompilerSettings.CheckForOverflowUnderflow" xml:space="preserve">
292+
<value>Check for overflow and underflow in operators</value>
293+
</data>
291294
<data name="DecompilerSettings.CheckedOperators" xml:space="preserve">
292295
<value>User-defined checked operators</value>
293296
</data>

ILSpy/Properties/Resources.zh-Hans.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@
279279
<data name="DecompilerSettings.AsyncEnumerator" xml:space="preserve">
280280
<value>反编译异步 IAsyncEnumerator 方法</value>
281281
</data>
282+
<data name="DecompilerSettings.CheckForOverflowUnderflow" xml:space="preserve">
283+
<value />
284+
</data>
282285
<data name="DecompilerSettings.DecompileAnonymousMethodsLambdas" xml:space="preserve">
283286
<value>反编译匿名方法或 lambda</value>
284287
</data>

0 commit comments

Comments
 (0)