Skip to content

Force unwrap in withUnsafeFileSystemRepresentation causes crash. #6419

Closed

Description

@liliomk-supercharge

New Issue Checklist

Bug Description

Force unwrap in withUnsafeFileSystemRepresentation (Source/SwiftLintCore/Extensions/URL+SwiftLint.swift) causes crash.

Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.

$ swiftlint

Workaround was to exclude some paths that crashed the app:

excluded:
  - App.xcodeproj
  - Resources/Settings.bundle

Environment

  • SwiftLint version 0.63.0
  • Xcode version 16.2, 16.3
  • Installation method used Homebrew
  • Configuration file:
disabled_rules:
  - dynamic_inline
  - private_unit_test
  - type_body_length
  - valid_ibinspectable
  - strict_fileprivate
  - function_default_parameter_at_end
  - multiline_function_chains
  - type_name
  - conditional_returns_on_newline
  - closure_body_length

opt_in_rules:
  - closure_spacing
  - convenience_type
  - discouraged_object_literal
  - empty_string
  - fallthrough
  - fatal_error_message
  - first_where
  - multiline_arguments
  - multiline_parameters
  - overridden_super_call
  - override_in_extension
  - required_enum_case
  - vertical_parameter_alignment_on_call
  - yoda_condition
  - array_init
  - explicit_init
  - function_default_parameter_at_end
  - redundant_nil_coalescing
  - collection_alignment
  - contains_over_filter_count
  - contains_over_filter_is_empty
  - contains_over_first_not_nil
  - contains_over_range_nil_comparison
  - discouraged_optional_boolean
  - empty_collection_literal
  - empty_count
  - file_name_no_space
  - flatmap_over_map_reduce
  - identical_operands
  - joined_default_parameter
  - last_where
  - literal_expression_end_indentation
  - nslocalizedstring_key
  - operator_usage_whitespace
  - pattern_matching_keywords
  - private_action
  - private_outlet
  - prohibited_super_call
  - reduce_into
  - redundant_type_annotation
  - sorted_first_last
  - static_operator
  - toggle_bool
  - unavailable_function
  - unneeded_parentheses_in_closure_argument
  - vertical_whitespace_closing_braces
  - closure_end_indentation
  - nimble_operator
  - weak_delegate
  - private_swiftui_state

analyzer_rules:
  - unused_declaration
  - unused_import

excluded:
  - Frameworks
  - Scripts
  - node_modules
  - Sources/Generated/
  - Tests
closing_brace:
  severity: error
closure_end_indentation:
  severity: error
colon:
  severity: error
  flexible_rightx_spacing: false
  apply_to_dictionaries: true
comma:
  severity: error
control_statement:
  severity: error
cyclomatic_complexity:
  - 7 #warning
  - 8 #error
empty_count:
  severity: error
empty_parameters:
  severity: error
explicit_init:
  severity: error
file_length:
  - 300 #warning
  - 500 #error
force_cast:
  severity: error
force_try:
  severity: error
function_body_length:
  - 50 #warning
  - 70 #error
function_parameter_count:
  - 5 #warning
  - 6 #error
implicit_getter:
  severity: error
large_tuple:
  - 3 #warning
  - 5 #error
leading_whitespace:
  severity: error
legacy_cggeometry_functions:
  severity: error
legacy_constant:
  severity: error
legacy_constructor:
  severity: error
legacy_nsgeometry_functions:
  severity: error
line_length:
  ignores_comments: true
  warning: 120
  error: 120
mark:
  severity: error
nesting:
  severity: error
nimble_operator:
  severity: error
opening_brace:
  severity: error
operator_usage_whitespace:
  severity: error
operator_whitespace:
  severity: error
overridden_super_call:
  severity: error
redundant_nil_coalescing:
  severity: error
redundant_optional_initialization:
  severity: error
redundant_string_enum_value:
  severity: error
redundant_void_return:
  severity: error
return_arrow_whitespace:
  severity: error
syntactic_sugar:
  severity: error
todo:
  severity: warning
trailing_comma:
  severity: error
trailing_newline:
  severity: error
trailing_semicolon:
  severity: error
trailing_whitespace:
  severity: error
unused_closure_parameter:
  severity: warning
identifier_name:
  min_length: 2
  severity: error
vertical_parameter_alignment:
  severity: error
vertical_whitespace:
  severity: error
void_return:
  severity: error
weak_delegate:
  severity: error
first_where:
  severity: error
private_action:
  severity: error
private_outlet:
  severity: error
closure_spacing:
  severity: error
array_init:
  severity: error
statement_position:
  severity: error
unused_enumerated:
  severity: error
vertical_parameter_alignment_on_call:
  severity: error
empty_string:
  severity: error
unneeded_break_in_switch:
  severity: error
fatal_error_message:
  severity: error
empty_parentheses_with_trailing_closure:
  severity: error
contains_over_first_not_nil:
  severity: error
private_swiftui_state:
  severity: error

custom_rules:
  environment_private:
    name: "Private SwiftUI Environment"
    regex: "\\@Environment\\s*var"
    message: "SwiftUI @Environment variables should always be marked private."
    severity: error
  binding_private:
    name: "Private SwiftUI Binding"
    regex: "\\@Binding\\s*var"
    message: "SwiftUI @Binding variables should always be marked private."
    severity: error

Activity

  1. SimplyDanny commented on Jan 7, 2026

    @SimplyDanny
    Collaborator

    Are you able to scope the crash to a single file (path)? That would help a lot.

    @compnerd: For your interest. Do you have an idea for which paths this call would fail?

  2. added
    bug
    on Jan 7, 2026
  3. compnerd commented on Jan 7, 2026

    @compnerd
    Contributor

    It is possible that the file path is not valid utf8, empty, or not a file URL. Those are the three cases that immediately come to mind.

  4. liliomk-supercharge commented on Jan 7, 2026

    @liliomk-supercharge
    Author

    I built the project locally and put in some logging into filepath:

        var filepath: String {
            print("xxx", self)
            return withUnsafeFileSystemRepresentation { String(cString: $0!) }
        }

    When the file had no issues, the log looked as follows:

    xxx FILENAME -- file://PARENT_DIRECTORY_PATH
    

    When there was an issue, either only the filename or the parent directory path was printed.

    Sorry, I can't give you anything easily reproducible, but as far as I could see, there was nothing special with the files/directories this has failed with.

    I'll check whether calling standardizedFileURL fixes the issue. In any case I think replacing the force unwrap with an optional would be a bit better.

  5. SimplyDanny commented on Jan 9, 2026

    @SimplyDanny
    Collaborator

    @liliomk-supercharge: Can you track down which part of the code calls url.filepath when it fails? We need to understand how the URL was constructed and do it right.

  6. liliomk-supercharge commented on Jan 12, 2026

    @liliomk-supercharge
    Author
    
    (lldb) bt
    * thread #2, queue = 'com.apple.root.default-qos', stop reason = EXC_BREAKPOINT (code=1, subcode=0x10049ee5c)
      * frame #0: 0x000000010049ee5c swiftlint`___lldb_unnamed_symbol51704 + 48
        frame #1: 0x000000019d2fde34 Foundation`Foundation.URL.withUnsafeFileSystemRepresentation<τ_0_0>((Swift.Optional<Swift.UnsafePointer<Swift.Int8>>) throws -> τ_0_0) throws -> τ_0_0 + 248
        frame #2: 0x000000010049edfc swiftlint`Foundation.URL.filepath.getter : Swift.String + 1320
        frame #3: 0x0000000100058224 swiftlint`___lldb_unnamed_symbol30176 + 580
        frame #4: 0x0000000100058674 swiftlint`___lldb_unnamed_symbol30178 + 128
        frame #5: 0x000000010047a6b4 swiftlint`___lldb_unnamed_symbol51157 + 24
        frame #6: 0x0000000100479780 swiftlint`___lldb_unnamed_symbol51100 + 192
        frame #7: 0x00000001b2cc11c4 libswiftDispatch.dylib`partial apply forwarder for reabstraction thunk helper from @callee_guaranteed (@unowned Swift.Int) -> () to @escaping @callee_guaranteed (@unowned Swift.Int) -> () + 28
        frame #8: 0x00000001b2cc11f0 libswiftDispatch.dylib`reabstraction thunk helper from @escaping @callee_guaranteed (@unowned Swift.Int) -> () to @callee_unowned @convention(block) (@unowned Swift.Int) -> () + 32
        frame #9: 0x000000019b3d1874 libdispatch.dylib`_dispatch_client_callout2 + 16
        frame #10: 0x000000019b3cc7b8 libdispatch.dylib`_dispatch_apply_serial + 164
        frame #11: 0x000000019b3d185c libdispatch.dylib`_dispatch_client_callout + 16
        frame #12: 0x000000019b3be444 libdispatch.dylib`_dispatch_sync_function_invoke + 52
        frame #13: 0x000000019b3cbe1c libdispatch.dylib`_dispatch_apply_with_attr_f + 1424
        frame #14: 0x000000019b3cbf34 libdispatch.dylib`dispatch_apply + 96
        frame #15: 0x00000001b2cc127c libswiftDispatch.dylib`_swift_dispatch_apply_current + 128
        frame #16: 0x00000001b2cc1148 libswiftDispatch.dylib`static __C.OS_dispatch_queue.concurrentPerform(iterations: Swift.Int, execute: (Swift.Int) -> ()) -> () + 196
        frame #17: 0x0000000100479698 swiftlint`___lldb_unnamed_symbol51099 + 136
        frame #18: 0x000000010047a9e0 swiftlint`___lldb_unnamed_symbol51175 + 24
        frame #19: 0x000000010047a12c swiftlint`___lldb_unnamed_symbol51142 + 260
        frame #20: 0x000000010047981c swiftlint`___lldb_unnamed_symbol51101 + 76
        frame #21: 0x0000000100479594 swiftlint`___lldb_unnamed_symbol51097 + 164
        frame #22: 0x0000000100479104 swiftlint`Swift.Array.parallelMap<τ_0_0>(transform: @Sendable (τ_0_0) -> τ_1_0) -> Swift.Array<τ_1_0> + 284
        frame #23: 0x0000000100479354 swiftlint`Swift.Array.parallelFlatMap<τ_0_0>(transform: @Sendable (τ_0_0) -> Swift.Array<τ_1_0>) -> Swift.Array<τ_1_0> + 88
        frame #24: 0x000000010005859c swiftlint`___lldb_unnamed_symbol30176 + 1468
        frame #25: 0x0000000100058674 swiftlint`___lldb_unnamed_symbol30178 + 128
        frame #26: 0x000000010047a6b4 swiftlint`___lldb_unnamed_symbol51157 + 24
        frame #27: 0x0000000100479780 swiftlint`___lldb_unnamed_symbol51100 + 192
        frame #28: 0x00000001b2cc11c4 libswiftDispatch.dylib`partial apply forwarder for reabstraction thunk helper from @callee_guaranteed (@unowned Swift.Int) -> () to @escaping @callee_guaranteed (@unowned Swift.Int) -> () + 28
        frame #29: 0x00000001b2cc11f0 libswiftDispatch.dylib`reabstraction thunk helper from @escaping @callee_guaranteed (@unowned Swift.Int) -> () to @callee_unowned @convention(block) (@unowned Swift.Int) -> () + 32
        frame #30: 0x000000019b3d1874 libdispatch.dylib`_dispatch_client_callout2 + 16
        frame #31: 0x000000019b3cc7b8 libdispatch.dylib`_dispatch_apply_serial + 164
        frame #32: 0x000000019b3d185c libdispatch.dylib`_dispatch_client_callout + 16
        frame #33: 0x000000019b3be444 libdispatch.dylib`_dispatch_sync_function_invoke + 52
        frame #34: 0x000000019b3cbe1c libdispatch.dylib`_dispatch_apply_with_attr_f + 1424
        frame #35: 0x000000019b3cbf34 libdispatch.dylib`dispatch_apply + 96
        frame #36: 0x00000001b2cc127c libswiftDispatch.dylib`_swift_dispatch_apply_current + 128
        frame #37: 0x00000001b2cc1148 libswiftDispatch.dylib`static __C.OS_dispatch_queue.concurrentPerform(iterations: Swift.Int, execute: (Swift.Int) -> ()) -> () + 196
        frame #38: 0x0000000100479698 swiftlint`___lldb_unnamed_symbol51099 + 136
        frame #39: 0x000000010047a9e0 swiftlint`___lldb_unnamed_symbol51175 + 24
        frame #40: 0x000000010047a12c swiftlint`___lldb_unnamed_symbol51142 + 260
        frame #41: 0x000000010047981c swiftlint`___lldb_unnamed_symbol51101 + 76
        frame #42: 0x0000000100479594 swiftlint`___lldb_unnamed_symbol51097 + 164
        frame #43: 0x0000000100479104 swiftlint`Swift.Array.parallelMap<τ_0_0>(transform: @Sendable (τ_0_0) -> τ_1_0) -> Swift.Array<τ_1_0> + 284
        frame #44: 0x0000000100479354 swiftlint`Swift.Array.parallelFlatMap<τ_0_0>(transform: @Sendable (τ_0_0) -> Swift.Array<τ_1_0>) -> Swift.Array<τ_1_0> + 88
        frame #45: 0x000000010005859c swiftlint`___lldb_unnamed_symbol30176 + 1468
        frame #46: 0x0000000100058674 swiftlint`___lldb_unnamed_symbol30178 + 128
        frame #47: 0x000000010047a6b4 swiftlint`___lldb_unnamed_symbol51157 + 24
        frame #48: 0x0000000100479780 swiftlint`___lldb_unnamed_symbol51100 + 192
        frame #49: 0x00000001b2cc11c4 libswiftDispatch.dylib`partial apply forwarder for reabstraction thunk helper from @callee_guaranteed (@unowned Swift.Int) -> () to @escaping @callee_guaranteed (@unowned Swift.Int) -> () + 28
        frame #50: 0x00000001b2cc11f0 libswiftDispatch.dylib`reabstraction thunk helper from @escaping @callee_guaranteed (@unowned Swift.Int) -> () to @callee_unowned @convention(block) (@unowned Swift.Int) -> () + 32
        frame #51: 0x000000019b3d1874 libdispatch.dylib`_dispatch_client_callout2 + 16
        frame #52: 0x000000019b3cc610 libdispatch.dylib`_dispatch_apply_invoke3 + 336
        frame #53: 0x000000019b3d185c libdispatch.dylib`_dispatch_client_callout + 16
        frame #54: 0x000000019b3baa28 libdispatch.dylib`_dispatch_once_callout + 32
        frame #55: 0x000000019b3cccb8 libdispatch.dylib`_dispatch_apply_invoke_and_wait + 372
        frame #56: 0x000000019b3cbdac libdispatch.dylib`_dispatch_apply_with_attr_f + 1312
        frame #57: 0x000000019b3cbf34 libdispatch.dylib`dispatch_apply + 96
        frame #58: 0x00000001b2cc127c libswiftDispatch.dylib`_swift_dispatch_apply_current + 128
        frame #59: 0x00000001b2cc1148 libswiftDispatch.dylib`static __C.OS_dispatch_queue.concurrentPerform(iterations: Swift.Int, execute: (Swift.Int) -> ()) -> () + 196
        frame #60: 0x0000000100479698 swiftlint`___lldb_unnamed_symbol51099 + 136
        frame #61: 0x000000010047a9e0 swiftlint`___lldb_unnamed_symbol51175 + 24
        frame #62: 0x000000010047a12c swiftlint`___lldb_unnamed_symbol51142 + 260
        frame #63: 0x000000010047981c swiftlint`___lldb_unnamed_symbol51101 + 76
        frame #64: 0x0000000100479594 swiftlint`___lldb_unnamed_symbol51097 + 164
        frame #65: 0x0000000100479104 swiftlint`Swift.Array.parallelMap<τ_0_0>(transform: @Sendable (τ_0_0) -> τ_1_0) -> Swift.Array<τ_1_0> + 284
        frame #66: 0x0000000100479354 swiftlint`Swift.Array.parallelFlatMap<τ_0_0>(transform: @Sendable (τ_0_0) -> Swift.Array<τ_1_0>) -> Swift.Array<τ_1_0> + 88
        frame #67: 0x000000010005859c swiftlint`___lldb_unnamed_symbol30176 + 1468
        frame #68: 0x0000000100057d04 swiftlint`__C.NSFileManager.filesToLint(inPath: Swift.String, rootDirectory: Swift.Optional<Swift.String>, excluder: SwiftLintFramework.Excluder) -> Swift.Array<Swift.String> + 628
        frame #69: 0x0000000100024da8 swiftlint`___lldb_unnamed_symbol29560 + 2732
        frame #70: 0x0000000100023714 swiftlint`___lldb_unnamed_symbol29555 + 148
        frame #71: 0x000000010005c988 swiftlint`___lldb_unnamed_symbol30242
        frame #72: 0x000000010005be04 swiftlint`___lldb_unnamed_symbol30235
        frame #73: 0x000000010005bbe4 swiftlint`___lldb_unnamed_symbol30230
        frame #74: 0x0000000100011f3c swiftlint`___lldb_unnamed_symbol29103
        frame #75: 0x00000001000122b4 swiftlint`___lldb_unnamed_symbol29117
        frame #76: 0x000000010089e0cc swiftlint`___lldb_unnamed_symbol97246
        frame #77: 0x0000000100018d24 swiftlint`___lldb_unnamed_symbol29266
    
  7. liliomk-supercharge commented on Jan 12, 2026

    @liliomk-supercharge
    Author

    Some more info:

    macOS 15.7.1

    Modified code to:

    private let printLock = NSLock()
    
    public extension URL {
        var filepath: String {
            // Get file type (file, directory, etc.) before converting to file system representation
            var type: String = "unknown"
            do {
                let resourceValues = try standardizedFileURL.resourceValues(forKeys: [.typeIdentifierKey])
                if let typeIdentifier = resourceValues.typeIdentifier {
                    type = typeIdentifier
                }
            } catch {
                print("xxx", "Error retrieving resource values: \(error)")
            }
    
            // Get file system entry type (file, directory, symbolic link, etc.)
            var fstype: String = "unknown"
            do {
                let resourceValues = try standardizedFileURL.resourceValues(forKeys: [.fileResourceTypeKey])
                if let fileResourceType = resourceValues.fileResourceType {
                    switch fileResourceType {
                    case .regular:
                        fstype = "regular file"
                    case .directory:
                        fstype = "directory"
                    case .symbolicLink:
                        fstype = "symbolic link"
                    case .socket:
                        fstype = "socket"
                    case .characterSpecial:
                        fstype = "character special"
                    case .blockSpecial:
                        fstype = "block special"
                    case .unknown:
                        fstype = "unknown"
                    default:
                        fstype = "unknown"
                    }
                }
            } catch {
                print("xxx", "Error retrieving file resource type: \(error)")
            }
    
            let pSelf = String(describing: self)
            let pPath = path(percentEncoded: false)
            let pStd = standardizedFileURL
            let pType = type
            let pFSType = fstype
    
            // Print debug information
            let debugInfo = """
            ------------------------------------------------------------
            URL: \(pSelf)
            Path: \(pPath)
            Standardized URL: \(pStd)
            Type Identifier: \(pType)
            File System Type: \(pFSType)
    
            """
    
            printLock.lock()
            defer { printLock.unlock() }
    
            FileHandle.standardOutput.write(debugInfo.data(using: .utf8)!)
    
            defer {
                let ok = "<<< OK\n"
                FileHandle.standardOutput.write(ok.data(using: .utf8)!)
            }
    
            return standardizedFileURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }
        }
    

    And before failing I got the following output:

    ------------------------------------------------------------
    URL: androidx.core:core-bundle.plist
    Path: 
    Standardized URL: androidx.core:core-bundle.plist
    Type Identifier: unknown
    File System Type: unknown
    PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
    zsh: trace trap  /Users/myuser/Downloads/swiftlint
    
  8. compnerd commented on Jan 12, 2026

    @compnerd
    Contributor

    Hmm, path being empty is the cause of the issue. For a file URL, I would expect path to be the actual path (relative or absolute). I wonder how this path is being modified after creation.

  9. SimplyDanny commented on Jan 14, 2026

    @SimplyDanny
    Collaborator

    @liliomk-supercharge: Could you check whether the current main fixes the issue for you? I made a change for another #6425 and I'm wondering if it might avoid this crash too.

    Other than that, I'm not able to create a test cases to reproduce this error. URL seems to be very safe to use normally.

  10. liliomk-supercharge commented on Jan 14, 2026

    @liliomk-supercharge
    Author

    @SimplyDanny main from commit 35e3d0c6f473fdd58d4821d3d93258e899c1832f didn't solve it. I attached a debug stacktrace.

    lldb.txt

  11. liliomk-supercharge commented on Jan 14, 2026

    @liliomk-supercharge
    Author

    @SimplyDanny @compnerd I attached a minimal example.

    App-minimal.zip

    Instructions:

    Unpack the zip
    Open Terminal
    `cd` into the unpacked `App` directory
    run swiftlint (without any additional parameters)
    

    Results:

    • macOS 15.3.2 + Xcode 16.2 => SwiftLintCore/URL+SwiftLint.swift:5: Fatal error: Unexpectedly found nil while unwrapping an Optional value
    • macOS 26.2 + Xcode 26.2 => no problem
  12. SimplyDanny commented on Jan 14, 2026

    @SimplyDanny
    Collaborator

    The problem is that the : in androidx.core:core-bundle.plist is actually not a colon. It's only how many fonts represent it. It really is a Unicode character from the Private Use Area to avoid using colons as they might be treated specially depending on the OS.

    It seems like there was an issue with that in older versions of Foundation, but now, with the latest one, it works. Not sure how to workaround the issue even for older versions. Avoiding the force-unwrap is one way assuming that nil only appears for file we wouldn't want to lint anyway. But what would be the result of url.filepath then? Or if we find out what fixed the issue in Foundation, maybe we can take over the fix temporarily ...

    @compnerd: I'm keen to hear your opinion and interpretation.

  13. compnerd commented on Jan 14, 2026

    @compnerd
    Contributor

    That is pretty terrible, but I can understand why - : is restricted on Windows file systems (it is the ADS separator and the drive separator). The file would not be writable in the first place and would most likely trigger an abnormal exit somewhere. This really feels like a client side issue as they should avoid using : in the filename. I think that your analysis is correct that we would fail to lint this in the first place. The only thing that I can think of is perhaps we should do something like this instead:

    return withUnsafeFileSystemRepresentation { pPtr in
      guard let pPtr else {
        fatalError("invalid file path: \(description)")
      }
      return String(cString: pPtr)
    }

    to improve the diagnostics.

  14. SimplyDanny commented on Jan 14, 2026

    @SimplyDanny
    Collaborator

    Better reporting instead of crashing will be done with #6435. There's nothing more we can do. The issue can easily be avoided by excluding folders with these problematic files (which is a good idea anyway to speed up linting).

    @liliomk-supercharge: Thank you for the analysis, logs and the example project!

    @compnerd: Thank you for your fast responses and valuable insights!

  15. SimplyDanny commented on Jan 15, 2026

    @SimplyDanny
    Collaborator

    Good news: The "URL over string paths" PR #6408 makes it work for all macOS versions again.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions