Skip to content

Make alert messages consistent across languages #9816

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 48 commits into from
Aug 25, 2022

Conversation

erik-krogh
Copy link
Contributor

@erik-krogh erik-krogh commented Jul 13, 2022

The tests fail and I know it. The internal PR fixes the tests (see backref towards the bottom).

I recently added a QL-for-QL query for detecting inconsistent alert messages across languages.

This PR fixes most of the alerts found by that query such that the queries now use mostly the same message across languages.
I fixed the alerts by looking at the alert-message across all languages, and then I picked the message I liked the most.
That choice is sometimes somewhat arbitrary, so feedback is very welcome.

There are still some queries that use different messages, e.g. because they report a different level of detail, or because different alert messages better suit each language.
(Example: cpp really likes to use a callchain, and the alert message is therefore different from the other languages).

I fixed the alerts while developing the QL-for-QL query, and a lot of the fixed alert messages are not reported by the current query.
I tried to get the QL-for-QL query to less noisy, but I could only manage that if I also removed some TPs, so I might make the QL-for-QL query more noisy in the future.

Comment on lines 108 to 113
from ControlStructure s, BlockStmt eb
where
emptyBlock(s, eb) and
not emptyBlockContainsNonchild(eb) and
not lineComment(eb)
select eb, "Empty block without comment"
select eb, "Empty block without comment."

Check warning

Code scanning / CodeQL

Consistent alert message

The cpp/empty-block query does not have the same alert message as java.
Comment on lines 40 to 45
from BlockStmt s
where
(loopStmtWithEmptyBlock(s) or conditionalWithEmptyBlock(s)) and
not exists(CommentBlock c | c.getParent() = s) and
not exists(ForStmt fs | fs.getBody() = s and exists(fs.getAnUpdate()))
select s, "Empty block."
select s, "Empty block without comment."

Check warning

Code scanning / CodeQL

Consistent alert message

The cs/empty-block query does not have the same alert message as java.
@erik-krogh erik-krogh force-pushed the msgConsis branch 2 times, most recently from e7e5f2d to d55db77 Compare August 11, 2022 10:52
select v,
"Unused local variable " + v.getName() +
". The variable is never read or written to and should be removed."
select v, "The value assigned to local variable '" + v.getName() + "' is never used."
Copy link
Contributor

Choose a reason for hiding this comment

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

The new message says that the variable is being assigned a value, but the old message specifically says that it wan't written to. Which is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to be "wasn't written to". The query behaves slightly different compared to the other languages.

I'll revert this change.

Copy link
Contributor

@aschackmull aschackmull left a comment

Choose a reason for hiding this comment

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

Java looks mostly good to me - two minor comments, though.

@@ -58,4 +58,4 @@ where
not exists(AsmStmt s | f = s.getEnclosingFunction()) and
not v.getAnAttribute().getName() = "unused" and
not any(ErrorExpr e).getEnclosingFunction() = f // unextracted expr may use `v`
select v, "Variable " + v.getName() + " is not used"
select v, "The value assigned to local variable '" + v.getName() + "' is never used."
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems wrong. The value might actually be used (as it might just have been another variable). It's just the variable that's not being used.

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 used the alert message from Python to create this alert message for CPP.
I'll do it the other way around instead 👍

@@ -26,4 +25,4 @@ where
forall(int op | op = lhs.(BitwiseAndExpr).getAnOperand().getValue().toInt() | op < 0) and
// exception for cases involving macros
not e.isAffectedByMacro()
select e, "Potential unsafe sign check of a bitwise operation."
select e, "Sign check of a bitwise operation."
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems less detailed, and make it less likely the user will inspect the alert?

Copy link
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

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

Python 👍

Overall question: Will changing these alert messages cause all current Code Scanning alerts to be closed, and new ones to pop up instead?

Grammar nitpick

I think the new alert messages that read User-provided value flows to here and is used in a command. sounds odd grammatically. Shouldn't they be quantified to read A user-provided value flows to here and is used in a command.

Reading that sentence over a few times, I'm also starting to wonder about flows to here, which sounds odd, I would always say flows here.

(I not a gramtic genius, these just didn't match how I would talk with a person)

Personally, I would suggest A user-provided value flows here and is used in a command.

smowton
smowton previously approved these changes Aug 23, 2022
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Go lgtm

@erik-krogh
Copy link
Contributor Author

Overall question: Will changing these alert messages cause all current Code Scanning alerts to be closed, and new ones to pop up instead?

Doesn't look like it. I tried to change an alert message here, and code-scanning reported no change in alerts.

Grammar nitpick

....
Personally, I would suggest A user-provided value flows here and is used in a command.

There is a lot of those: https://cs.github.com/github/codeql?q=%22%5C%22User-provided+value%22
Also beyond the queries I've touched here.
So that seems like a nice idea for a followup PR.

@erik-krogh erik-krogh dismissed stale reviews from smowton and esbena via 1a7d3ee August 23, 2022 10:35
@@ -16,4 +16,4 @@ where
e.isStrict() and
e.getGreaterOperand() instanceof BitwiseExpr and
e.getLesserOperand().(IntegerLiteral).getIntValue() = 0
select e, "Sign check of a bitwise operation."
select e, "Potential unsafe sign check of a bitwise operation."
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't that be

Suggested change
select e, "Potential unsafe sign check of a bitwise operation."
select e, "Potentially unsafe sign check of a bitwise operation."

Copy link
Contributor

Choose a reason for hiding this comment

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

Either works gramatically, with different bindings:

Potential (unsafe check)
(Potentially unsafe) check

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I just felt that the latter was closer to the intended message. But I guess both work semantically.

Comment on lines 41 to 46
from Name unused, LocalVariable v
where
unused_local(unused, v) and
// If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
forall(Name el | el = unused.getParentNode().(Tuple).getAnElt() | unused_local(el, _))
select unused, "The value assigned to local variable '" + v.getId() + "' is never used."
select unused, "Variable " + v.getId() + " is not used"

Check warning

Code scanning / CodeQL

Consistent alert message

The py/unused-local-variable query does not have the same alert message as java.
aibaars
aibaars previously approved these changes Aug 25, 2022
Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

Added a minor comment, otherwise C# LGTM.

@@ -35,4 +35,4 @@ where
access = cast.getAChild() and
access.getTarget().getDeclaringElement() = access.getEnclosingCallable() and
nodeBeforeParameterAccess(access.getAControlFlowNode())
select cast, "Missing type-check before casting parameter to 'Equals'."
select cast, "Equals() method does not seem to check argument type."
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't @precision high mean high confidence in the result? If so, then we could change does not seem to check to does not check.

Copy link
Contributor

@nickrolfe nickrolfe left a comment

Choose a reason for hiding this comment

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

Changes since the previous approvals look good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C# C++ depends on internal PR This PR should only be merged in sync with an internal Semmle PR documentation Go Java JS Python Ruby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants