Skip to content

Commit

Permalink
Guard nil address
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin committed Sep 6, 2023
1 parent 6cf6355 commit 926ba65
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
77 changes: 77 additions & 0 deletions DemoApp/DemoApp.xcodeproj/xcshareddata/xcschemes/DemoApp.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FA1671BE2A5367A800A42DB0"
BuildableName = "DemoApp.app"
BlueprintName = "DemoApp"
ReferencedContainer = "container:DemoApp.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FA1671BE2A5367A800A42DB0"
BuildableName = "DemoApp.app"
BlueprintName = "DemoApp"
ReferencedContainer = "container:DemoApp.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FA1671BE2A5367A800A42DB0"
BuildableName = "DemoApp.app"
BlueprintName = "DemoApp"
ReferencedContainer = "container:DemoApp.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
10 changes: 7 additions & 3 deletions Sources/SnapshotPreviewsCore/ConformanceLookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ private func parseConformance(conformance: UnsafePointer<ProtocolConformanceDesc
let descriptorOffset = Int(conformance.pointee.protocolDescriptor & ~1)
let jumpPtr = UnsafeRawPointer(conformance).advanced(by: MemoryLayout<ProtocolConformanceDescriptor>.offset(of: \.protocolDescriptor)!).advanced(by: descriptorOffset)
let address = jumpPtr.load(as: UInt64.self)

let typeDescriptorPointer = UnsafeRawPointer(conformance).advanced(by: MemoryLayout<ProtocolConformanceDescriptor>.offset(of: \.nominalTypeDescriptor)!).advanced(by: Int(conformance.pointee.nominalTypeDescriptor))
let descriptor = typeDescriptorPointer.assumingMemoryBound(to: TargetModuleContextDescriptor.self)
guard address != 0 else {
print("Unexpected 0 address of protocol for \(getTypeName(descriptor: descriptor) ?? "unknown")")
return nil
}
let protoPtr = UnsafeRawPointer(bitPattern: UInt(address))!
let proto = protoPtr.load(as: ProtocolDescriptor.self)
let namePtr = protoPtr.advanced(by: MemoryLayout<ProtocolDescriptor>.offset(of: \.name)!).advanced(by: Int(proto.name))
Expand All @@ -57,9 +64,6 @@ private func parseConformance(conformance: UnsafePointer<ProtocolConformanceDesc
return nil
}

let typeDescriptorPointer = UnsafeRawPointer(conformance).advanced(by: MemoryLayout<ProtocolConformanceDescriptor>.offset(of: \.nominalTypeDescriptor)!).advanced(by: Int(conformance.pointee.nominalTypeDescriptor))

let descriptor = typeDescriptorPointer.assumingMemoryBound(to: TargetModuleContextDescriptor.self)
if let name = getTypeName(descriptor: descriptor),
[ContextDescriptorKind.Class, ContextDescriptorKind.Struct, ContextDescriptorKind.Enum].contains(descriptor.pointee.flags.kind) {
let accessFunctionPointer = UnsafeRawPointer(descriptor).advanced(by: MemoryLayout<TargetModuleContextDescriptor>.offset(of: \.accessFunction)!).advanced(by: Int(descriptor.pointee.accessFunction))
Expand Down
4 changes: 2 additions & 2 deletions Sources/SnapshotPreviewsCore/PrecisionModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import Foundation
import SwiftUI

public struct EmergePrecisionModifier: ViewModifier {
let precision: Float
let precision: Float?

public func body(content: Content) -> some View {
content
}
}

extension View {
public func emergeSnapshotPrecision(_ precision: Float) -> some View {
public func emergeSnapshotPrecision(_ precision: Float?) -> some View {
modifier(EmergePrecisionModifier(precision: precision))
}
}

0 comments on commit 926ba65

Please sign in to comment.