Skip to content

message::get_part_by_name causes segfault if the key it searches for does not exist #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Iuliean opened this issue Jul 18, 2022 · 2 comments · Fixed by #501
Closed

message::get_part_by_name causes segfault if the key it searches for does not exist #500

Iuliean opened this issue Jul 18, 2022 · 2 comments · Fixed by #501
Labels
bug Something isn't working

Comments

@Iuliean
Copy link
Contributor

Iuliean commented Jul 18, 2022

I was working with this

void Site::auth(const crow::request& req, crow::response& resp)
{
    CROW_LOG_INFO << "Login Attempt";
    crow::multipart::message msg(req);
    CROW_LOG_INFO << "Login Attempt";
    if(msg.get_part_by_name("password").body == password)
    {
        resp.body = Session::newSession();
        resp.code = 200;
        resp.end();
        return;
    }
    else
    {
        resp.code = 401;
        resp.body = "Failed login attempt";
        resp.end();
        return;
    }
}

And the server kept crashing when a request without that key was made

so i investigated

part get_part_by_name(const std::string& name)
{
    return part_map.find(name)->second;
}

this current implementation tries to imediately acces the object but if that object does not exist it tries to access part.end() which is unallocated memory.

i replaced it with

part get_part_by_name(const std::string& name)
{
    mp_map::iterator result = part_map.find(name);
    if(result != part_map.end())
        return result->second;
    else
        return {}; 
}

which seems to fix the issue and i am still able to check the body of the part or headers

please let me know if everything is correct and my fix is good :)

@The-EDev The-EDev added the bug Something isn't working label Jul 18, 2022
@The-EDev
Copy link
Member

Your solution looks good, Feel free to open a PR with your solution (I'll do it if you don't want to :)).

Thank you for taking the time to report and investigate the issue!

@Iuliean
Copy link
Contributor Author

Iuliean commented Jul 18, 2022

is there a guide or something ?
i've never made one before and i was kinda hunting to find something to fix :))

Edit : nvm i figured it out

@The-EDev The-EDev linked a pull request Jul 18, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants