-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #719 from github/michaelrfairhurst/implement-funct…
…ion-types-package Implement function types package
- Loading branch information
Showing
9 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
c/misra/src/rules/RULE-17-12/FunctionAddressesShouldAddressOperator.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @id c/misra/function-addresses-should-address-operator | ||
* @name RULE-17-12: A function identifier should only be called with a parenthesized parameter list or used with a & | ||
* @description A function identifier should only be called with a parenthesized parameter list or | ||
* used with a & (address-of). | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-17-12 | ||
* readability | ||
* external/misra/c/2012/amendment3 | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
|
||
predicate isImplicitlyAddressed(FunctionAccess access) { | ||
not access.getParent() instanceof AddressOfExpr and | ||
// Note: the following *seems* to only exist in c++ codebases, for instance, | ||
// when calling a member. In c, this syntax should always extract as a | ||
// [FunctionCall] rather than a [ExprCall] of a [FunctionAccess]. Still, this | ||
// is a good pattern to be defensive against. | ||
not exists(ExprCall call | call.getExpr() = access) | ||
} | ||
|
||
from FunctionAccess funcAccess | ||
where | ||
not isExcluded(funcAccess, FunctionTypesPackage::functionAddressesShouldAddressOperatorQuery()) and | ||
isImplicitlyAddressed(funcAccess) | ||
select funcAccess, | ||
"The address of function " + funcAccess.getTarget().getName() + | ||
" is taken without the & operator." |
13 changes: 13 additions & 0 deletions
13
c/misra/test/rules/RULE-17-12/FunctionAddressesShouldAddressOperator.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
| test.c:14:25:14:29 | func2 | The address of function func2 is taken without the & operator. | | ||
| test.c:15:25:15:29 | func3 | The address of function func3 is taken without the & operator. | | ||
| test.c:21:12:21:16 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:38:3:38:7 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:39:3:39:7 | func2 | The address of function func2 is taken without the & operator. | | ||
| test.c:57:13:57:17 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:58:21:58:25 | func2 | The address of function func2 is taken without the & operator. | | ||
| test.c:59:13:59:17 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:59:20:59:24 | func2 | The address of function func2 is taken without the & operator. | | ||
| test.c:67:11:67:15 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:68:12:68:16 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:69:12:69:16 | func1 | The address of function func1 is taken without the & operator. | | ||
| test.c:71:18:71:22 | func1 | The address of function func1 is taken without the & operator. | |
1 change: 1 addition & 0 deletions
1
c/misra/test/rules/RULE-17-12/FunctionAddressesShouldAddressOperator.qlref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rules/RULE-17-12/FunctionAddressesShouldAddressOperator.ql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
void func1() {} | ||
void func2(int x, char *y) {} | ||
|
||
typedef struct { | ||
} s; | ||
|
||
int func3() { return 0; } | ||
|
||
typedef void (*func_ptr_t1)(); | ||
typedef void (*func_ptr_t2)(int x, char *y); | ||
typedef s (*func_ptr_t3)(); | ||
|
||
func_ptr_t1 func_ptr1 = &func1; // COMPLIANT | ||
func_ptr_t2 func_ptr2 = func2; // NON-COMPLIANT | ||
func_ptr_t3 func_ptr3 = func3 + 0; // NON-COMPLIANT | ||
|
||
void take_func(func_ptr_t1 f1, func_ptr_t2 f2); | ||
|
||
func_ptr_t1 returns_func(int x) { | ||
if (x == 0) { | ||
return func1; // NON-COMPLIANT | ||
} else if (x == 1) { | ||
return &func1; // COMPLIANT | ||
} | ||
|
||
return returns_func(0); // COMPLIANT | ||
} | ||
|
||
#define MACRO_IDENTITY(f) (f) | ||
#define MACRO_INVOKE_RISKY(f) (f()) | ||
#define MACRO_INVOKE_IMPROVED(f) ((f)()) | ||
#define MACRO_INVOKE_AND_USE_AS_TOKEN(f) f(0, #f) | ||
|
||
void test() { | ||
func1(); // COMPLIANT | ||
func2(1, "hello"); // COMPLIANT | ||
|
||
func1; // NON-COMPLIANT | ||
func2; // NON-COMPLIANT | ||
|
||
&func1; // COMPLIANT | ||
&func2; // COMPLIANT | ||
|
||
(func1)(); // COMPLIANT | ||
(func2)(1, "hello"); // COMPLIANT | ||
|
||
&(func1); // COMPLIANT | ||
&(func2); // COMPLIANT | ||
|
||
(&func1)(); // COMPLIANT | ||
(&func2)(1, "hello"); // COMPLIANT | ||
|
||
(func1()); // COMPLIANT | ||
(func2(1, "hello")); // COMPLIANT | ||
|
||
take_func(&func1, &func2); // COMPLIANT | ||
take_func(func1, &func2); // NON-COMPLIANT | ||
take_func(&func1, func2); // NON-COMPLIANT | ||
take_func(func1, func2); // NON-COMPLIANT | ||
|
||
returns_func(0); // COMPLIANT | ||
returns_func(0)(); // COMPLIANT | ||
(returns_func(0))(); // COMPLIANT | ||
|
||
(void *)&func1; // COMPLIANT | ||
(void *)(&func1); // COMPLIANT | ||
(void *)func1; // NON-COMPLIANT | ||
(void *)(func1); // NON-COMPLIANT | ||
((void *)func1); // NON-COMPLIANT | ||
|
||
MACRO_IDENTITY(func1); // NON-COMPLIANT | ||
MACRO_IDENTITY(func1)(); // NON-COMPLIANT[FALSE NEGATIVE] | ||
MACRO_IDENTITY(&func1); // COMPLIANT | ||
MACRO_IDENTITY (&func1)(); // COMPLIANT | ||
|
||
MACRO_INVOKE_RISKY(func3); // NON-COMPLIANT[FALSE NEGATIVE] | ||
MACRO_INVOKE_IMPROVED(func3); // NON-COMPLIANT[FALSE NEGATIVE] | ||
MACRO_INVOKE_IMPROVED(&func3); // COMPLIANT | ||
|
||
MACRO_INVOKE_AND_USE_AS_TOKEN(func1); // COMPLIANT | ||
|
||
// Function pointers are exempt from this rule. | ||
func_ptr1(); // COMPLIANT | ||
func_ptr2(1, "hello"); // COMPLIANT | ||
func_ptr1; // COMPLIANT | ||
func_ptr2; // COMPLIANT | ||
&func_ptr1; // COMPLIANT | ||
&func_ptr2; // COMPLIANT | ||
(func_ptr1)(); // COMPLIANT | ||
(func_ptr2)(1, "hello"); // COMPLIANT | ||
(*func_ptr1)(); // COMPLIANT | ||
(*func_ptr2)(1, "hello"); // COMPLIANT | ||
take_func(func_ptr1, func_ptr2); // COMPLIANT | ||
(void *)func_ptr1; // COMPLIANT | ||
(void *)&func_ptr1; // COMPLIANT | ||
(void *)(&func_ptr1); // COMPLIANT | ||
(void *)func_ptr1; // COMPLIANT | ||
(void *)(func_ptr1); // COMPLIANT | ||
((void *)func_ptr1); // COMPLIANT | ||
MACRO_IDENTITY(func_ptr1); // COMPLIANT | ||
MACRO_IDENTITY(func_ptr1)(); // COMPLIANT | ||
MACRO_IDENTITY(&func_ptr1); // COMPLIANT | ||
(*MACRO_IDENTITY(&func_ptr1))(); // COMPLIANT | ||
MACRO_INVOKE_RISKY(func_ptr3); // COMPLIANT | ||
MACRO_INVOKE_IMPROVED(func_ptr3); // COMPLIANT | ||
MACRO_INVOKE_IMPROVED(*&func_ptr3); // COMPLIANT | ||
} |
26 changes: 26 additions & 0 deletions
26
cpp/common/src/codingstandards/cpp/exclusions/c/FunctionTypes.qll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
import cpp | ||
import RuleMetadata | ||
import codingstandards.cpp.exclusions.RuleMetadata | ||
|
||
newtype FunctionTypesQuery = TFunctionAddressesShouldAddressOperatorQuery() | ||
|
||
predicate isFunctionTypesQueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
query = | ||
// `Query` instance for the `functionAddressesShouldAddressOperator` query | ||
FunctionTypesPackage::functionAddressesShouldAddressOperatorQuery() and | ||
queryId = | ||
// `@id` for the `functionAddressesShouldAddressOperator` query | ||
"c/misra/function-addresses-should-address-operator" and | ||
ruleId = "RULE-17-12" and | ||
category = "advisory" | ||
} | ||
|
||
module FunctionTypesPackage { | ||
Query functionAddressesShouldAddressOperatorQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `functionAddressesShouldAddressOperator` query | ||
TQueryC(TFunctionTypesPackageQuery(TFunctionAddressesShouldAddressOperatorQuery())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"MISRA-C-2012": { | ||
"RULE-17-12": { | ||
"properties": { | ||
"obligation": "advisory" | ||
}, | ||
"queries": [ | ||
{ | ||
"description": "A function identifier should only be called with a parenthesized parameter list or used with a & (address-of).", | ||
"kind": "problem", | ||
"name": "A function identifier should only be called with a parenthesized parameter list or used with a &", | ||
"precision": "very-high", | ||
"severity": "error", | ||
"short_name": "FunctionAddressesShouldAddressOperator", | ||
"tags": [ | ||
"readability", | ||
"external/misra/c/2012/amendment3" | ||
] | ||
} | ||
], | ||
"title": "A function identifier should only be called with a parenthesized parameter list or used with a & (address-of)" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters