Skip to content

C++: Always recognize pointers as iterators#7159

Merged
geoffw0 merged 3 commits into
github:mainfrom
MathiasVP:pointers-are-always-iterators
Nov 18, 2021
Merged

C++: Always recognize pointers as iterators#7159
geoffw0 merged 3 commits into
github:mainfrom
MathiasVP:pointers-are-always-iterators

Conversation

@MathiasVP
Copy link
Copy Markdown
Contributor

In order to figure out which parameters can carry flow out of a function, we have a predicate parameterIsNonConstReference. That predicate handles pointers and iterators in one case (since pointers are also iterators thanks to the T* specialization of iterator_traits).

However, if that specialization isn't in the database for some reason, we won't always recognize that a parameter is of a type that can carry flow out of a function.

This PR adds a subclass of our modeling of iterator so that pointers are always a possible iterator.

@MathiasVP MathiasVP added the C++ label Nov 17, 2021
@MathiasVP MathiasVP requested a review from a team as a code owner November 17, 2021 14:47
*/
private class IteratorByPointer extends Iterator instanceof PointerType {
IteratorByPointer() { not this instanceof IteratorByTraits }
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This LGTM.

Do you have any idea what the downside would be if we made something an Iterator when it shouldn't be? e.g. if we made every IntegralType an Iterator, would we get some kind of incorrect flow through integer parameters?

Copy link
Copy Markdown
Contributor Author

@MathiasVP MathiasVP Nov 17, 2021

Choose a reason for hiding this comment

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

Yeah, I think such a change would mean that we get flow in cases like:

void f(int n) {
  n = source();
}

void test() {
  int n = 0;
  f(n);
  sink(n);
}

I'm not sure if we've set up everything such that this was the only thing necessary to get flow in such cases, but it would certainly be a likely outcome of such a change.

Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@geoffw0 geoffw0 merged commit 6c2713d into github:main Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants