Fix SSA header parsing for files without a [V4+ Styles] section

Three latent bugs combined into a crash or silently-empty subtitle
import (surfaced by feeding conv.ass to the Matroska subtitle-add
path):

- parseHeader: used NSScanner scanUpToString: for the section
  markers; a failed scanUpToString: returns YES and advances the
  scan location to the end of the string, so a file lacking
  "[V4+ Styles]" left the scanner exhausted and the [Events]
  Format: line was never parsed. Locate the markers with
  rangeOfString: instead.
- The Format line is split on commas but names are separated with
  ", " in the wild; the leading spaces made every field name fail
  its isEqualToString: in MP42SSALine, so no line ever got
  text/timestamps. Split on comma and trim whitespace.
- MP42SSALine's value loop looped to `count - 1` on an NSUInteger;
  with an empty format array that underflows and appends forever
  until the array traps. Guard count == 0.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Scott GarrettandClaude Code committed 2026-11-26 11:53:01 -05:00
1 parent 1040f47542
commit e42855867d
1 file changed
+39 -20
+39 -20
View File
@@ -181,13 +181,20 @@ static NSArray<NSString *> * parse(NSString * string, NSUInteger count, BOOL mkv
NSScanner *sc = [NSScanner scannerWithString:string]; NSScanner *sc = [NSScanner scannerWithString:string];
NSMutableArray<NSString *> *valuesArray = [NSMutableArray array]; NSMutableArray<NSString *> *valuesArray = [NSMutableArray array];
// No field layout (header scan found nothing): there is nothing to split
// into. Without this guard the `count - 1` loop bound below underflows
// and appends empty values until the array traps.
if (count == 0) {
return valuesArray;
}
if (mkvStyle || [sc scanUpToString:@"Dialogue:" intoString:nil] || [sc scanString:@"Dialogue:" intoString:nil]) { if (mkvStyle || [sc scanUpToString:@"Dialogue:" intoString:nil] || [sc scanString:@"Dialogue:" intoString:nil]) {
[sc scanString:@"Dialogue:" intoString:nil]; [sc scanString:@"Dialogue:" intoString:nil];
NSString *value; NSString *value;
for (NSUInteger index = 0; index < count - 1; index += 1) { for (NSUInteger index = 0; index < count - 1; index += 1) {
if ([sc scanUpToString:@"," intoString:&value]) { if ([sc scanUpToString:@"," intoString:&value] && value) {
[valuesArray addObject:value]; [valuesArray addObject:value];
} }
else { else {
@@ -322,24 +329,30 @@ static NSMutableString *STStandardizeStringNewlines(NSString *str)
- (void)parseHeader:(NSString *)header - (void)parseHeader:(NSString *)header
{ {
NSString *info; // A failed scanUpToString: consumes the whole string here, so the
NSString *styles; // section markers are located explicitly: a file without a
NSString *format; // "[V4+ Styles]" section (plain SSA, or no styles at all) used to leave
// the scan position at the end of the document, and the Format: line in
NSScanner *sc = [NSScanner scannerWithString:header]; // [Events] was never found — every Dialogue line then parsed to nothing.
[sc setCharactersToBeSkipped:nil]; NSUInteger stylesLoc = [header rangeOfString:@"[V4+ Styles]"].location;
NSUInteger eventsLoc = [header rangeOfString:@"[Events]"].location;
if ([sc scanUpToString:@"[V4+ Styles]" intoString:&info]) { if (stylesLoc != NSNotFound && (eventsLoc == NSNotFound || stylesLoc > eventsLoc)) {
_info = [self parseInfo:info]; stylesLoc = NSNotFound; // a styles marker inside/after Events is not the styles header
} }
if ([sc scanUpToString:@"[Events]" intoString:&styles]) { if (stylesLoc != NSNotFound) {
_styles = [self parseStyles:styles]; _info = [self parseInfo:[header substringToIndex:stylesLoc]];
NSUInteger stylesEnd = (eventsLoc != NSNotFound) ? eventsLoc : header.length;
_styles = [self parseStyles:[header substringWithRange:NSMakeRange(stylesLoc, stylesEnd - stylesLoc)]];
} else {
NSUInteger stylesEnd = (eventsLoc != NSNotFound) ? eventsLoc : header.length;
_styles = [self parseStyles:[header substringToIndex:stylesEnd]];
} }
format = [sc.string substringFromIndex:sc.scanLocation]; if (eventsLoc != NSNotFound) {
if (format.length) { _format = [self parseFormat:[header substringFromIndex:eventsLoc]];
_format = [self parseFormat:format]; } else if (stylesLoc == NSNotFound) {
_format = [self parseFormat:header]; // no sections at all: last resort
} }
} }
@@ -399,11 +412,17 @@ static NSMutableString *STStandardizeStringNewlines(NSString *str)
[sc scanString:@"Format:" intoString:nil]; [sc scanString:@"Format:" intoString:nil];
NSString *formats; NSString *formats;
if ([sc scanUpToString:@"\n" intoString:&formats]) { if ([sc scanUpToString:@"\n" intoString:&formats]) {
NSScanner *formatScanner = [NSScanner scannerWithString:formats]; // Split on commas and trim: a scanUpToString:@"," loop silently
NSString *formatName; // drops the final field (no trailing comma), and SSA Format
while ([formatScanner scanUpToString:@"," intoString:&formatName]) { // lines separate names with ", " — the leading space made every
[formatScanner scanString:@"," intoString:nil]; // name (" Start", " Text", …) fail its comparison in
[formatsArray addObject:formatName]; // MP42SSALine, so no line ever got text/timestamps.
for (NSString *field in [formats componentsSeparatedByString:@","]) {
NSString *formatName = [field stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (formatName.length) {
[formatsArray addObject:formatName];
}
} }
} }
} }