Skip to content

Conversation

@3405691582
Copy link
Member

In #86276 strict memory safety becomes an error, necessitating adding the following annotations and changes:

  • in init(): check the return value of pthread_mutex_init; since this isn't a failable initializer, fatalError if this fails, to avoid silently leaving a zero-valued pointer,

  • similarly check return values in pthread_mutex_lock, pthread_mutex_unlock, and pthread_mutex_destroy,

  • the message in fatalError should use strerror, which necessitates unsafe markings since despite strerror never returning a null pointer.

  • marking the value as @safe since the underlying mutex type is an OpaquePointer and is unsafe and all access to value occur through a safe interface,

  • thus, marking _MutexHandle @safe.

@3405691582 3405691582 requested a review from a team as a code owner January 17, 2026 16:20
@3405691582
Copy link
Member Author

@swift-ci please test.

@3405691582 3405691582 added the OpenBSD Platform: OpenBSD label Jan 17, 2026
@grynspan
Copy link
Contributor

Can we use strerror_r()? strerror() is not thread-safe on OpenBSD (nor FreeBSD).

@3405691582
Copy link
Member Author

Can we use strerror_r()? strerror() is not thread-safe on OpenBSD (nor FreeBSD).

Good catch. Updated; I think I've done so appropriately.

@grynspan
Copy link
Contributor

I'd use String.init(unsafeUninitializedCapacity:_:) to avoid a copy, and set its capacity it to Int(NL_TEXTMAX) per https://man.openbsd.org/strerror.3 maybe?

In swiftlang#86276 strict memory safety becomes an error, necessitating adding
the following annotations and changes:

* in init(): check the return value of `pthread_mutex_init`; since this
  isn't a failable initializer, `fatalError` if this fails, to avoid
  silently leaving a zero-valued pointer,

* similarly check return values in `pthread_mutex_lock`,
  `pthread_mutex_unlock`, and `pthread_mutex_destroy`,

* the message in `fatalError` should use `strerror_r`, which necessitates
  `unsafe` markings since it initializes the array storage unsafely.

* marking the `value` as `@safe` since the underlying mutex type is an
  `OpaquePointer` and is unsafe and all access to value occur through
  a safe interface,

* thus, marking `_MutexHandle` `@safe`.
@3405691582
Copy link
Member Author

I'd use String.init(unsafeUninitializedCapacity:_:) to avoid a copy, and set its capacity it to Int(NL_TEXTMAX) per https://man.openbsd.org/strerror.3 maybe?

Done. That certainly looks more idomatic now.

Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

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

You're not quite holding the Array API right as-is. How about this instead? It avoids an intermediate heap allocation for the buffer and it uses a fallback string that still vends the error code.

@3405691582 3405691582 requested a review from grynspan January 18, 2026 13:19
@3405691582
Copy link
Member Author

3405691582 commented Jan 18, 2026

(Oh -- I just noticed I was using Array's withUnsafeTemporaryAllocation instead of String's like you originally suggested. 🤦‍♀️)

@3405691582
Copy link
Member Author

@swift-ci please test.


@usableFromInline
@_transparent
func errstr(_ no: Int32) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the OpenBSD implementation call strerror() elsewhere? Not for this PR, but would be worth auditing the codebase and replacing such calls with something like this function in the future. (Same for FreeBSD.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Not that I'm aware of, but making this more accessible is probably a good idea...

...as well as adding other affordances for interacting with libc, which again, is another good fit with the import C pitch...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah 🥲

@3405691582
Copy link
Member Author

@swift-ci please test.

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

Labels

OpenBSD Platform: OpenBSD

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants