Skip to content

Commit da9906d

Browse files
authored
Fix issues in HANDLE types and other helpers when CheckForOverflowUnderflow is enabled (#1513)
1 parent cc1459e commit da9906d

6 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/Microsoft.Windows.CsWin32/Generator.TypeDef.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private IEnumerable<MemberDeclarationSyntax> CreateCommonTypeDefMembers(Identifi
278278
yield return ConstructorDeclaration(structName.Identifier)
279279
.AddModifiers(TokenWithSpace(this.Visibility))
280280
.AddParameterListParameters(Parameter(valueParameter.Identifier).WithType(IntPtrTypeSyntax.WithTrailingTrivia(TriviaList(Space))))
281-
.WithInitializer(ConstructorInitializer(SyntaxKind.ThisConstructorInitializer).AddArgumentListArguments(Argument(CastExpression(fieldType, valueParameter))))
281+
.WithInitializer(ConstructorInitializer(SyntaxKind.ThisConstructorInitializer).AddArgumentListArguments(Argument(UncheckedExpression(CastExpression(fieldType, valueParameter)))))
282282
.WithBody(Block());
283283
}
284284

src/Microsoft.Windows.CsWin32/templates/HRESULT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
partial struct HRESULT
3939
{
4040
public static implicit operator uint(HRESULT value) => (uint)value.Value;
41-
public static explicit operator HRESULT(uint value) => new HRESULT((int)value);
41+
public static explicit operator HRESULT(uint value) => new HRESULT(unchecked((int)value));
4242

4343
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
4444
internal bool Succeeded => this.Value >= 0;

src/Microsoft.Windows.CsWin32/templates/NTSTATUS.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
partial struct NTSTATUS
2727
{
2828
public static implicit operator uint(NTSTATUS value) => (uint)value.Value;
29-
public static explicit operator NTSTATUS(uint value) => new NTSTATUS((int)value);
29+
public static explicit operator NTSTATUS(uint value) => new NTSTATUS(unchecked((int)value));
3030

31-
internal Severity SeverityCode => (Severity)(((uint)this.Value & 0xc0000000) >> 30);
31+
internal Severity SeverityCode => (Severity)unchecked(((uint)this.Value & 0xc0000000) >> 30);
3232

3333
internal enum Severity
3434
{

src/Microsoft.Windows.CsWin32/templates/PInvokeClassMacros.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class PInvokeClassMacros
1919
/// <param name="a">The low word.</param>
2020
/// <param name="b">The high word.</param>
2121
/// <returns>A 32-bit unsigned integer.</returns>
22-
internal static uint MAKELONG(ushort a, ushort b) => (uint)(a | b << 16);
22+
internal static uint MAKELONG(ushort a, ushort b) => unchecked((uint)(a | b << 16));
2323

2424
/// <summary>
2525
/// Constructs a <see cref="global::Windows.Win32.Foundation.WPARAM"/> from two 16-bit values.
@@ -57,5 +57,5 @@ internal class PInvokeClassMacros
5757
/// </summary>
5858
/// <param name="value">The 32-bit value.</param>
5959
/// <returns>The high-order word.</returns>
60-
internal static ushort HIWORD(uint value) => (ushort)(value >> 16);
60+
internal static ushort HIWORD(uint value) => unchecked((ushort)(value >> 16));
6161
}

test/GenerationSandbox.Tests/BasicTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ public void HANDLE_OverridesEqualityOperator()
200200
Assert.False(handle5 == handle8);
201201
}
202202

203+
[Fact]
204+
public void HANDLE_CanCreateFromNegative()
205+
{
206+
// unchecked this will throw overflow exception in .NET Core.
207+
var handle = new HANDLE(new IntPtr(-3));
208+
}
209+
203210
[Fact]
204211
public void GetWindowText_FriendlyOverload()
205212
{

test/GenerationSandbox.Tests/GenerationSandbox.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<Compile Remove="ComRuntimeTests.cs" Condition="!$([MSBuild]::IsOSPlatform('Windows'))" />
66
</ItemGroup>
77

8+
<PropertyGroup>
9+
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
10+
</PropertyGroup>
11+
812
<ProjectExtensions>
913
<VisualStudio><UserProperties nativemethods_1json__JsonSchema="..\..\src\Microsoft.Windows.CsWin32\settings.schema.json" /></VisualStudio>
1014
</ProjectExtensions>

0 commit comments

Comments
 (0)