Skip to content

Commit 4cb16b2

Browse files
Implement Language4 package, banning obsolete language features.
Many of the cases outlined in the amendment are covered by other rules. Add support for new cases where possible (was not possible for ID 3, storage class specifiers not at beginning of declaration, or ID 2, which is a feature of the implementation not determinable by static analysis), and reference existing rules in one comprehensive test for maximal clarity that those parts of rule 1-5 are indeed supported by our existing queries.
1 parent 021fabb commit 4cb16b2

22 files changed

+278
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/call-to-realloc-with-size-zero
3+
* @name RULE-1-5: Disallowed size argument value equal to zero in call to realloc
4+
* @description Invoking realloc with a size argument set to zero is implementation-defined behavior
5+
* and declared as an obsolete feature in C18.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-1-5
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import semmle.code.cpp.rangeanalysis.new.RangeAnalysis
17+
18+
from FunctionCall call, Expr arg
19+
where
20+
not isExcluded(call, Language4Package::callToReallocWithSizeZeroQuery()) and
21+
call.getTarget().hasGlobalOrStdName("realloc") and
22+
arg = call.getArgument(1) and
23+
upperBound(arg) = 0
24+
select arg, "Calling realloc with size zero results in implementation-defined behavior."
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @id c/misra/invalid-define-or-undef-of-std-bool-macro
3+
* @name RULE-1-5: Programs may not undefine or redefine the macros bool, true, or false
4+
* @description Directives that undefine and/or redefine the standard boolean macros has been
5+
* declared an obsolescent language feature since C99.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-1-5
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
string getABoolMacroName() { result = ["true", "false", "bool"] }
19+
20+
from PreprocessorDirective directive, string opString, string macroName
21+
where
22+
not isExcluded(directive, Language4Package::invalidDefineOrUndefOfStdBoolMacroQuery()) and
23+
macroName = getABoolMacroName() and
24+
(
25+
macroName = directive.(Macro).getName() and
26+
opString = "define"
27+
or
28+
macroName = directive.(PreprocessorUndef).getName() and
29+
opString = "undefine"
30+
)
31+
select directive, "Invalid " + opString + " of boolean standard macro " + macroName
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/use-of-obsolete-macro-atomic-var-init
3+
* @name RULE-1-5: Disallowed usage of obsolete macro ATOMIC_VAR_INIT compiled as C18
4+
* @description The macro ATOMIC_VAR_INIT is has been declared an obsolescent language feature since
5+
* C18.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/rule-1-5
10+
* maintainability
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
from MacroInvocation invoke, Compilation c, string flag
19+
where
20+
not isExcluded(invoke, Language4Package::useOfObsoleteMacroAtomicVarInitQuery()) and
21+
invoke.getMacroName() = "ATOMIC_VAR_INIT" and
22+
flag = c.getAnArgument() and
23+
flag.regexpMatch("-std=c1[78]")
24+
select invoke, "Usage of macro ATOMIC_VAR_INIT() is considered obsolete for c version " + flag
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:13:14:13:14 | 0 | Calling realloc with size zero results in implementation-defined behavior. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-5/CallToReallocWithSizeZero.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:40:6:40:7 | f2 | Function f2 does not specify void for no parameters present. |
2+
| test.c:44:5:44:6 | f5 | Function f5 declares parameter in unsupported declaration list. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-2/FunctionTypesNotInPrototypeForm.ql
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.c:23:1:23:14 | #define true 3 | Invalid define of boolean standard macro true |
2+
| test.c:24:1:24:15 | #define false 3 | Invalid define of boolean standard macro false |
3+
| test.c:25:1:25:18 | #define bool int * | Invalid define of boolean standard macro bool |
4+
| test.c:26:1:26:11 | #undef true | Invalid undefine of boolean standard macro true |
5+
| test.c:27:1:27:12 | #undef false | Invalid undefine of boolean standard macro false |
6+
| test.c:28:1:28:11 | #undef bool | Invalid undefine of boolean standard macro bool |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-5/InvalidDefineOrUndefOfStdBoolMacro.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:10:12:10:17 | call to malloc | Use of banned dynamic memory allocation. |
2+
| test.c:13:3:13:9 | call to realloc | Use of banned dynamic memory allocation. |
3+
| test.c:16:3:16:9 | call to realloc | Use of banned dynamic memory allocation. |

0 commit comments

Comments
 (0)