title: Occasional Crash Setting UILabel Text - NSString substringWithRange
body: <p>I have noticed a few crash reports from the App store recently.  However, I can't recreate the error using the simulator or on my iPhone 5.</p>
  
  <p>One of the users emailed me to report the problem and provided some details. The App crashed for this user when they tried to open the view controller which displays a list of recordings. This is is the view controller which failed (BRRecordingsViewController)</p>
  
  <p>The line which failed is where the UILabel text "recordingName.text" is set to a substring of the filename string. </p>
  
  <p>The filename string would typically be something like this:</p>
  
  <pre><code>VoCo_DATE_2015-05-14 16:14:55_NAME_Arpeggio #02
  </code></pre>
  
  <p>"Arpeggio #02" is the name of the track. (The text preceding this is a unique identifier and allows tracks to be displayed in order of creation date/time)</p>
  
  <p>The filename will never be empty - it will at least contain the text </p>
  
  <pre><code>VoCo_DATE_2015-05-14 16:14:55_NAME_
  </code></pre>
  
  <p>The substring would in this case result in this:</p>
  
  <pre><code>Arpeggio #02
  </code></pre>
  
  <p>The track name can be empty. This does not cause the program to fail.</p>
  
  <p>Here is the section of code which is triggering the crash:</p>
  
  <pre><code>UILabel *recordingName = (UILabel *)[cell viewWithTag:100];
  NSRange rangeNameTag = [fileNameWithoutExt rangeOfString:@"_NAME_"];
  NSRange rangeOfRecordingName = NSMakeRange(rangeNameTag.location+6, fileNameWithoutExt.length-6-rangeNameTag.location);
  recordingName.text = [fileNameWithoutExt substringWithRange: rangeOfRecordingName];
  </code></pre>
  
  <p>Here's the exception I get from the App store crash log.</p>
  
  <p><img src="https://i.stack.imgur.com/51Ewi.png" alt="Last Exception Backtrace"></p>
  
  <p>I cannot recreate the error and therefore can not be entirely sure of the steps leading up to the crash.</p>
  
  <p>By changing the code and forcing bad data into the string I can achieve a crash. </p>
  
  <pre><code>*** -[__NSCFConstantString substringWithRange:]: Range {9223372036854775813, 9223372036854775813} out of bounds; string length 10. This will become an exception for apps linked after 10.10 and iOS 8. Warning shown once per app execution
  </code></pre>