Skip to content

Fix macOS Finder FileOpen SIGABRT crash and UTF-8 non-ASCII path handling - #2572

Open
Pegasus02 wants to merge 1 commit into
KLayout:masterfrom
Pegasus02:fix/macos-finder-fileopen-utf8-crash
Open

Pegasus02 wants to merge 1 commit into
KLayout:masterfrom
Pegasus02:fix/macos-finder-fileopen-utf8-crash

Conversation

@Pegasus02

Copy link
Copy Markdown

Summary

Fixes a macOS crash (SIGABRT / Signal 6 via _objc_terminate) and silent non-ASCII path corruption when opening layout files with non-ASCII (e.g. CJK, accented) characters in their path from macOS Finder / LaunchServices (including CloudStorage / FileProvider mounts such as Synology Drive, iCloud Drive, OneDrive).

Related issues: #1213, #2442.

Root Causes

  1. Obsolete Qt4 macro Q_WS_MAC in src/tl/tl/tlInternational.cc:
    tl::initialize_codecs() checked #elif defined(Q_WS_MAC), which was removed in Qt 5. On Qt 5/Qt 6 macOS builds, execution fell through to #else (setlocale(LC_ALL, "") + nl_langinfo(CODESET)). Because macOS Finder launches .app bundles without LANG/LC_CTYPE, the C locale stays "C" (US-ASCII), leaving ms_system_codec and C multibyte routines unable to represent UTF-8 paths.
  2. Locale-dependent tl::to_local() / tl::to_string_from_local() under "C" locale in src/tl/tl/tlString.cc:
    Under "C"/US-ASCII LC_CTYPE, wctomb() returns -1 for every non-ASCII code point (silently stripping non-ASCII characters from file paths, causing ENOENT), and mbrtowc() aborts at the first byte >= 0x80 (truncating paths). On macOS, Darwin VFS (APFS/HFS+/FileProvider) POSIX paths are unconditionally UTF-8 regardless of LC_CTYPE.
  3. Uncaught C++ exception across the Objective-C FileOpen boundary in src/lay/lay/layApplication.cc:
    Unlike MainWindow::dropEvent(), GuiApplication::event() did not wrap its QEvent::FileOpen handler (mp_mw->load_layout(...)) in BEGIN_PROTECTED ... END_PROTECTED and did not return true. Any tl::Exception thrown while loading a file from Finder unwound into Cocoa (-[NSApplication _openURLs:...]), triggering _objc_terminate and aborting with SIGABRT (Signal 6) instead of showing KLayout's error dialog.

Changes

  • src/tl/tl/tlString.cc: Under #if defined(__APPLE__), return UTF-8 directly in tl::to_local() and tl::to_string_from_local() (preserving Darwin VFS UTF-8 paths even when LC_CTYPE is "C", in both Qt and headless HAVE_QT=0 builds).
  • src/tl/tl/tlInternational.cc: Replace dead #elif defined(Q_WS_MAC) with #elif defined(__APPLE__) || defined(Q_OS_MAC) and call setlocale (LC_CTYPE, "UTF-8") so ms_system_codec and C-library LC_CTYPE are UTF-8 while keeping LC_NUMERIC at "C".
  • src/lay/lay/layApplication.cc: Guard the QEvent::FileOpen handler in GuiApplication::event() with BEGIN_PROTECTED ... END_PROTECTED (matching MainWindow::dropEvent()) and return true; after handling.
  • macbuild/Resources/Info.plist.template: Add LSEnvironment (LANG=en_US.UTF-8, LC_CTYPE=UTF-8) so macOS LaunchServices launches the .app bundle with a UTF-8 environment for embedded Python/Ruby and C libraries.
  • src/tl/unit_tests/tlStringTests.cc: Add an #if defined(__APPLE__) regression test in TEST(15) verifying tl::to_local() and tl::to_string_from_local() preserve CJK/accented UTF-8 paths under setlocale(LC_ALL, "C").

Verification

  • Verified Info.plist.template with plutil -lint and xmllint --noout.
  • Verified src/tl/tl/tlString.cc and src/tl/tl/tlInternational.cc compile cleanly in headless (!HAVE_QT) mode with c++ -std=c++17 -Wall -Wextra -fsyntax-only.
  • Verified to_local / to_string_from_local UTF-8 round-trip under setlocale(LC_ALL, "C") and setlocale(LC_CTYPE, "UTF-8") (preserving LC_NUMERIC="C") via standalone C++17 -Wall -Wextra -Werror test on macOS arm64.
  • Verified real macOS LaunchServices (env -i ... open -a klayout.app <CJK path>.gds) with LSEnvironment UTF-8 loads cleanly with zero crash.

…ling

Fixes Signal 6 (SIGABRT via _objc_terminate) and non-ASCII path corruption
when opening layout files from macOS Finder / LaunchServices without LANG:

- src/tl/tl/tlString.cc: Preserve UTF-8 in to_local() and
  to_string_from_local() under #if defined(__APPLE__) because Darwin VFS
  (APFS/HFS+/FileProvider) POSIX paths are unconditionally UTF-8 even
  under the "C"/US-ASCII locale.
- src/tl/tl/tlInternational.cc: Replace obsolete Qt4 macro Q_WS_MAC with
  defined(__APPLE__) || defined(Q_OS_MAC) and call setlocale(LC_CTYPE,
  "UTF-8") in initialize_codecs().
- src/lay/lay/layApplication.cc: Wrap QEvent::FileOpen layout loading in
  BEGIN_PROTECTED ... END_PROTECTED (matching MainWindow::dropEvent) and
  return true so tl::Exception displays KLayout's error dialog instead of
  unwinding across Objective-C into _objc_terminate.
- macbuild/Resources/Info.plist.template: Set LSEnvironment (LANG and
  LC_CTYPE) to UTF-8 for macOS app bundle launches.
- src/tl/unit_tests/tlStringTests.cc: Add macOS regression test in TEST(15)
  verifying UTF-8 CJK/accented paths survive to_local()/to_string_from_local()
  under setlocale(LC_ALL, "C").

Refs: KLayout#1213, KLayout#2442
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