github/codeql-coding-standards

`DCL53-CPP`: Reports non syntactically ambiguous object declarations

lcartey opened this issue · 0 comments

Affected rules

  • DCL53-CPP

Description

LocalConstructorInitializedObjectHidesIdentifier.ql currently identifies variable declarations that call a constructor and hide an outer scope variable. However, this does not fully capture the cases covered by this vexing parsing situation, which is looking for S1(g1).

The following additions to the query could help address this problem:

  v.getInitializer().getExpr().(ConstructorCall).getNumberOfArguments() = 0 and
  not v.getInitializer().isBraced()

However this would still flag S1 g3; below - as we don't currently have a record of where the brackets were during parsing.

Example

This modification of the test case highlights the problems:

int g1 = 0;
int g2 = 0;
int g3 = 0;
int g4 = 0;
int g5 = 0;
int g6 = 0;
void f1() {
  S1(g1);   // NON_COMPLIANT
  S1 g2();  // NON_COMPLIANT
  S1 g3;    // COMPLIANT[FALSE_POSITIVE]
  S1 g4{};  // COMPLIANT[FALSE_POSITIVE]
  S1 g5(1); // COMPLIANT[FALSE_POSITIVE]
  S1 g6{1}; // COMPLIANT[FALSE_POSITIVE]
}