Skip to content

[overlays] Clean up some sources of undefined behaviour #18799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/SDK/CoreGraphics/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ extension CGAffineTransform : Codable {

extension CGImage {
public func copy(maskingColorComponents components: [CGFloat]) -> CGImage? {
return self.__copy(maskingColorComponents: UnsafePointer(components))
return self.__copy(maskingColorComponents: components)
}
}

Expand Down
9 changes: 5 additions & 4 deletions stdlib/public/SDK/Metal/Metal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ extension MTLDevice {
@available(swift 4)
@available(macOS 10.13, *)
public func MTLCopyAllDevicesWithObserver(handler: @escaping MTLDeviceNotificationHandler) -> (devices:[MTLDevice], observer:NSObject) {
var resultTuple: (devices:[MTLDevice], observer:NSObject)
resultTuple.observer = NSObject()
resultTuple.devices = __MTLCopyAllDevicesWithObserver(AutoreleasingUnsafeMutablePointer<NSObjectProtocol?>(&resultTuple.observer), handler)
return resultTuple
var observer: NSObjectProtocol?
let devices = __MTLCopyAllDevicesWithObserver(&observer, handler)
// FIXME: The force cast here isn't great – ideally we would return the
// observer as an NSObjectProtocol.
return (devices, observer as! NSObject)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said in the comment, the force cast here isn't ideal, but it's better than what the code was previously doing – which was type punning an NSObject pointer to a NSObjectProtocol? pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can I confirm that __MTLCopyAllDevicesWithObserver is guaranteed to set observer? The previous code was initially assigning NSObject(), but I'm not sure whether that was specifically because it could actually return that as the observer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinged Metal team about that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the reply:

MTLCopyAllDevicesWithObserver(id <NSObject>* observer, MTLDeviceNotificationHandler handler) allocates a framework object to return via the observer pointer (specifically a _MTLDeviceNotifier object), so the pointer passed in can be set to nullptr.

}
#endif

Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/SDK/SceneKit/SceneKit.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ extension SCNGeometryElement {
extension SCNGeometrySource {
@nonobjc
public convenience init(vertices: [SCNVector3]) {
self.init(vertices: UnsafePointer(vertices), count: vertices.count)
self.init(vertices: vertices, count: vertices.count)
}
@nonobjc
public convenience init(normals: [SCNVector3]) {
self.init(normals: UnsafePointer(normals), count: normals.count)
self.init(normals: normals, count: normals.count)
}
@nonobjc
public convenience init(textureCoordinates: [CGPoint]) {
self.init(textureCoordinates: UnsafePointer(textureCoordinates), count: textureCoordinates.count)
self.init(textureCoordinates: textureCoordinates, count: textureCoordinates.count)
}
}

Expand Down