Skip to content

Commit 1e915b7

Browse files
committed
Fixed a bug which caused a crash if SSYTextCodec attempted to decode a file which did not contain any \r carriage return characters.
1 parent 63e927e commit 1e915b7

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

SSYTextCodec.m

+20-12
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,29 @@ + (BOOL)decodeTextData:(NSData*)fileDataIn
2323
}
2424

2525
// Discover and set line endings type (dos, old Mac or unix)
26-
NSString* newLinesFound = nil ;
26+
NSString* newLinesFound = nil;
2727
if (!error) {
28+
NSInteger nLocation = stringASCII ? [stringASCII rangeOfString:@"\n"].location : 0 ;
2829
NSInteger rLocation = stringASCII ? [stringASCII rangeOfString:@"\r"].location : 0 ;
29-
NSInteger nLocation = stringASCII ? [stringASCII rangeOfString:@"\n"].location : 0 ;
30-
30+
3131
// Set to whichever occurs first in the file: \r\n, \r or \n
32-
if (nLocation == rLocation + 1) {
33-
newLinesFound = @"\r\n" ;
34-
}
35-
else if (rLocation < nLocation) {
36-
newLinesFound = @"\r" ;
37-
}
38-
else {
39-
newLinesFound = @"\n" ;
40-
}
32+
if (nLocation != NSNotFound) {
33+
if (rLocation != NSNotFound) {
34+
if (nLocation == rLocation + 1) {
35+
newLinesFound = @"\r\n" ;
36+
} else if (nLocation < rLocation) {
37+
newLinesFound = @"\n" ;
38+
} else {
39+
newLinesFound = @"\r" ;
40+
}
41+
} else {
42+
newLinesFound = @"\n" ;
43+
}
44+
} else if (rLocation != NSNotFound) {
45+
newLinesFound = @"\r" ;
46+
} else {
47+
newLinesFound = @"\n" ;
48+
}
4149
}
4250

4351
NSString *utfdash8Key = @"content=\"text/html; charset=utf-8" ;

0 commit comments

Comments
 (0)