Skip to content

xpc: replace precondition+force-unwrap with guard-let in XPCMessage#1852

Open
ArasanDev wants to merge 1 commit into
apple:mainfrom
ArasanDev:fix/xpc-message-force-unwrap
Open

xpc: replace precondition+force-unwrap with guard-let in XPCMessage#1852
ArasanDev wants to merge 1 commit into
apple:mainfrom
ArasanDev:fix/xpc-message-force-unwrap

Conversation

@ArasanDev

Copy link
Copy Markdown

Type of Change

  • Bug fix

Motivation and Context

In XPCMessage, two methods use a precondition(x != nil) + immediate force-unwrap pattern, which violates NeverForceUnwrap:

// error()
let item = try? JSONDecoder().decode(ContainerXPCError.self, from: data)
precondition(item != nil, "expected to receive a ContainerXPCXPCError")
throw ContainerizationError(item!.code, message: item!.message)

// set(error:)
let data = try? JSONEncoder().encode(serializableError)
precondition(data != nil)
set(key: Self.errorKey, value: data!)

Both replaced with guard let + preconditionFailure, which expresses the same invariant without force-unwrap. Also fixed a typo in the error message (ContainerXPCXPCErrorContainerXPCError).

Testing

  • Tested locally
  • make test passes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant