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

async_hooks: avoid decrementing iterator after erase #42749

Merged

Conversation

dygabo
Copy link
Member

@dygabo dygabo commented Apr 15, 2022

decrementing an iterator returned by std::vector::erase may have
undefined behaviour and should not be used. Decrementing end()
on an empty container is undefined and .erase() could leave
the container empty.

Instead, by calling vec.erase(it--) we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of AsyncHooks::RemoveContext perform the cleanup of empty
contexts upfront using std::remove_if because the iteration gets
interrupted as soon as the context to be removed has been found.

decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Apr 15, 2022
Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine there's no easy way to add a test for this, but mentioning it anyway in case I'm wrong.

@Trott Trott added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Apr 15, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 15, 2022
@nodejs-github-bot

This comment was marked as outdated.

@dygabo
Copy link
Member Author

dygabo commented Apr 15, 2022

I imagine there's no easy way to add a test for this, but mentioning it anyway in case I'm wrong.

I also don't think that would be easy to write since it is difficult to test undefined behaviour. It might even act differently on different stl implementations (on some even work correctly). But the new approach should not be controversial and existing tests must stay all green.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 17, 2022
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 17, 2022
@nodejs-github-bot nodejs-github-bot merged commit 083d4c4 into nodejs:master Apr 17, 2022
@nodejs-github-bot
Copy link
Collaborator

Landed in 083d4c4

@dygabo dygabo deleted the async_hooks-fix-empty-context-removal branch April 19, 2022 04:56
xtx1130 pushed a commit to xtx1130/node that referenced this pull request Apr 25, 2022
decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.

PR-URL: nodejs#42749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
targos pushed a commit that referenced this pull request Apr 28, 2022
decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.

PR-URL: #42749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
@targos targos mentioned this pull request May 2, 2022
juanarbol pushed a commit that referenced this pull request May 31, 2022
decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.

PR-URL: #42749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
danielleadams pushed a commit that referenced this pull request Jun 27, 2022
decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.

PR-URL: #42749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
targos pushed a commit that referenced this pull request Jul 11, 2022
decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.

PR-URL: #42749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
guangwong pushed a commit to noslate-project/node that referenced this pull request Oct 10, 2022
decrementing an iterator returned by `std::vector::erase` may have
undefined behaviour and should not be used. Decrementing `end()`
on an empty container is undefined and `.erase()` could leave
the container empty.

Instead, by calling `vec.erase(it--)` we decrement the valid iterator
before the erase operation but after being passed to the erase method.

In case of `AsyncHooks::RemoveContext` perform the cleanup of empty
contexts upfront using `std::remove_if` because the iteration gets
interrupted as soon as the context to be removed has been found.

PR-URL: nodejs/node#42749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants