karastojko / mailio

mailio is a cross platform C++ library for MIME format and SMTP, POP3, IMAP protocols. It is based on the standard C++ 17 and Boost library.
Other
381 stars 102 forks source link

parsing imap quoted string #136 #145

Closed karastojko closed 11 months ago

yjm6560 commented 11 months ago

As i commented in #134, \\ and \" must be converted to \ and " in quoted state. However the modified code seems only handle \". And it will call std::string::back on empty std::string if token is "", which can cause undefined behavior. it would better to consider variable cases.

I think below code may solve the problem.

...
switch (ch)
{
    ...
    case codec::BACKSLASH_CHAR:
    {
        if (atom_state_ == atom_state_t::NONE)
        {
             // create and add cur_token to token_list
             cur_token->atom += ch;
        }
        else if (atom_state_ == atom_state_t::QUOTED)
        {
            if (cur_token->atom.size() > 0 && cur_token->atom.back() == codec::BACKSLASH_CHAR)
            {
                cur_token->atom.back() = ch;
            }
            else
            {
                cur_token->atom += ch;
            }
        }
        else
        {
            // throw exception or ignore or append to cur_token->atom...?
            // In rfc, it seems impossible for the backslash char to come in the middle of the token string.(except in quoted string token)
            // If I were you I would throw an exception.
        }
        break;
    }
    ...
    case QUOTED_ATOM:
    {
        ...
        else if (atom_state_ == atom_state_t::QUOTED)
        {
            if (cur_token->atom.size() > 0 && cur_token->atom.back() == codec::BACKSLASH_CHAR)
                token_list->back()->atom.back() = ch;
            else
                atom_state_ = atom_state_t::NONE;
        }
        break;
    }
    ...
}
...

I haven't even compiled this code, so please just refer to it. And please consider whether there are any other exceptional cases or if they may interfere with existing operation. If you think there's a better way than my suggestion, of course you're welcome to go with it.

yjm6560 commented 11 months ago

please forget the code I suggested. It doesn't seem to work correctly.

karastojko commented 11 months ago

@yjm6560 Thanks for the review, seems I cannot add you to the reviewers list. I have added also the escaping of the double backslash. Seems nothing is broken with this change, I'll check it a little bit more.