Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusing encoding exception in NSTextStorage

I have an NSDocument subclass for a simple text editor (using Lion's new document based app template, with few customisations), and I'm encountering a strange bug loading the file content into the text storage.

Here's my code:

- (void)loadTextContentIntoStorage
{
  if (!self.textStorage || !textContentToLoad)
    return;

  ...

  [self.textStorage beginEditing];

//  NSLog(@"storage: %@ length: %lu textContent: %@", self.textStorage, (unsigned long)self.textStorage.length, textContentToLoad);
//  [self.textStorage replaceCharactersInRange:NSMakeRange(0, self.textStorage.length) withString:textContentToLoad];
  [self.textStorage replaceCharactersInRange:NSMakeRange(0, 0) withString:@"hello world"];

  ..

  [self.textStorage endEditing];
}

The bug happens when I:

  • run the app in Xcode (as a debug build)
  • open any document
  • quit the app (without closing the document)
  • run the app again from Xcode

It crashes in -replaceCharactersInRange:withString: with "Unable to convert bytes in string 0x10004d430 to _NSCStringEncoding".

But it only happens on every second launch of the app (a third launch will not crash, and it will automatically re-open the document it crashed trying to open the previous time). It also only happens when I run the app from Xcode. Release builds have never crashed on launch.

I thought it might be an encoding issue with the auto-save system, but it even crashes when I comment out that code and just load @"hello world" string into the text view (as shown in the above code). Similarly, the commented out NSLog() doesn't show anything weird. The text storage is valid (loaded from the xib file), the text storage length is 0, and the textContent is the contents of the file being opened.

--- EDIT ---

I've learned this problem is related in some way to the com.apple.security.app-sandbox entitlement. If entitlements / sandbox are enabled, then my app does not crash. If either entitlements or the app-sandbox feature are disabled, then my app crashes on every second launch trying to restore previously opened documents.

I had only noticed it crashing when doing a build/run from inside xcode, because that was my only build configuration with the sandbox disabled.

--- /EDIT ---

Does anyone have any ideas? The full exception follows, and the full source code is on github: https://github.com/abhibeckert/Dux/blob/master/Dux/DuxTextView.m

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to convert bytes in string 0x10004d430 to _NSCStringEncoding'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff84afb286 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff88991d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff84afb0ba +[NSException raise:format:arguments:] + 106
    3   CoreFoundation                      0x00007fff84afb044 +[NSException raise:format:] + 116
    4   Foundation                          0x00007fff835bfae4 copyFromStringToStorage + 262
    5   Foundation                          0x00007fff835bf979 -[NSBigMutableString replaceCharactersInRange:withString:] + 1000
    6   Foundation                          0x00007fff835bc3f7 -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 375
    7   AppKit                              0x00007fff86149e14 -[NSConcreteTextStorage replaceCharactersInRange:withString:] + 81
    8   Dux                                 0x0000000100002f9a -[MyTextDocument loadTextContentIntoStorage] + 1338
    9   Dux                                 0x00000001000022a0 -[MyTextDocument windowControllerDidLoadNib:] + 640
    10  AppKit                              0x00007fff860f1328 -[NSWindowController _windowDidLoad] + 667
    11  AppKit                              0x00007fff860e89a3 -[NSWindowController window] + 109
    12  AppKit                              0x00007fff8615d761 -[NSDocument windowForSheet] + 86
    13  AppKit                              0x00007fff860e82c4 -[NSDocument _shouldShowAutosaveButtonForWindow:] + 50
    14  AppKit                              0x00007fff860e7fbb -[NSWindowController setDocument:] + 237
    15  AppKit                              0x00007fff8629c9b6 -[NSDocument makeWindowControllers] + 139
    16  AppKit                              0x00007fff8615d555 -[NSDocument(NSPersistentUISupport) restoreDocumentWindowWithIdentifier:state:completionHandler:] + 90
    17  AppKit                              0x00007fff8615d4aa -[NSDocumentControllerPersistentRestoration loadedDocument:forAutoID:] + 179
    18  AppKit                              0x00007fff8615cfbe __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_8 + 187
    19  AppKit                              0x00007fff86148e14 __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_5 + 163
    20  AppKit                              0x00007fff86148d5f __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_4 + 697
    21  AppKit                              0x00007fff86148aa1 -[NSDocumentController _openDocumentWithContentsOfURL:usingProcedure:] + 530
    22  AppKit                              0x00007fff8614868d __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_3 + 242
    23  libdispatch.dylib                   0x00007fff8bbbe8ba _dispatch_call_block_and_release + 18
    24  libdispatch.dylib                   0x00007fff8bbc072a _dispatch_main_queue_callback_4CF + 308
    25  CoreFoundation                      0x00007fff84a904dc __CFRunLoopRun + 1724
    26  CoreFoundation                      0x00007fff84a8fae6 CFRunLoopRunSpecific + 230
    27  HIToolbox                           0x00007fff8852f3d3 RunCurrentEventLoopInMode + 277
    28  HIToolbox                           0x00007fff8853663d ReceiveNextEventCommon + 355
    29  HIToolbox                           0x00007fff885364ca BlockUntilNextEventMatchingListInMode + 62
    30  AppKit                              0x00007fff85ef23f1 _DPSNextEvent + 659
    31  AppKit                              0x00007fff85ef1cf5 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
    32  AppKit                              0x00007fff85eee62d -[NSApplication run] + 470
    33  AppKit                              0x00007fff8616d80c NSApplicationMain + 867
    34  Dux                                 0x0000000100001e32 main + 34
    35  Dux                                 0x0000000100001e04 start + 52
    36  ???                                 0x0000000000000003 0x0 + 3
)
like image 690
Abhi Beckert Avatar asked Dec 06 '11 22:12

