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
Grabbing Mac computer name through C++ Interop hangs #69870
Comments
Looks like the conversion from @efroemling is |
That's just a standard Apple call from Foundation (albeit a deprecated one). I'm assuming it's Objective-C under the hood since it's been around for a while, but you'd have to check on your end to be sure. That repro case should be fully self contained if you want to give it a try. I'll go through it now and make sure it's still causing the same problem on my end. One note is to make sure to use a curly quote in the machine name (’) to trigger the problem; I'm not sure if straight quotes (') will do it. |
Just went through the steps on Xcode 15.0.1 on the macOS 14.2 RC and it's still exhibiting the hang. I also checked and found that adding a straight quote (') to the machine name does not trigger the problem; only a curly quote does (’). |
Thanks @efroemling, I can reproduce this on my end. Looks like we're getting into an infinite loop while converting the Swift String into a |
Edit: I realized that I'm quite late to the party. Hopefully my answer below can contribute to getting this issue fixed. I have the ambition of becoming an active contributor to the Swift project, and I find the c++ interop feature to be especially interesting. This issue seems to happen when we are dealing with non-contiguous strings which is defined in the above link as:
A temporary workaround is to modify // Expose a call for C++ to use.
public func getMacName() -> String {
var s = Host.current().localizedName ?? "Unknown"
s.makeContiguousUTF8()
return s
}
// Call our C++ function that runs the test.
doTest() If we do not make the String contiguous, we end up in an endless loop SWIFT_INLINE_THUNK String::operator std::string() const {
auto u = getUtf8();
std::string result;
result.reserve(u.getCount() + 1);
using IndexType = decltype(u.getStartIndex());
for (auto s = u.getStartIndex().getEncodedOffset(),
e = u.getEndIndex().getEncodedOffset();
s != e; s = u.indexOffsetBy(IndexType::init(s), 1).getEncodedOffset()) {
result.push_back(u[IndexType::init(s)]);
}
return result;
} defined in
and
Original post: I have been experimenting a bit with this, below are my findings. First of all, as @efroemling identified, the problem appears to happen when using an acute accent (´) but not '. I continued testing a bit and found the problem to occur specifically when using non-ASCII characters. Hence, both ' and ` work, but neither ´ nor 🧑💻. I modified the getMacName() function so that the string is created using Swift's String type: // Expose a call for C++ to use.
public func getMacName() -> String {
return String("Ludwig´s-MacBook-Pro")
} And the problem appears to be gone. If we now instead try using NSString: // Expose a call for C++ to use.
public func getMacName() -> String {
let a = NSString("Ludwig´s-MacBook-Pro")
return String(a)
} And the problem is back. It appears as if once we create an NSString there is no way to avoid the issue, even after converting to String. I guess in the original example: return Host.current().localizedName ?? "Unknown" We implicitly convert from |
Description
I've found that defining a simple function in Swift to grab the name of the local Mac and calling that from C++ can hang if the Mac name contains particular characters.
Steps to reproduce
The following steps reproduce the hang for me using XCode 15.0.1 (15A507) on an M1 Max MBP running Sonoma 14.1.1:
Environment
Swift compiler version info: swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Xcode version info: Xcode 15.0.1 Build version 15A507
Deployment target: macOS Sonoma 14.1.1 (23B81). Also repro'd on Monterey 12.7.1 (21G920)
The text was updated successfully, but these errors were encountered: