Crash on Data.base64EncodedString of Foundation

Xcode Version 15.0.1 (15A507)

Crash log


Hardware Model:      iPhone14,7
Code Type:           ARM-64 (Native)
Role: Foreground 
Parent Process: launchd [1]
OS Version: iPhone OS 16.6 (20G75) 
Release Type: User 
Baseband Version: 1.80.02 
Report Version: 104

Exception Type: EXC_CRASH (SIGABRT)
Triggered by Thread: 26
Application Specific Information: abort() called

Last Exception Backtrace:
0   CoreFoundation                	       0x1b3c54cb4 __exceptionPreprocess + 164
1   libobjc.A.dylib               	       0x1accf03d0 objc_exception_throw + 60
2   Foundation                    	       0x1adf6950c -[NSData(NSData) _base64EncodingAsString:withOptions:] + 1072
3   Foundation                    	       0x1ae2cf8bc Data.base64EncodedString(options:) + 640
...
6   libdispatch.dylib             	       0x1bb11a320 _dispatch_call_block_and_release + 32
7   libdispatch.dylib             	       0x1bb11beac _dispatch_client_callout + 20
8   libdispatch.dylib             	       0x1bb11ef8c _dispatch_queue_override_invoke + 788
9   libdispatch.dylib             	       0x1bb12d944 _dispatch_root_queue_drain + 396
10  libdispatch.dylib             	       0x1bb12e158 _dispatch_worker_thread2 + 164
11  libsystem_pthread.dylib       	       0x213a85da0 _pthread_wqthread + 228

Thread 26 name:   Dispatch queue: com.apple.root.default-qos
Thread 26 Crashed:
0   libsystem_kernel.dylib        	       0x1f2c11578 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x213a8c118 pthread_kill + 268
2   libsystem_c.dylib             	       0x1bb17c178 abort + 180
3   libc++abi.dylib               	       0x2139cabf8 abort_message + 132
4   libc++abi.dylib               	       0x2139ba444 demangling_terminate_handler() + 348
5   libobjc.A.dylib               	       0x1accf5ea4 _objc_terminate() + 144
6   libc++abi.dylib               	       0x2139c9fbc std::__terminate(void (*)()) + 16
7   libc++abi.dylib               	       0x2139c9f60 std::terminate() + 56
8   libdispatch.dylib             	       0x1bb11bec0 _dispatch_client_callout + 40
9   libdispatch.dylib             	       0x1bb11ef8c _dispatch_queue_override_invoke + 788
10  libdispatch.dylib             	       0x1bb12d944 _dispatch_root_queue_drain + 396
11  libdispatch.dylib             	       0x1bb12e158 _dispatch_worker_thread2 + 164
12  libsystem_pthread.dylib       	       0x213a85da0 _pthread_wqthread + 228
13  libsystem_pthread.dylib       	       0x213a85b7c start_wqthread + 8
12  libsystem_pthread.dylib       	       0x213a85b7c start_wqthread + 8

My code

    private func encodeStringTo64(fromString: String) -> String? {
        let plainData = fromString.data(using: .utf8)
        return plainData?.base64EncodedString(options: [])
    }

Call foundation method

extension Data {
    ...

    /// Returns a Base-64 encoded string.
    ///
    /// - parameter options: The options to use for the encoding. Default value is `[]`.
    /// - returns: The Base-64 encoded string.
    @inlinable public func base64EncodedString(options: Data.Base64EncodingOptions = []) -> String
}

Description

This foundation method don't have throws, but it throw an exception. It will cause a crash.

  • extension Data { /// Returns a Base-64 encoded string. /// /// - parameter options: The options to use for the encoding. Default value is []. /// - returns: The Base-64 encoded string. @inlinable public func base64EncodedString(options: Data.Base64EncodingOptions = []) -> String }

    This method don't have "throws" and not declare that it will throw an exception.

Add a Comment

Replies

Version 15.0.1 (15A507)

Hardware Model:      iPhone14,7
Code Type:           ARM-64 (Native)
Role: Foreground 
Parent Process: launchd [1]
OS Version: iPhone OS 16.6 (20G75) 
Release Type: User 
Baseband Version: 1.80.02 
Report Version: 104

Exception Type: EXC_CRASH (SIGABRT)
Triggered by Thread: 26
Application Specific Information: abort() called

Last Exception Backtrace:
0   CoreFoundation                	       0x1b3c54cb4 __exceptionPreprocess + 164
1   libobjc.A.dylib               	       0x1accf03d0 objc_exception_throw + 60
2   Foundation                    	       0x1adf6950c -[NSData(NSData) _base64EncodingAsString:withOptions:] + 1072
3   Foundation                    	       0x1ae2cf8bc Data.base64EncodedString(options:) + 640
...
6   libdispatch.dylib             	       0x1bb11a320 _dispatch_call_block_and_release + 32
7   libdispatch.dylib             	       0x1bb11beac _dispatch_client_callout + 20
8   libdispatch.dylib             	       0x1bb11ef8c _dispatch_queue_override_invoke + 788
9   libdispatch.dylib             	       0x1bb12d944 _dispatch_root_queue_drain + 396
10  libdispatch.dylib             	       0x1bb12e158 _dispatch_worker_thread2 + 164
11  libsystem_pthread.dylib       	       0x213a85da0 _pthread_wqthread + 228

Thread 26 name:   Dispatch queue: com.apple.root.default-qos
Thread 26 Crashed:
0   libsystem_kernel.dylib        	       0x1f2c11578 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x213a8c118 pthread_kill + 268
2   libsystem_c.dylib             	       0x1bb17c178 abort + 180
3   libc++abi.dylib               	       0x2139cabf8 abort_message + 132
4   libc++abi.dylib               	       0x2139ba444 demangling_terminate_handler() + 348
5   libobjc.A.dylib               	       0x1accf5ea4 _objc_terminate() + 144
6   libc++abi.dylib               	       0x2139c9fbc std::__terminate(void (*)()) + 16
7   libc++abi.dylib               	       0x2139c9f60 std::terminate() + 56
8   libdispatch.dylib             	       0x1bb11bec0 _dispatch_client_callout + 40
9   libdispatch.dylib             	       0x1bb11ef8c _dispatch_queue_override_invoke + 788
10  libdispatch.dylib             	       0x1bb12d944 _dispatch_root_queue_drain + 396
11  libdispatch.dylib             	       0x1bb12e158 _dispatch_worker_thread2 + 164
12  libsystem_pthread.dylib       	       0x213a85da0 _pthread_wqthread + 228
13  libsystem_pthread.dylib       	       0x213a85b7c start_wqthread + 8
12  libsystem_pthread.dylib       	       0x213a85b7c start_wqthread + 8

call method

my code

    private func encodeStringTo64(fromString: String) -> String? {
        let plainData = fromString.data(using: .utf8)
        return plainData?.base64EncodedString(options: [])
    }

foundation method

extension Data {
    ...

    /// Returns a Base-64 encoded string.
    ///
    /// - parameter options: The options to use for the encoding. Default value is `[]`.
    /// - returns: The Base-64 encoded string.
    @inlinable public func base64EncodedString(options: Data.Base64EncodingOptions = []) -> String
}

This foundation method don't have throws, but it throw an exception.

This foundation method don't have throws, but it throw an exception.

You are mixing up Swift error and Objective-C language exceptions. I explain that difference in What is an exception?

Does this crash happen all the time? Or are you trying to debug a crash report coming in from the field?

Also, please post a full crash report; instructions are in Posting a Crash Report.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"