MailCore / mailcore2

MailCore 2 provide a simple and asynchronous API to work with e-mail protocols IMAP, POP and SMTP. The API has been redesigned from ground up.
Other
2.61k stars 627 forks source link

BAD_ACCESS: String::hasSuffix #141

Closed jianfengqiu closed 11 years ago

jianfengqiu commented 11 years ago

screen shot 2013-06-23 at 2 44 08 pm

CodaFi commented 11 years ago

Looks like one of those variables dereferenced a bad pointer. Are you sure all the variables involved are of class MailCore::String?

dinhvh commented 11 years ago

How do you reproduce the issue?

jianfengqiu commented 11 years ago

@dinhviethoa run below code several time can reproduce it.

for (int i = 0; i < _loadedMessages.count ; i++) {
            MCOIMAPMessage *msg = [_loadedMessages objectAtIndex:[_loadedMessages count]-1-i];
            MCOMessageParser * parser = [[_vcMailboxes.dictMessagesContent objectForKey:[_dictAccount objectForKey:@"email"]] objectForKey:[NSString stringWithFormat:@"%u",msg.uid]];
            NSArray *emailContentArray = [self flattenHtmlAndRemoveHeader:[parser htmlRenderingWithDelegate:nil]];
            [_loadedEmailContent setObject:emailContentArray forKey:[NSString stringWithFormat:@"%u",msg.uid]];
        }

- (NSArray *)flattenHtmlAndRemoveHeader:(NSString *)string
{
    NSString *tempString = [[string mco_flattenHTML] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"tempString:%@",tempString);
    // remove head info
    NSRange range = [tempString rangeOfString:@"PDT"];
    NSUInteger end = range.location + range.length;
    NSString *stringWithoutHeader = [tempString substringFromIndex:end];

    NSArray *stringArray = [stringWithoutHeader componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSMutableArray *finalStringArray = [NSMutableArray array];
    for (NSString *subString in stringArray) {
        NSString *tempString = [subString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [finalStringArray addObject:tempString];
    }
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSString *string in finalStringArray) {
        if (![string isEqualToString:@""]) {
            [finalArray addObject:string];
        }
    }

    NSLog(@"resultString: %@",finalArray);
    return finalArray;
}
dinhvh commented 11 years ago

Could you minimize that code to extract only what makes it crash? Additionally, if it's due to a given message, could you attach it? Thanks.

jianfengqiu commented 11 years ago

I think this method [string mco_flattenHTML] cause the crash. Especially call this method in a for loop. It's not due to a given message.

dinhvh commented 11 years ago

Which string does it fail with?
The string is probably generated from the content of a message. Could you find the message that cause the failure? Thanks.

Hoà V. Dinh

On Sunday, June 23, 2013 at 7:30 PM, jianfengqiu wrote:

I think this method [string mco_flattenHTML] cause the crash. It's not due to a given message.

— Reply to this email directly or view it on GitHub (https://github.com/MailCore/mailcore2/issues/141#issuecomment-19886358).

jianfengqiu commented 11 years ago

Sorry I can not catch which string fail. But the string is the return from [parser htmlRenderingWithDelegate:nil]

dinhvh commented 11 years ago

Could you NSLog() each string (or message) before you pass them to mco_flattenHTML? The last one you sent through mco_flattenHTML before crashing should show. The last one is the string that will cause the failure. Copy-paste the content of the string (or the source of the message) to a file and send it to me. Thanks.

jianfengqiu commented 11 years ago

ok, thanks!

jianfengqiu commented 11 years ago

I can not reproduce it now. I need more test about it.

jianfengqiu commented 11 years ago

@dinhviethoa I catched the string which I pass to mco_flattenHTML, then crash shows up. But it is too long to paste here. Can I email it to you? My email is jianfengqiu@gmail.com Thank you very much.

dinhvh commented 11 years ago

Can you reproduce it?

jianfengqiu commented 11 years ago

@dinhviethoa Yes, I can. It only happened when the htmlString is very long. And also I used your approach to minimize the code and test it. The result was no crash. So I used another flattenHtml method to test and find that works great. I have no idea about it. I will test more to figure out why this issue happened. If I found interesting something, I will email you.

dinhvh commented 11 years ago

If that results in no crash, you should reintroduce back the code you removed progressively until it crashes.

lostincode commented 11 years ago

I'm getting this error randomly too. I will try and locate a sample message for you.

dinhvh commented 11 years ago

When you get that crash, could you get the value of mUnicodeChars, mLength, suffix->mUnicodeChars, suffix->mLength? Thanks!

dinhvh commented 11 years ago

Any update on it?