Skip to content

Commit a18d0d4

Browse files
Merge pull request #3484 from ds5678/issue3483
Add configuration option to check for overflow and underflow
2 parents 7fc9025 + 4e21aed commit a18d0d4

File tree

13 files changed

+113
-13
lines changed

13 files changed

+113
-13
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@
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\PointerArithmetic.cs" />
145144
<Compile Include="TestCases\Pretty\Issue3439.cs" />
146145
<Compile Include="TestCases\Pretty\Issue3442.cs" />
146+
<Compile Include="TestCases\Pretty\Issue3483.cs" />
147+
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
147148
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
148149
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
149150
<Compile Include="TestCases\VBPretty\VBNonGenericForEach.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/ProjectDecompiler/IProjectInfoProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public interface IProjectInfoProvider
3939
/// </summary>
4040
LanguageVersion LanguageVersion { get; }
4141

42+
/// <summary>
43+
/// Check for overflow and underflow in operators.
44+
/// </summary>
45+
bool CheckForOverflowUnderflow { get; }
46+
4247
/// <summary>
4348
/// Gets the unique ID of the project.
4449
/// </summary>

ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public void Write(
9898

9999
w.WriteElementString("OutputType", outputType);
100100
w.WriteElementString("LangVersion", project.LanguageVersion.ToString().Replace("CSharp", "").Replace('_', '.'));
101+
w.WriteElementString("CheckForOverflowUnderflow", project.CheckForOverflowUnderflow ? "true" : "false");
101102

102103
w.WriteElementString("AssemblyName", module.Name);
103104
if (targetFramework.Identifier != null)

ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterSdkStyle.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ static void WriteProjectInfo(XmlTextWriter xml, IProjectInfoProvider project)
192192
{
193193
xml.WriteElementString("LangVersion", project.LanguageVersion.ToString().Replace("CSharp", "").Replace('_', '.'));
194194
xml.WriteElementString("AllowUnsafeBlocks", TrueString);
195+
xml.WriteElementString("CheckForOverflowUnderflow", project.CheckForOverflowUnderflow ? TrueString : FalseString);
195196

196197
if (project.StrongNameKeyFile != null)
197198
{

ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public LanguageVersion LanguageVersion {
6868
}
6969
}
7070

71+
bool IProjectInfoProvider.CheckForOverflowUnderflow => Settings.CheckForOverflowUnderflow;
72+
7173
public IAssemblyResolver AssemblyResolver { get; }
7274

7375
public AssemblyReferenceClassifier AssemblyReferenceClassifier { get; }

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)]

0 commit comments

Comments
 (0)