Uploaded image for project: 'Swift'
  1. Swift
  2. SR-8528

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'The data couldn’t be read because it isn’t in the correct format.'

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Foundation
    • Labels:
    • Environment:

      Swift 4.1.2, Swift 4.2
      macOS 10.13.6

       

      Description

      While using `NSKeyedUnarchiver` with `Codable` I found the swift-only method `

      decodeTopLevelDecodable` does not properly catch decoding exceptions, as documented.

      If the archive is not a valid property list, this method throws the

      DecodingError.dataCorrupted(_:)

      error. If a value within the archive fails to decode, this method throws the corresponding error.

       
      The following, Swift-only code can be used to demonstrate the issue.

      import Foundation
      
      struct Person: Codable {
         let name: String
         let age: Int
      }
      
      let archiver = NSKeyedArchiver()
      try archiver.encodeEncodable(Person(name: "Foo", age: 42), forKey: "foo")
      let data = archiver.encodedData
      
      // Successful decoding to Person
      let unarchiver = NSKeyedUnarchiver(forReadingWith: data)
      _ = try unarchiver.decodeTopLevelDecodable(Person.self, forKey: "foo")
      
      // Unssuccesful decoding to String should throw and catch exception
      do {
          let badstring = try unarchiver.decodeTopLevelDecodable(String.self, forKey: "foo")
          print("FAIL: Should have thrown exception")
      }
      catch {
          print("SUCCESS: Exception handled")
      }
      

      The above code can be run from the command line with `$ swift archiver.swift`. The program execution will be terminated, as the `NSInvalidUnarchiveOperationException` exception is not caught by the `do/catch` expression.

      Issue:
      Application is terminated, thrown exception cannot be handled by Swift code.

      Expected:
      Application continues running after exception is handled.

      Full error message when running `$ swift archiver.swift`

      $ swift archiver.swift 
      2018-08-13 08:48:10.790 swift[54396:5151039] *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'The data couldn’t be read because it isn’t in the correct format.'
      *** First throw call stack:
      (
      	0   CoreFoundation                      0x00007fff315f12db __exceptionPreprocess + 171
      	1   libobjc.A.dylib                     0x00007fff587a1c76 objc_exception_throw + 48
      	2   Foundation                          0x00007fff3376b077 -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 0
      	3   Foundation                          0x00007fff3376b371 -[NSCoder(Exceptions) __failWithExternalError:] + 161
      	4   libswiftFoundation.dylib            0x00000001194c96fb $SSo17NSKeyedUnarchiverC10FoundationE23decodeTopLevelDecodable_6forKeyxSgxm_SStKSeRzlF + 363
      	5   ???                                 0x0000000116ca3641 0x0 + 4677318209
      	6   swift                               0x00000001100b338d _ZN4llvm5MCJIT11runFunctionEPNS_8FunctionENS_8ArrayRefINS_12GenericValueEEE + 365
      	7   swift                               0x00000001100b9d8c _ZN4llvm15ExecutionEngine17runFunctionAsMainEPNS_8FunctionERKNSt3__16vectorINS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEENS8_ISA_EEEEPKPKc + 1004
      	8   swift                               0x000000010f3356c3 _ZL14performCompileRN5swift16CompilerInstanceERNS_18CompilerInvocationEN4llvm8ArrayRefIPKcEERiPNS_16FrontendObserverEPNS_20UnifiedStatsReporterE + 52643
      	9   swift                               0x000000010f325425 _ZN5swift15performFrontendEN4llvm8ArrayRefIPKcEES3_PvPNS_16FrontendObserverE + 7717
      	10  swift                               0x000000010f2cb175 main + 1349
      	11  libdyld.dylib                       0x00007fff593bb015 start + 1
      )
      libc++abi.dylib: terminating with uncaught exception of type NSException
      0  swift                    0x0000000113071c5a PrintStackTraceSignalHandler(void*) + 42
      1  swift                    0x00000001130713fe SignalHandler(int) + 302
      2  libsystem_platform.dylib 0x00007fff596c9f5a _sigtramp + 26
      3  libsystem_platform.dylib 0x0000000000020008 _sigtramp + 2794807496
      4  libsystem_c.dylib        0x00007fff594671ae abort + 127
      5  libc++abi.dylib          0x00007fff57361f8f __cxa_bad_cast + 0
      6  libc++abi.dylib          0x00007fff5736212b default_unexpected_handler() + 0
      7  libobjc.A.dylib          0x00007fff587a3ea3 _objc_terminate() + 97
      8  libc++abi.dylib          0x00007fff5737d7c9 std::__terminate(void (*)()) + 8
      9  libc++abi.dylib          0x00007fff5737d26f __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*) + 0
      10 libobjc.A.dylib          0x00007fff587a1da5 objc_exception_throw + 351
      11 Foundation               0x00007fff3376b077 -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 0
      12 Foundation               0x00007fff3376b371 -[NSCoder(Exceptions) __failWithExternalError:] + 161
      13 libswiftFoundation.dylib 0x00000001194c96fb $SSo17NSKeyedUnarchiverC10FoundationE23decodeTopLevelDecodable_6forKeyxSgxm_SStKSeRzlF + 363
      14 libswiftFoundation.dylib 0x0000000116ca3641 $SSo17NSKeyedUnarchiverC10FoundationE23decodeTopLevelDecodable_6forKeyxSgxm_SStKSeRzlF + 4252868785
      15 swift                    0x00000001100b338d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 365
      16 swift                    0x00000001100b9d8c llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 1004
      17 swift                    0x000000010f3356c3 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 52643
      18 swift                    0x000000010f325425 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
      19 swift                    0x000000010f2cb175 main + 1349
      20 libdyld.dylib            0x00007fff593bb015 start + 1
      Stack dump:
      0.	Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret archiver.swift -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name archiver 
      Abort trap: 6
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              eneko Eneko Alonso
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: