Skip to content

Commit 1cfb91e

Browse files
authored
Avoid using NTSTATUS near exit(). (#776)
On Windows, `NTSTATUS` is a typedef of `long`. On 32-bit Windows, `long` is imported into Swift as `Int` but on 64-bit Windows,it's imported as `Int32`. These types are layout-equivalent but not implicitly convertible, so we get errors on 32-bit Windows when using an `NTSTATUS` (i.e. `Int`) where a `CInt` (i.e. `Int32`) is expected. This PR changes the types of the constants introduced in #766 to be explicitly `CInt` so that they are usable on both flavours of Windows. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent d7d2427 commit 1cfb91e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Sources/Testing/Support/Additions/WinSDKAdditions.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ internal import _TestingInternals
1313
#if os(Windows)
1414
/// A bitmask that can be applied to an `HRESULT` or `NTSTATUS` value to get the
1515
/// underlying status code.
16-
let STATUS_CODE_MASK = NTSTATUS(0xFFFF)
16+
///
17+
/// The type of this value is `CInt` rather than `HRESULT` or `NTSTATUS` for
18+
/// consistency between 32-bit and 64-bit Windows.
19+
let STATUS_CODE_MASK = CInt(0xFFFF)
1720

1821
/// The severity and facility bits to mask against a caught signal value before
1922
/// terminating a child process.
2023
///
21-
/// For more information about the `NTSTATUS` type including its bitwise layout,
22-
/// see [Microsoft's documentation](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781).
24+
/// The type of this value is `CInt` rather than `HRESULT` or `NTSTATUS` for
25+
/// consistency between 32-bit and 64-bit Windows. For more information about
26+
/// the `NTSTATUS` type including its bitwise layout, see
27+
/// [Microsoft's documentation](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/87fba13e-bf06-450e-83b1-9241dc81e781).
2328
let STATUS_SIGNAL_CAUGHT_BITS = {
24-
var result = NTSTATUS(0)
29+
var result = CInt(0)
2530

2631
// Set the severity and status bits.
27-
result |= STATUS_SEVERITY_ERROR << 30
32+
result |= CInt(STATUS_SEVERITY_ERROR) << 30
2833
result |= 1 << 29 // "Customer" bit
2934

3035
// We only have 12 facility bits, but we'll pretend they spell out "s6", short
@@ -33,7 +38,7 @@ let STATUS_SIGNAL_CAUGHT_BITS = {
3338
// We're camping on a specific "facility" code here that we don't think is
3439
// otherwise in use; if it conflicts with an exit test, we can add an
3540
// environment variable lookup so callers can override us.
36-
let FACILITY_SWIFT6 = ((NTSTATUS(UInt8(ascii: "s")) << 4) | 6)
41+
let FACILITY_SWIFT6 = ((CInt(UInt8(ascii: "s")) << 4) | 6)
3742
result |= FACILITY_SWIFT6 << 16
3843

3944
#if DEBUG

0 commit comments

Comments
 (0)