Repository: apple/swift-nio
File: Tests/NIOPosixTests/DatagramChannelTests.swift (Swift)

Code before:
        var version: String

        struct Release {
            var release: String

            var major: Int?
            var minor: Int?

            init(parsing release: String) {
                self.release = release

                let components = release.split(separator: ".", maxSplits: 1)

                if components.count == 2 {
                    self.major = Int(components[0])
                    self.minor = components[1].split(separator: ".").first.map(String.init).flatMap(Int.init)
                } else {
                    self.major = nil
                    self.minor = nil
                }
            }
        }

        init(utsname info: utsname) {
            self.machine = withUnsafePointer(to: info.machine) { pointer in
                pointer.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: pointer)) {
                    String(cString: $0)
                }
            }

            self.nodeName = withUnsafePointer(to: info.nodename) { pointer in
                pointer.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: pointer)) {
                    String(cString: $0)
                }
            }

            self.sysName = withUnsafePointer(to: info.sysname) { pointer in
                pointer.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: pointer)) {
                    String(cString: $0)
                }
            }

           self.version = withUnsafePointer(to: info.version) { pointer in
               pointer.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: pointer)) {
                    String(cString: $0)
                }
            }

            self.release = withUnsafePointer(to: info.release) { pointer in
                pointer.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: pointer)) {
                    Release(parsing: String(cString: $0))

Review comment (bug):
This doesn't look like this can possibly be right: the size of value seems absolutely wrong. Can I suggest we use `withUnsafeBytesOf` instead?

Code after:
        var machine: String
        var nodeName: String
        var sysName: String
        var release: Release
        var version: String

        struct Release {
            var release: String

            var major: Int?
            var minor: Int?

            init(parsing release: String) {
                self.release = release

                let components = release.split(separator: ".", maxSplits: 1)

                if components.count == 2 {
                    self.major = Int(components[0])
                    self.minor = components[1].split(separator: ".").first.map(String.init).flatMap(Int.init)
                } else {
                    self.major = nil
                    self.minor = nil
                }
            }
        }

        init(utsname info: utsname) {
            self.machine = withUnsafeBytes(of: info.machine) { bytes in
                let pointer = bytes.baseAddress?.assumingMemoryBound(to: CChar.self)
                return pointer.map { String(cString: $0) } ?? ""
            }

            self.nodeName = withUnsafeBytes(of: info.nodename) { bytes in
                let pointer = bytes.baseAddress?.assumingMemoryBound(to: CChar.self)
                return pointer.map { String(cString: $0) } ?? ""
            }

            self.sysName = withUnsafeBytes(of: info.sysname) { bytes in
                let pointer = bytes.baseAddress?.assumingMemoryBound(to: CChar.self)
                return pointer.map { String(cString: $0) } ?? ""
            }

            self.version = withUnsafeBytes(of: info.version) { bytes in
                let pointer = bytes.baseAddress?.assumingMemoryBound(to: CChar.self)
                return pointer.map { String(cString: $0) } ?? ""
            }

            self.release = withUnsafeBytes(of: info.release) { bytes in
                let pointer = bytes.baseAddress?.assumingMemoryBound(to: CChar.self)
                let release = pointer.map { String(cString: $0) } ?? ""