diff --git a/change_notes/2023-05-02-single-reserved-prefix-generated.md b/change_notes/2023-05-02-single-reserved-prefix-generated.md new file mode 100644 index 0000000000..59bde6cca0 --- /dev/null +++ b/change_notes/2023-05-02-single-reserved-prefix-generated.md @@ -0,0 +1 @@ + * `DCL51-CPP` - `cpp/cert/use-of-single-underscore-reserved-prefix` - remove false positives which were compiler generated, such as the function `_FUN` generated by the compiler for lambdas converted to function pointers. \ No newline at end of file diff --git a/cpp/cert/src/rules/DCL51-CPP/UseOfSingleUnderscoreReservedPrefix.ql b/cpp/cert/src/rules/DCL51-CPP/UseOfSingleUnderscoreReservedPrefix.ql index cf47ad444d..e2f7270f9c 100644 --- a/cpp/cert/src/rules/DCL51-CPP/UseOfSingleUnderscoreReservedPrefix.ql +++ b/cpp/cert/src/rules/DCL51-CPP/UseOfSingleUnderscoreReservedPrefix.ql @@ -53,5 +53,8 @@ where isGeneratedByUserMacro(d) ) ) - ) + ) and + // Ignore compiler generated functions and variables + not l.(Function).isCompilerGenerated() and + not l.(Variable).isCompilerGenerated() select l, "Name $@ uses the reserved prefix '_'.", l, s diff --git a/cpp/cert/test/rules/DCL51-CPP/test.cpp b/cpp/cert/test/rules/DCL51-CPP/test.cpp index 028867b88f..5e27dd2390 100644 --- a/cpp/cert/test/rules/DCL51-CPP/test.cpp +++ b/cpp/cert/test/rules/DCL51-CPP/test.cpp @@ -1,8 +1,8 @@ +#include "test.h" #include +#include #include -#include "test.h" - #undef INT_MAX // NON_COMPLIANT #define SIZE_MAX 256 // NON_COMPLIANT @@ -40,4 +40,12 @@ FD_SET(j); // COMPLIANT - standard library macro void f() { std::string x = __func__; // COMPLIANT +} + +void g(int (*l)(int)) {} + +void test_lambda(const int y) { + // Lambda generates a static function called `_FUN` when the lambda is + // converted to a function pointer + g([](int x) { return x; }); // COMPLIANT - compiler generated } \ No newline at end of file