Skip to content
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

Graph<T>::removeNode has potential to throw due to optional being accessed early #418

Closed
nolankramer opened this issue Apr 27, 2024 · 5 comments · Fixed by #430
Closed
Labels
bug Something isn't working core something about core enhancement New feature or request good first issue Good for newcomers Priority:High Priority Label for high priority issue

Comments

@nolankramer
Copy link
Collaborator

nolankramer commented Apr 27, 2024

The implementation that added isolated nodes seems to be assuming that nodeOpt will always have a value. This is untrue if the node does not exist:

template <typename T>
void Graph<T>::removeNode(const std::string &nodeUserId) {
  auto nodeOpt = getNode(nodeUserId);
  auto isolatedNodeIt = isolatedNodesSet.find(nodeOpt.value());
  ...

nodeOpt.value() is called before verifying the optional has something in it.

This should be updated to something like this:

template <typename T>
void Graph<T>::removeNode(const std::string &nodeUserId) {
  auto nodeOpt = getNode(nodeUserId);
  auto isolatedNodeIt = isolatedNodesSet.end();
  if (nodeOpt) {
    isolatedNodeIt  = isolatedNodeIt.find(nodeOpt.value());
  }
  ...

The tests should also be updated to test the case where we try to remove a node that does not exist in the graph.

@ZigRazor ZigRazor added bug Something isn't working enhancement New feature or request good first issue Good for newcomers core something about core Priority:High Priority Label for high priority issue labels Apr 29, 2024
@Ajay-26
Copy link
Contributor

Ajay-26 commented May 6, 2024

Hi, I would like to have a crack at this issue. I'm pretty new to the open source world, and this would be my first contribution. I am currently reading through the documentation and contribution guidelines. Thank you for your patience :)

@nolankramer
Copy link
Collaborator Author

@Ajay-26 Thanks! Go for it!

@Ajay-26
Copy link
Contributor

Ajay-26 commented May 6, 2024

Hi @nolankramer, I just made a PR from my branch to the head of the repo. I see that the test for Labeler / triage (pull_request_target) has failed, but I can't make much sense of the feedback from the test. I'm trying to fix that before I complete my PR, could you guide me on how to do it ? I can retract this PR and work on those changes.

@nolankramer
Copy link
Collaborator Author

@Ajay-26 No need to retract the PR. It seems like an unrelated issue. Feel free to ignore it.

@nolankramer
Copy link
Collaborator Author

I've added some feedback on the PR to point you in the right direction :)

ZigRazor pushed a commit that referenced this issue Jun 20, 2024
… optional being accessed early (#430)

* optional should first check whether the node indeed exists

* fixed typo in impl and also added test for removing node that was never added

* incorporated review into PR

* resolved use after free for map iterator

* reformatted code as per PR feedback

* reformatted code as per PR feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core something about core enhancement New feature or request good first issue Good for newcomers Priority:High Priority Label for high priority issue
Projects
None yet
3 participants