Abhi Beckert


2 Answers

I admit, this question stumped me for quite awhile. I think I've finally found a repro, though.

Looking through the GNUstep code that emulates this behavior, I found the following source code:

if (enc == NSASCIIStringEncoding
    && isByteEncoding(internalEncoding))
    {
      unsigned  i;

      if (bytes > self->_count)
    {
      bytes = self->_count;
    }
      for (i = 0; i < bytes; i++)
    {
      unsigned char c = self->_contents.c[i];

      if (c > 127)
        {
          [NSException raise: NSCharacterConversionException
              format: @"unable to convert to encoding"];
        }
      buffer[i] = c;
    }
      buffer[bytes] = '\0';
      if (bytes < self->_count)
    {
      return NO;
    }
      return YES;
    }
...

Obviously, this code isn't one-to-one with your error message. However, I note this from your error repro:

reason: 'Unable to convert bytes in string 0x10004d430 to_NSCStringEncoding'

Well, we all know what a C String is, and if you use the wrong encoding method (for example, Wide where ASCII is expected or vice versa), you're going to end up with exactly this problem. Thus: have you tried varying ASCII and Wide data types in your application to fix this issue?

Due to how common this behavior happens to be, it very likely indicates that it's a handled case by the application sandbox. I can't yet find documentation to this effect, however. This should be something explicitly noted by sandboxd, as I raised in the question errata.

like image 149
MrGomez Avatar answered Nov 13 '22 22:11

MrGomez


I got same exception on 10.9.2.

 Unable to convert bytes in string 0x60800026cd40 to _NSCStringEncoding
 (
    0   CoreFoundation                      0x00007fff8dec825c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff81de0e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8dec810c +[NSException raise:format:] + 204
    3   Foundation                          0x00007fff8d94dd2b copyFromStringToStorage + 223
    4   Foundation                          0x00007fff8d94db9e -[NSBigMutableString replaceCharactersInRange:withString:] + 1081
    5   Foundation                          0x00007fff8d94d738 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 296
    6   AppKit                              0x00007fff88d7889c -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 77
    9   myApp                            0x00000001035d3fff myApp + 12287
    10  CoreFoundation                      0x00007fff8de96e0c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    11  CoreFoundation                      0x00007fff8dd8a82d _CFXNotificationPost + 2893
    12  Foundation                          0x00007fff8d8f8e4a -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
    13  AppKit                              0x00007fff88b99cde -[NSApplication finishLaunching] + 440
    14  AppKit                              0x00007fff88b997f3 -[NSApplication run] + 128
    15  AppKit                              0x00007fff88b84783 NSApplicationMain + 940
    16  libdyld.dylib                       0x00007fff81aa65fd start + 1
    17  ???                                 0x0000000000000001 0x0 + 1
)

I was using NSTextStorage in applicationWillFinishLaunching. I have moved that to applicationDidFinishLaunching and its working fine.

like image 30
Parag Bafna Avatar answered Nov 13 '22 22:11

Parag Bafna