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

M0-1-9: constexpr used in array size is detected as dead code #678

Open
fjatWbyT opened this issue Sep 12, 2024 · 3 comments
Open

M0-1-9: constexpr used in array size is detected as dead code #678

fjatWbyT opened this issue Sep 12, 2024 · 3 comments
Labels
false positive/false negative An issue related to observed false positives or false negatives.

Comments

@fjatWbyT
Copy link
Contributor

Affected rules

  • M0-1-9 (dead code)

Description

Integer constant expression used for the size in an array declaration produces dead-code false positive. In other words, the e.g. constexpr int is not dead code because it is used to define the array size. This is contrast with when the size is (static) const, which does not produce the alert.

Example

int main() {
    constexpr int constexpr_unused = 1;   // True Positives, these first three are indeed unused / dead code
    static const int static_const_unused = 2;
    int unused_variable = 3;                            

    constexpr int constexpr_size       = 7;  // dead code detection  <-- False Positive, it is used in array a

    static const int static_const_size = 8;  // True Negative, 
                                             // (static) const doesn't trigger dead code

    int a[constexpr_size]    = {};  // The remaining have no dead code issue either
    int c[static_const_size] = {};  // they are used at the end

    return a[0] + c[2];
}
@fjatWbyT fjatWbyT added the false positive/false negative An issue related to observed false positives or false negatives. label Sep 12, 2024
@fjatWbyT
Copy link
Contributor Author

Fix Strategy Proposal

Add an additional case to the predicate isDeadStmt in DeadCode.qll. The approach in #660 can be reused to determine if the constant expression is used in an array size.

There could be value avoiding a bit of code duplication so that the M0-1-3 fix from that PR and this proposed fix for M0-1-9 share a predicate that returns the count.

As I am still learning CodeQL and writing my first queries, I have already been playing with the fix in c765f93 and unit-testing it. I tried first using the pure / maybePure builtin predicates, but I didn’t manage to separate the constexpr and const cases only with them.

@lcartey
Copy link
Collaborator

lcartey commented Sep 16, 2024

I think your fix sounds reasonable 👍

@fjatWbyT
Copy link
Contributor Author

Thank you 😊 I have applied it in #690.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false positive/false negative An issue related to observed false positives or false negatives.
Projects
None yet
Development

No branches or pull requests

2 participants