Skip to content

Grabbing Mac computer name through C++ Interop hangs #69870

@efroemling

Description

@efroemling

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:

  • Create a new ‘Command Line Tool’ project named “TestCommand”.
  • Create a new C++ file named ‘test’ (with header). Click ‘Create Bridging Header’ when it asks.
  • In TestCommand-Bridging-Header.h, add:
    #include "test.hpp"
  • In test.hpp, add:
    void doTest();
  • In test.cpp, add:
    #include <TestCommand-Swift.h>
    
    void doTest() {
      printf("Getting Mac name via Swift...\n");
      std::string val = TestCommand::getMacName();
      printf("Got Mac name: %s\n", val.c_str());
    }
  • In main.swift, add:
    // Expose a call for C++ to use.
    public func getMacName() -> String {
      return Host.current().localizedName ?? "Unknown"
    }
    
    // Call our C++ function that runs the test.
    doTest()
  • Under build settings for the TestCommand target, set C++ and Objective-C Interoperability to “C++/Objective C++”
  • You should now be able to build and run the project.
  • Now, depending on the name of the Mac (as per System Settings -> General -> About), running should either print the name or hang indefinitely. In my case, if my Mac is named "Eric’s MacBook Pro" I get a hang, but if I rename my Mac in System Settings and remove the apostrophe then the run completes.
  • Interestingly, hard-coding the Swift function to return the exact string "Eric’s MacBook Pro" succeeds, whatever that might imply.

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions