{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/xctool/issues/555","labels_url":"https://api.github.com/repos/facebook/xctool/issues/555/labels{/name}","comments_url":"https://api.github.com/repos/facebook/xctool/issues/555/comments","events_url":"https://api.github.com/repos/facebook/xctool/issues/555/events","html_url":"https://github.com/facebook/xctool/issues/555","id":100732265,"number":555,"title":"otest-shim: Deadlock due to StripAnsi nil argument exception in single byte input","user":{"login":"joamaki","id":1102396,"avatar_url":"https://avatars.githubusercontent.com/u/1102396?v=3","gravatar_id":"","url":"https://api.github.com/users/joamaki","html_url":"https://github.com/joamaki","followers_url":"https://api.github.com/users/joamaki/followers","following_url":"https://api.github.com/users/joamaki/following{/other_user}","gists_url":"https://api.github.com/users/joamaki/gists{/gist_id}","starred_url":"https://api.github.com/users/joamaki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joamaki/subscriptions","organizations_url":"https://api.github.com/users/joamaki/orgs","repos_url":"https://api.github.com/users/joamaki/repos","events_url":"https://api.github.com/users/joamaki/events{/privacy}","received_events_url":"https://api.github.com/users/joamaki/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-13T10:17:56Z","updated_at":"2015-08-14T20:09:29Z","closed_at":null,"body":"In some cases the test output is written one character at a time and when printing the\r\ndefault NSError localized description \"The operation couldn’t be completed\", StripAnsi crashes\r\nwhen the input is the accent in \"couldn't\", which has the octal code \\342. This in turn causes a recursive call into the intercepted write due to logging attempt by the handleException and a deadlock in the xctool.events queue.\r\n\r\nProgram to crash StripAnsi:\r\n```\r\nint main() {\r\n  NSString *output = [[NSString alloc] initWithBytes:\"\\342\" length:1 encoding:NSUTF8StringEncoding];\r\n  NSLog(@\"res: %@\", StripAnsi(output));\r\n}\r\n```\r\n\r\nThe exception thrown:\r\n```\r\n2015-08-13 12:06:33.068 test[14839:4584644] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument'\r\n*** First throw call stack:\r\n(\r\n\t0   CoreFoundation                      0x00007fff93dcf03c __exceptionPreprocess + 172\r\n\t1   libobjc.A.dylib                     0x00007fff94dcb76e objc_exception_throw + 43\r\n\t2   CoreFoundation                      0x00007fff93dceeed +[NSException raise:format:] + 205\r\n\t3   Foundation                          0x00007fff9694e499 -[NSRegularExpression(NSMatching) enumerateMatchesInString:options:range:usingBlock:] + 232\r\n\t4   Foundation                          0x00007fff9694e398 -[NSRegularExpression(NSMatching) matchesInString:options:range:] + 162\r\n\t5   Foundation                          0x00007fff96a0547f -[NSRegularExpression(NSReplacement) stringByReplacingMatchesInString:options:range:withTemplate:] + 300\r\n\t6   test                                0x000000010e011bee StripAnsi + 302\r\n\t7   test                                0x000000010e011d92 main + 162\r\n\t8   libdyld.dylib                       0x00007fff963be5c9 start + 1\r\n\t9   ???                                 0x0000000000000001 0x0 + 1\r\n)\r\n```\r\n\r\nBacktrace from the xctool.events thread showing the deadlock:\r\n```\r\n  thread #9: tid = 0x45fdfd, 0x00007fff8ba4b51a libsystem_kernel.dylib`semaphore_wait_trap + 10, queue = 'xctool.events'\r\n    frame #0: 0x00007fff8ba4b51a libsystem_kernel.dylib`semaphore_wait_trap + 10\r\n    frame #1: 0x00007fff94423c5b libsystem_platform.dylib`_os_semaphore_wait + 16\r\n    frame #2: 0x00007fff93c4b557 libdispatch.dylib`_dispatch_barrier_sync_f_slow + 597\r\n    frame #3: 0x00000001029cf3ca otest-shim-osx.dylib`__writev(fildes=<unavailable>, iov=0x00000001075ec6c0, iovcnt=2) + 116 at otest-shim.m:574\r\n    frame #4: 0x00007fff93d0ea83 CoreFoundation`__CFLogCString + 1011\r\n    frame #5: 0x00007fff93db280f CoreFoundation`_CFLogvEx2 + 271\r\n    frame #6: 0x00007fff93d68285 CoreFoundation`CFLog + 165\r\n    frame #7: 0x00007fff93dcf598 CoreFoundation`__handleUncaughtException + 888\r\n    frame #8: 0x00007fff94dcf7cd libobjc.A.dylib`_objc_terminate() + 94\r\n    frame #9: 0x00007fff8d50a0a1 libc++abi.dylib`std::__terminate(void (*)()) + 8\r\n    frame #10: 0x00007fff8d50a113 libc++abi.dylib`std::terminate() + 51\r\n    frame #11: 0x00007fff94dcf5ff libobjc.A.dylib`objc_terminate + 9\r\n    frame #12: 0x00007fff93c41c27 libdispatch.dylib`_dispatch_client_callout + 28\r\n    frame #13: 0x00007fff93c42e5e libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 57\r\n    frame #14: 0x00000001029cf1d1 otest-shim-osx.dylib`___write_nocancel(fildes=<unavailable>, buf=0x00007fff788f23cf, nbyte=1) + 118 at otest-shim.m:450\r\n    frame #15: 0x00007fff8d49a9ed libsystem_c.dylib`_swrite + 87\r\n    frame #16: 0x00007fff8d4934a7 libsystem_c.dylib`__sflush + 101\r\n    frame #17: 0x00007fff8d4aafb1 libsystem_c.dylib`__swbuf + 177\r\n    frame #18: 0x00007fff8d499bae libsystem_c.dylib`putchar + 91\r\n    frame #19: 0x0000000106b174b0 libswiftCore.dylib`function signature specialization <Arg[0] = Owned To Guaranteed and Exploded, Arg[1] = Dead> of Swift._Stdout.write (inout Swift._Stdout)(Swift.String) -> () + 192\r\n    frame #20: 0x0000000106ad877e libswiftCore.dylib`protocol witness for Swift.OutputStreamType.write <A : Swift.OutputStreamType>(inout Swift.OutputStreamType.Self)(Swift.String) -> () in conformance Swift._Stdout : Swift.OutputStreamType in Swift + 14\r\n    frame #21: 0x0000000106a5fd0c libswiftCore.dylib`Swift.print <A, B : Swift.OutputStreamType>(A, inout B) -> () + 492\r\n    frame #22: 0x000000010675cf8d XCGLogger`function signature specialization <Arg[0] = Dead, Arg[1] = Value Promoted from InOut> of XCGLogger.XCGConsoleLogDestination.(fullLogMessage=Swift.String at 0x00000001075ecd88) -> (XCGLogger.XCGLogDetails) -> ()).(closure #1) + 93 at XCGLogger.swift:87\r\n    frame #23: 0x000000010675cfc7 XCGLogger`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39 at XCGLogger.swift:0\r\n```\r\n\r\nNot sure what would be a sensible workaround as the problem seems to be in Foundation rather\r\nthen in otest-shim. Perhaps filtering non-ascii characters from the input stream or buffering?"},"comment":{"url":"https://api.github.com/repos/facebook/xctool/issues/comments/131225499","html_url":"https://github.com/facebook/xctool/issues/555#issuecomment-131225499","issue_url":"https://api.github.com/repos/facebook/xctool/issues/555","id":131225499,"user":{"login":"ExtremeMan","id":1044236,"avatar_url":"https://avatars.githubusercontent.com/u/1044236?v=3","gravatar_id":"","url":"https://api.github.com/users/ExtremeMan","html_url":"https://github.com/ExtremeMan","followers_url":"https://api.github.com/users/ExtremeMan/followers","following_url":"https://api.github.com/users/ExtremeMan/following{/other_user}","gists_url":"https://api.github.com/users/ExtremeMan/gists{/gist_id}","starred_url":"https://api.github.com/users/ExtremeMan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ExtremeMan/subscriptions","organizations_url":"https://api.github.com/users/ExtremeMan/orgs","repos_url":"https://api.github.com/users/ExtremeMan/repos","events_url":"https://api.github.com/users/ExtremeMan/events{/privacy}","received_events_url":"https://api.github.com/users/ExtremeMan/received_events","type":"User","site_admin":false},"created_at":"2015-08-14T20:09:29Z","updated_at":"2015-08-14T20:09:29Z","body":"Thanks for a great bug report. I was able to reproduce deadlock with this test:\r\n\r\n    - (void)testHandlingOfUnicodeStrings\r\n    {\r\n      fprintf(stdout, \"---\");\r\n      sleep(2);\r\n      fprintf(stdout, \"\\342\");\r\n      sleep(2);\r\n    }\r\n\r\n\r\nWe've already solved similar problems in other parts of xctool: 9a8a4a0262a3faff7ce598a223ebcec3f7f89b77, 7662c449779e0f496ab41016b5f427691e521b2e.\r\nI will try to repeat fixes here as well."}}