Skip to content

Undefined stack behavior w/ InlineArray: InlineArray<16, UInt8> contents change after a trivial copy #83774

@MahdiBM

Description

@MahdiBM

EDIT:
Marking enum IPAddress as indirect solves this issue which further confirms this a stack handling issue/overflow.

Description

Some sort of stack overflow?

TL;DR, I've confirmed when I pass an IPv6Address to a copyable IPAddress enum, the contents of IPv6Address.bytes which is of type InlineArray<16, UInt8> change.
Looks like some kind of stack mishandling or overflow to me.

    @available(swiftDNSApplePlatforms 26, *)
    @Test func ipv6AddressToName() {
        let ipAddress = IPv6Address(bytes: [
            0x2a, 0x01,
            0x5c, 0xc0,
            0x00, 0x01,
            0x00, 0x02,
            0x00, 0x00,
            0x00, 0x00,
            0x00, 0x00,
            0x00, 0x04,
        ])
        let name1 = Name(ipAddress: ipAddress)
        let name2 = Name(ipAddress: .v6(ipAddress))
        #expect(name1.debugDescription == "[2a01:5cc0:1:2:0:0:0:4]")
        #expect(name2.debugDescription == "[2a01:5cc0:1:2:0:0:0:4]")
    }

This test fails the last #expect, but passed the one above.

Name(ipAddress: .v6(ipAddress)) essentially calls the same initializer as Name(ipAddress: ipAddress):

@available(swiftDNSApplePlatforms 26, *)
extension Name {
    public init(ipAddress: IPAddress) {
        switch ipAddress {
        case .v4(let ipv4):
            self.init(ipAddress: ipv4)
        case .v6(let ipv6):
            self.init(ipAddress: ipv6)
        }
    }

    public init(ipAddress: IPv4Address) {
        ...
    }

    public init(ipAddress: IPv6Address) {
        ...
    }

IPv6Address is defined as:

@available(swiftDNSApplePlatforms 26, *)
public struct IPv6Address: Sendable {
    public var bytes: InlineArray<16, UInt8>
}

Reproduction

docker run --rm -it swiftlang/swift:nightly-6.2-noble bash -c '
  git clone https://github.com/MahdiBM/swift-dns
  cd swift-dns
  git checkout 57da9f9464eb889005f9424f96bed0d2a658ae99
  swift test --filter "ipv6AddressToName"
'

Result:

[856/856] Linking swift-dnsPackageTests.xctest
Build complete! (141.17s)
Test Suite 'Selected tests' started at 2025-08-16 09:57:51.813
Test Suite 'Selected tests' passed at 2025-08-16 09:57:51.815
         Executed 0 tests, with 0 failures (0 unexpected) in 0.0 (0.0) seconds
◇ Test run started.
↳ Testing Library Version: 6.2 (3fdabe5392108d8)
↳ Target Platform: aarch64-unknown-linux-gnu
◇ Suite NameTests started.
◇ Test ipv6AddressToName() started.
2 ["2a", "1", "5c", "c0", "0", "1", "0", "2", "0", "0", "0", "0", "0", "0", "0", "4"] # Manual print statement inserted
1 ["2a", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"] # Manual print statement inserted
2 ["2a", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"] # Manual print statement inserted
✘ Test ipv6AddressToName() recorded an issue at NameTests.swift:271:9: Expectation failed: (name2.debugDescription → "[2a00:0:0:0:0:0:0:0]") == "[2a01:5cc0:1:2:0:0:0:4]"
✘ Test ipv6AddressToName() failed after 0.001 seconds with 1 issue.
✘ Suite NameTests failed after 0.001 seconds with 1 issue.
✘ Test run with 1 test in 1 suite failed after 0.002 seconds with 1 issue.

["2a", "1", "5c", "c0", "0", "1", "0", "2", "0", "0", "0", "0", "0", "0", "0", "4"] are the contents of IPv6Address which is correct.

["2a", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"] are the contents of IPAddress, which is incorrect.

Also reproducible on macOS 26 beta 5 + Xcode 26 beta 5.

Expected behavior

Same ip, same value.

Environment

swiftlang/swift:nightly-6.2-noble official Docker image,
or macOS 26 beta 6 + Xcode 26 beta 5.

$ swiftc -v
Apple Swift version 6.2-dev (LLVM 0d47fa0a0fd41fb, Swift 536aa932a9e0ea3)
Target: arm64-apple-macosx16.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions