Skip to content

C++: Fix AV Rule 85 #371

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

Merged
merged 2 commits into from
Oct 30, 2018
Merged

C++: Fix AV Rule 85 #371

merged 2 commits into from
Oct 30, 2018

Conversation

ian-semmle
Copy link
Contributor

We have to be careful to avoid giving alerts to functions that might be correctly defined, but we can't see the definition as they weren't instantiated.

We have to be careful to avoid giving alerts to functions that might be
correctly defined, but we can't see the definition as it wasn't
instantiated.
@ian-semmle ian-semmle added the C++ label Oct 25, 2018
Copy link
Contributor

@jonas-semmle jonas-semmle left a comment

Choose a reason for hiding this comment

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

The original query gave alerts for both templates and instantiations, and the alerts for instantiations are now causing issues if I understand you correctly. Why not fix that by removing the alerts for instantiations and only keeping them for uninstantiated templates and non-templates?

| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 90, but it is not defined in terms of its opposite operator operator>. |
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 88, but it is not defined in terms of its opposite operator operator<=. |
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 85, but it is not defined in terms of its opposite operator operator<. |
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 83, but it is not defined in terms of its opposite operator operator>=. |
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look right. Why do the alerts for MyClass{5,6,7} go away completely? The comments in the test source say they should still be there. And why is there no alert added for the new MyClass10?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For 5, 6, 7 no function instantiations are extracted. For 10, no <= function instantiated is extracted.

I left the comments as they are still bad, even if we don't currently detect them.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should modify the test to instantiate these functions, and add just one test case where the function is never instantiated to document that behaviour. This way the tests will be providing coverage for whatever issues they were originally intended to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These cases really are just testing the various cases where some things are and aren't used. Although the current query definition means that several of them get no alerts, I think they're still useful test cases for potential future changes to the query.

@ian-semmle
Copy link
Contributor Author

We don't get bodies for the templates, so we can't give alerts based on them.

@jonas-semmle
Copy link
Contributor

I'm still confused by this PR and by this query. The test seems to have produced alerts for code marked as // GOOD since it was introduced in 174f1fe1c197e88220efa6b99fcece06cfacd199.

Is it correct that if we get the body for an instantiation then we also get the body of its template? If that's the case, we should be able to ignore instantiations in this query. That seems a lot easier than trying to match instantiated operators with their templates by matching on the name. Matching by name is fragile in any case as operators can be overloaded. I'd much appreciate some QLDoc about which declarations and bodies are guaranteed to get extracted, especially now that it's changed after discover_walk.

Ignoring instantiations doesn't by itself fix the problems with this query because we also need to suppress alerts from operators whose body did not get extracted. This might mean we'll end up suppressing all the same true positives that you're suppressing in this PR, but it should still make the query less complicated.

exists(Function f | f = c.getAMember() and
f.hasName(op) and
f.isDefined())))
else any()
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be more readable without the if, as exists(Class temp1 | c.isConstructedFrom(temp1) and ...) or not exists(Class temp1 | c.isConstructedFrom(temp1))?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to be an implies

@ian-semmle
Copy link
Contributor Author

174f1fe was PR 23883, which has description "It looks like the query is doing the wrong thing for templates, but this just adds a test of the status quo for now.".

I've changed it to look at the template bodies instead of the instantiation bodies. Note that this means allowing a LTExpr rather than a FunctionCall to operator<.

Copy link
Contributor

@jbj jbj left a comment

Choose a reason for hiding this comment

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

Thanks, @ian-semmle

@jbj jbj merged commit 3340e79 into github:next Oct 30, 2018
@ian-semmle ian-semmle deleted the av85 branch October 30, 2018 11:43
aibaars pushed a commit that referenced this pull request Oct 20, 2021
Data flow: Fix bug for sugared call arguments
smowton pushed a commit to smowton/codeql that referenced this pull request Apr 16, 2022
Fix `ExprStmt` and `StmtExpr` in Boolean context
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.

4 participants