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:
1 parent
1040f47542
commit
e42855867d
1 file changed
+39
-20
+39
-20
@@ -181,13 +181,20 @@ static NSArray<NSString *> * parse(NSString * string, NSUInteger count, BOOL mkv
|
||||
NSScanner *sc = [NSScanner scannerWithString:string];
|
||||
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]) {
|
||||
[sc scanString:@"Dialogue:" intoString:nil];
|
||||
|
||||
NSString *value;
|
||||
|
||||
for (NSUInteger index = 0; index < count - 1; index += 1) {
|
||||
if ([sc scanUpToString:@"," intoString:&value]) {
|
||||
if ([sc scanUpToString:@"," intoString:&value] && value) {
|
||||
[valuesArray addObject:value];
|
||||
}
|
||||
else {
|
||||
@@ -322,24 +329,30 @@ static NSMutableString *STStandardizeStringNewlines(NSString *str)
|
||||
|
||||
- (void)parseHeader:(NSString *)header
|
||||
{
|
||||
NSString *info;
|
||||
NSString *styles;
|
||||
NSString *format;
|
||||
|
||||
NSScanner *sc = [NSScanner scannerWithString:header];
|
||||
[sc setCharactersToBeSkipped:nil];
|
||||
|
||||
if ([sc scanUpToString:@"[V4+ Styles]" intoString:&info]) {
|
||||
_info = [self parseInfo:info];
|
||||
// A failed scanUpToString: consumes the whole string here, so the
|
||||
// section markers are located explicitly: a file without a
|
||||
// "[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
|
||||
// [Events] was never found — every Dialogue line then parsed to nothing.
|
||||
NSUInteger stylesLoc = [header rangeOfString:@"[V4+ Styles]"].location;
|
||||
NSUInteger eventsLoc = [header rangeOfString:@"[Events]"].location;
|
||||
if (stylesLoc != NSNotFound && (eventsLoc == NSNotFound || stylesLoc > eventsLoc)) {
|
||||
stylesLoc = NSNotFound; // a styles marker inside/after Events is not the styles header
|
||||
}
|
||||
|
||||
if ([sc scanUpToString:@"[Events]" intoString:&styles]) {
|
||||
_styles = [self parseStyles:styles];
|
||||
if (stylesLoc != NSNotFound) {
|
||||
_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 (format.length) {
|
||||
_format = [self parseFormat:format];
|
||||
if (eventsLoc != NSNotFound) {
|
||||
_format = [self parseFormat:[header substringFromIndex:eventsLoc]];
|
||||
} 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];
|
||||
NSString *formats;
|
||||
if ([sc scanUpToString:@"\n" intoString:&formats]) {
|
||||
NSScanner *formatScanner = [NSScanner scannerWithString:formats];
|
||||
NSString *formatName;
|
||||
while ([formatScanner scanUpToString:@"," intoString:&formatName]) {
|
||||
[formatScanner scanString:@"," intoString:nil];
|
||||
[formatsArray addObject:formatName];
|
||||
// Split on commas and trim: a scanUpToString:@"," loop silently
|
||||
// drops the final field (no trailing comma), and SSA Format
|
||||
// lines separate names with ", " — the leading space made every
|
||||
// name (" Start", " Text", …) fail its comparison in
|
||||
// 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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user